codeigniter - 如何在 codeigniter 中发送包含 View 内容的电子邮件

标签 codeigniter php

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

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

工作代码:

$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);应该像 lycanian 指出的那样 true。通过将其设置为 true ,它不会将数据发送到输出流,但会以字符串形式返回。

编辑: $this->load->view();需要第二个带数据的参数或为空,如 $mesg = $this->load->view(view,data,true); ,否则它不会工作

关于codeigniter - 如何在 codeigniter 中发送包含 View 内容的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10663277/

相关文章:

php - Symfony2 并行处理

php - 转义 $_SESSION 用户名以便在 mysql 查询中使用的正确方法是什么?

php - FOSUserBundle - 如何在尝试访问 login_path 时重定向已经登录的用户

php - 想要通过合并两个查询在一个查询中获得两个查询的结果

php - 未定义索引 : in PHP codeigniter while sending variable

php - 如何扩展 CodeIgniter 数据库实用程序类

php - HTML/PHP 表单不向 MySQL 数据库发送数据

php - setcookie() : Passing null to parameter #7 ($httponly) of type bool is deprecated

php - 错误号 : 1048 Column 'btc' cannot be null

php - 如何在每个页面上正确包含 PHP header