0

I need to provide an email form on my website that'll email members. I want to be able to set custom mail headers before the email is sent (I'm using Mandrill, so custom headers allow me additional features). I don't see anything in the docs about custom mail headers: https://ellislab.com/expressionengine/user-guide/development/usage/email.html

Is this possible to do in EE?

UPDATE 1:

I created an extension. And looking at this: https://ellislab.com/expressionengine/user-guide/development/extension_hooks/global/email/index.html -- it seems I should be able to set the headers by manipulating $data['headers']. I have this in my extension:

function email_send(&$data)
{       
    $data['subject']        = 'Hello World';
    $data['headers']['foo'] = 'bar';
}

When I receive the email, the subject is "Hello World". However, when I look at the headers, I do not see foo => bar in it. I've checked with my host to see if there is anything that the hosting might be doing to strip custom headers. They claim no. So, the next question: is there anything in EE that might be preventing me from setting custom headers?

I'm currently configured to use PHP Mail, but I do want to use SMTP for this eventually.

UPDATE 2:

When I configure CPanel to use either Sendmail or SMTP, the subject does not get set to "Hello World". Instead, it says "Contact Form Email". Summary:

  1. Subject only gets set when in PHP Mail
  2. Email headers not set in PHP Mail, Sendmail, or SMTP

2 Answers 2

2
ee()->email->set_header($header, $value);

This isn't documented anywhere, but I see this function in CodeIgniter's Email library (which EE Email library extends), so I assume it should work fine.

1

The answer lies in CodeIgniter's Email library. You'll see that in many places the header_str is used, not the whole headers array. So you'd have to add to the header_str instead.

As for sending with other methods, you'll see that the subject is never used with SMTP or Sendmail - instead those assume that the subject is already in the header_str.

Shameless plug: my Escort add-on allows you to easily send email with Mandrill (and other services) and offers a simple hook to add any data you want to your email before it gets sent (see escort_pre_send extension hook). E.g.,

function escort_pre_send($service, $data)
{
    if($service == 'mandrill')
    {
        $data['headers']['foo'] = 'bar';
    }
    return $data;
}
3
  • I'm actually trying to integrate with Mandrill! Let me digest what you wrote here and take a look at your add-on. Thanks a million! Apr 19, 2015 at 22:11
  • Hi Derek, I purchased your addon and have installed it. I have verified in the headers that the addon is indeed being used. How do I set custom headers? Do you have documentation I can reference? Thanks! Apr 27, 2015 at 19:41
  • I just added a code example to my answer. Apr 28, 2015 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.