How to send an email with content from a View in codeigniter

I want to send an email to user from my application with the content of the email loaded from a view . This is the code i’ve tried out till now:

$toemail = "[email protected]";

$subject = "Mail Subject is here";
$mesg = $this->load->view('template/email');

$this->load->library('email');

$config['charset'] = 'utf-8';
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';

$this->email->initialize($config);

$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

  1. You need to call $this->load->library('email'); within the controller as well for the email in CI to work.
  2. Also , in your code : $fromemail is not initialized.
  3. You need to have SMTP support on your server.
  4. $config should be declared as an array before assigning values and keys.

Working Code:

$this->load->library('email');
$fromemail="[email protected]";
$toemail = "[email protected]";
$subject = "Mail Subject is here";
$data=array();
// $mesg = $this->load->view('template/email',$data,true);
// or
$mesg = $this->load->view('template/email','',true);


$config=array(
'charset'=>'utf-8',
'wordwrap'=> TRUE,
'mailtype' => 'html'
);

$this->email->initialize($config);

$this->email->to($toemail);
$this->email->from($fromemail, "Title");
$this->email->subject($subject);
$this->email->message($mesg);
$mail = $this->email->send();

Edit:
$mesg = $this->load->view('template/email',true); should be having the true as pointed out by lycanian. By setting it as true , it doesn’t send data to the output stream but it will return as a string.

Edit:
$this->load->view(); need a second parameter with data or empty like $mesg = $this->load->view(view,data,true);, if not it wont work

Solution 2

This line $mesg = $this->load->view(‘template/email’,true); should be like this

$mesg = $this->load->view(‘template/email’,”,true);
with the single quotes before the value true, and it will work perfectly

Solution 3

Email template send In codeigniter we need to put and meta tag before sending email

$this->data['data'] = $data;
$message = $this->load->view('emailer/create-account', $this->data,  TRUE);
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
$this->email->from($email, $name);
$this->email->to('[email protected]');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();

Solution 4

U will try it!! it’s working for mine after many errors facing

            $subject = 'New message.';
            $config = Array(        
                'protocol' => 'sendmail',
                'smtp_host' => 'Your smtp host',
                'smtp_port' => 465,
                'smtp_user' => 'webmail',
                'smtp_pass' => 'webmail pass',
                'smtp_timeout' => '4',
                'mailtype'  => 'html', 
                'charset'   => 'utf-8',
                'wordwrap' => TRUE
            );
            $this->load->library('email', $config);
            $this->email->set_newline("\r\n");
            $this->email->set_header('MIME-Version', '1.0; charset=utf-8');
            $this->email->set_header('Content-type', 'text/html');

            $this->email->from('from mail address', 'Company name ');
            $data = array(
                 'message'=> $this->input->post('message')
                     );
            $this->email->to($toEmail);  
            $this->email->subject($subject); 

            $body = $this->load->view('email/sendmail.php',$data,TRUE);
            $this->email->message($body);   
            $this->email->send();

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply