【发布时间】:2012-01-30 10:06:22
【问题描述】:
我想发送一些关于从 codeigniter 库发送的电子邮件的额外信息。有没有办法配置或添加这个?
我想对来自我网站的所有外发邮件进行分类。我需要包含 sendgrid 类别标头以进行跟踪。
【问题讨论】:
标签: php email codeigniter html-email
我想发送一些关于从 codeigniter 库发送的电子邮件的额外信息。有没有办法配置或添加这个?
我想对来自我网站的所有外发邮件进行分类。我需要包含 sendgrid 类别标头以进行跟踪。
【问题讨论】:
标签: php email codeigniter html-email
CodeIgniter 电子邮件类不允许您手动设置标题。但是,您可以通过扩展它并添加一个允许您设置 sendgrid 标头的新功能来更改它。
参见 CodeIgniter 手册的“扩展原生库”部分:
https://www.codeigniter.com/user_guide/general/creating_libraries.html
以下是您的新电子邮件类中的代码可能的样子。
class MY_Email extends CI_Email {
public function __construct(array $config = array())
{
parent::__construct($config);
}
public function set_header($header, $value){
$this->_headers[$header] = $value;
}
}
然后您就可以像这样使用新的电子邮件类设置标题:
$this->email->set_header($header, $value);
此页面将解释哪些标头可以传递给 SendGrid: http://sendgrid.com/docs/API%20Reference/SMTP%20API/
【讨论】:
好的,我只想在这里改进最佳答案。 归功于@Tekniskt,这里唯一的区别是您可能在 /application/config/email.php 中的设置被忽略了,这很痛苦,尤其是在您使用自定义 STMP 设置时。
这是我从上面的答案中改进的 MY_Email.php 类的完整代码:
class MY_Email extends CI_Email {
public function __construct($config = array())
{
if (count($config) > 0)
{
$this->initialize($config);
}
else
{
$this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
$this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
}
log_message('debug', "Email Class Initialized");
}
// this will allow us to add headers whenever we need them
public function set_header($header, $value){
$this->_headers[$header] = $value;
}
}
希望对您有所帮助! :)
我进行了测试,现在似乎包含 /config/email.php 并且设置已正确传递。
干杯并感谢您的回答! :)
【讨论】:
传递$config参数
class MY_Email extends CI_Email
{
public function __construct(array $config = array())
{
parent::__construct($config);
}
public function set_header($header, $value)
{
$this->_headers[ $header ] = $value;
}
}
设置自定义标题为
$this->email->set_header($header, $value);
【讨论】: