Text Headers

Text headers are the simplest type of Header. They contain textual information with no special information included within it – for example the Subject header in a message.

There's nothing particularly interesting about a text header, though it is probably the one you'd opt to use if you need to add a custom header to a message. It represents text just like you'd think it does. If the text contains characters that are not permitted in a message header (such as new lines, or non-ascii characters) then the header takes care of encoding the text so that it can be used.

No header – including text headers – in Swift Mailer is vulnerable to header-injection attacks. Swift Mailer breaks any attempt at header injection by encoding the dangerous data into a non-dangerous form.

It's easy to add a new text header to a HeaderSet. You do this by calling the HeaderSet's addTextHeader() method.

<?php

$message = Swift_Message::newInstance();

$headers = $message->getHeaders();

$headers->addTextHeader('Your-Header-Name', 'the header value');

Changing the value of an existing text header is done by calling it's setValue() method.

<?php

$subject = $message->getHeaders()->getHeader('Subject');

$subject->setValue('new subject');

When output via toString(), a text header produces something like the following:

<?php

$subject = $message->getHeaders()->getHeader('Subject');

$subject->setValue('amazing subject line');

echo $subject->toString();

/*

Subject: amazing subject line

*/

If the header contains any characters that are outside of the US-ASCII range however, they will be encoded. This is nothing to be concerned about since mail clients will decode them back.

<?php

$subject = $message->getHeaders()->getHeader('Subject');

$subject->setValue('contains – dash');

echo $subject->toString();

/*

Subject: contains =?utf-8?Q?=E2=80=93?= dash

*/

NOTE: Documentation for version 3 is in the wiki.