How to send an email with content from a view to codeigniter

I want to send an email to a user from my application with the contents of the email downloaded from the view. This is the code I've tried so far:

$toemail = "user@email.id";

$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();
+3
source share
3 answers
  • You need to < make a call $this->load->library('email');in the controller, as well as for email to work in CI.
  • Also, in your code: $fromemailnot initialized.
  • Your server must have SMTP support .
  • $ config must be declared as an array before assigning values ​​and keys.

Work code:

$this->load->library('email');
$fromemail="ad@c.com";
$toemail = "user@email.id";
$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); , . true, , .

Edit: $this->load->view(); , $mesg = $this->load->view(view,data,true);,

+10

$mesg = $this- > load- > view ('template/email', true);
$mesg = $this- > load- > view ('template/email', '', true)
true,

+7

Sending an email template In codeigniter we need to put a meta tag and send it by 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('emailaddres@mail.com');
$this->email->subject($subject);
$this->email->message($message);
$this->email->send();
+1
source

All Articles