Akhil Akhil - 3 months ago 10
PHP Question

Sending form data to an email codeigniter

I am trying to send the form data to an email. The functions are running perfectly but email is not going to the specified email address. I am getting the response as 100 but there is no email received.

My controller:

<?php
class Contact extends CI_Controller {

public function sendemail()
{
$this->load->model('Contactmodel');

$response=array();
$response=$this->Contactmodel->sendemail();
echo json_encode($response);
}
}
?>


My model

<?php
class Contactmodel extends CI_Model
{
public function _construct()
{
parent::_construct();
}

public function sendemail()
{
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];
$message = $_REQUEST['message'];

$returnarray = array();
$to_email = 'cse.akhilraj@gmail.com';
$this->email->to($to_email);
$this->email->from($email, $name);
$this->email->subject($subject);
$this->email->message($message);

if($this->email->send())
{
$returnarray[0] = 100;
}
else
{
$returnarray[0] = 101;
}

return $returnarray;
}
}
?>

Answer
    <?php
    class Contactmodel extends CI_Model
    {

        public function _construct()
        {
            parent::_construct();
        }

        public function sendemail()
        {
            $name = $_REQUEST['name'];
            $email = $_REQUEST['email'];
            $subject = $_REQUEST['subject'];
            $message = $_REQUEST['message'];

            $config = Array(
                    'protocol' => 'smtp',
                    'smtp_host' => 'ssl://smtp.googlemail.com',
                    'smtp_port' => '465',
                    'smtp_user' => 'xxxxx', //email-id of company's
                    'smtp_pass' => 'xxxxx', //password
                    'mailtype'  => 'html',
                    'charset'   => 'iso-8859-1'
                    );
                    $to_email = 'cse.akhilraj@gmail.com';
                    $fromemail = ''; // same email-id whatever smtp_user
                    $sendername = 'your sender name';
                    $this->load->library('email',$config);
                    $this->email->set_newline("\r\n");
                    $this->email->to($to_email);
                    $this->email->from($fromemail, $sendername);
                    $this->email->subject($subject);
                    $message = 'Name = '.$name.', Email-id='.$email.'  '.$message;  // or html code or view 
/*
$email_data = array('name'=>$name,'email'=>$email,'message'=>$message);
$message = $this->load->view('../views/mail/email_view',$email_data,TRUE);
and use variable in view but first try normal one
*/
                    $this->email->message($message);
                    $returnarray = array();
                    $returnarray[0] = ($this->email->send()?100:101);
                    return $returnarray;
        }
    }
    ?>
Comments