How to add a custom header - Using 4.0.0-b5

Swift Mailer is a fantastic library for sending email with php. Discuss this library or ask any questions about it here.

Moderators: Chris Corbyn, General Moderators

Post Reply
KevinC
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2009 12:01 pm

How to add a custom header - Using 4.0.0-b5

Post by KevinC »

Looking for some help in adding a custom header.
I need to add a custom header similar to v3.3.3 using:
$message->headers->set("name", "value");
and can not find the proper method.

Thanks
Kevin
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to add a custom header - Using 4.0.0-b5

Post by Chris Corbyn »

This is one area that changed drastically. The whole model on which headers are based had to be re-thought out if things were to comply with RFC 2822 (and others) in a nice abstract way.

Code: Select all

$message->getHeaders()->addTextHeader('Name-Of-Header', 'value of header');
 
/* Results...
 
Name-Of-Header: value of header
*/
 
Note that there are other "types" of header you can add such as these:

Code: Select all

$message->getHeaders()->addMailboxHeader('Header-Name', array(
  'address1@domain.tld' => 'Person One',
  'address2@domain.tld' => 'Person Two'
));
 
/* Results...
 
Header-Name: Person One <address1@domain.tld>, Person Two <address2@domain.tld>
*/
And...

Code: Select all

$message->addDateHeader('X-Some-Date', time());
 
/* Results...
 
X-Some-Date: Mon, 16 Feb 2009 08:58:51 GMT
*/
There are more available methods that I'm working on documentation for.
KevinC
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2009 12:01 pm

Re: How to add a custom header - Using 4.0.0-b5

Post by KevinC »

Thanks, That worked great with one exception.
The headers added were not listed in the order they were added.

I am planning on using these items to handle bounce processing I would like to maintain them in a certain order for ease of scripting parse searches.

Let me know if there is a (or will be) mechanism to control the order. Otherwise I'll attack it from the bounce processing side.

Thanks,
Kevin
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to add a custom header - Using 4.0.0-b5

Post by Chris Corbyn »

KevinC wrote:Thanks, That worked great with one exception.
The headers added were not listed in the order they were added.

I am planning on using these items to handle bounce processing I would like to maintain them in a certain order for ease of scripting parse searches.

Let me know if there is a (or will be) mechanism to control the order. Otherwise I'll attack it from the bounce processing side.

Thanks,
Kevin
Mail servers can change the order of the headers, so even if Swift puts them in a particular order you can't guarantee that they'll arrive in that order ;)

There is a way to get Swift to put them in a particular order however... I'll have to dig up the code so I'll post back when I'm a little less busy :) It's something along the lines of:

Code: Select all

$message->getHeaders()->defineOrdering(array(
  'To',
  'Cc',
  'Bcc',
  'Subject',
  'My-Custom-Header',
  'Another-Header',
  'Content-Type',
  'MIME-Version',
  'Message-ID'
));
In fact, I think that is the code

You can check what order Swift puts them in prior to sending by doing:

Code: Select all

echo $message->toString();
If they arrive in a different order it's because an interfering mail server decided it knew you wanted them in a different order :P They think they know best ;)
KevinC
Forum Newbie
Posts: 8
Joined: Sun Feb 15, 2009 12:01 pm

Re: How to add a custom header - Using 4.0.0-b5

Post by KevinC »

Actually I was only worried about the additional header elements that I added using:
$message->getHeaders()->addTextHeader('Name-Of-Header', 'value of header');

Thanks
Kevin
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: How to add a custom header - Using 4.0.0-b5

Post by Chris Corbyn »

KevinC wrote:Actually I was only worried about the additional header elements that I added using:
$message->getHeaders()->addTextHeader('Name-Of-Header', 'value of header');

Thanks
Kevin
The same principle applies. However, using that call that I posted above, with only you new header names will override Swift Mailer's defaults (which includes To: and Subject: header etc). This shouldn't cause any issues, but if it's not desirable we may have modify the interface to allow this to be handled a little better.

My gawd, it's so tricky trying to account for everybody's needs :P
Post Reply