在代码点火器电子邮件中添加额外的标题信息


Add extra header information in codeigniter email

我想发送一些关于从代码点火器库发送的电子邮件的额外信息。有什么方法可以配置或添加它吗?

我想对我网站上所有发送的邮件进行分类。我需要包括用于跟踪的sendgrid类别标头。

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