如何从codeigniter中的视图发送包含内容的电子邮件 [英] How to send an email with content from a View in codeigniter

查看:27
本文介绍了如何从codeigniter中的视图发送包含内容的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序向用户发送一封电子邮件,其中包含从 view 加载的电子邮件内容.这是我迄今为止尝试过的代码:

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 = "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();

推荐答案

  1. 需要在控制器中调用 $this->load->library('email'); 以便 CI 中的电子邮件工作.
  2. 此外,在您的代码中:$fromemail 未初始化.
  3. 您的服务器需要SMTP 支持.
  4. $config 应该在分配值和键之前声明为一个数组.
  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.

工作代码:

$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();

$mesg = $this->load->view('template/email',true); 应该具有 true 正如 lycanian 所指出的那样.通过将其设置为 true ,它不会将数据发送到输出流,但会以字符串形式返回.

$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.

$this->load->view(); 需要第二个带数据或空的参数,如 $mesg = $this->load->view(view,data,true);, 否则不行

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

这篇关于如何从codeigniter中的视图发送包含内容的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆