Add extra header information in codeigniter email

*爱你&永不变心* 提交于 2019-12-07 03:46:17

问题


I would like to send some extra information on the emails which is sent from codeigniter library. Is there any way to configure or add this?

I want to categorize all the outgoing mail from my site. I need to include sendgrid category header for tracking.


回答1:


The CodeIgniter email class doesn't let you manually set headers. However you could change this by extending it and adding a new function that allows you to set the sendgrid headers.

See the "Extending Native Libraries" section of the CodeIgniter manual:
https://www.codeigniter.com/user_guide/general/creating_libraries.html
Here's what the code in your new email class might look like.

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

You'd then be able to set headers using your new email class like this:

$this->email->set_header($header, $value);

This page will explain what headers can be passed to SendGrid: http://sendgrid.com/docs/API%20Reference/SMTP%20API/




回答2:


Alright, I just want to improve the best answer here. Credit goes to @Tekniskt, and the only difference here is that the settings you might have in /application/config/email.php are ignored, which hurts, especially if you are using custom STMP settings.

Here's the full code of the class MY_Email.php I've improved from the answer above:

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

Hope it helps! :)

I did my test, and it seems now /config/email.php is included and settings are passed properly.

Cheers and thanks for the answer! :)




回答3:


Pass the $config parameter

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

Set custom header as

$this->email->set_header($header, $value);


来源:https://stackoverflow.com/questions/9062192/add-extra-header-information-in-codeigniter-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!