vendor/swiftmailer/doc/headers.rst
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 Message Headers
       
     2 ===============
       
     3 
       
     4 Sometimes you'll want to add your own headers to a message or modify/remove
       
     5 headers that are already present. You work with the message's HeaderSet to do
       
     6 this.
       
     7 
       
     8 Header Basics
       
     9 -------------
       
    10 
       
    11 All MIME entities in Swift Mailer -- including the message itself --
       
    12 store their headers in a single object called a HeaderSet. This HeaderSet is
       
    13 retrieved with the ``getHeaders()`` method.
       
    14 
       
    15 As mentioned in the previous chapter, everything that forms a part of a
       
    16 message in Swift Mailer is a MIME entity that is represented by an instance of
       
    17 ``Swift_Mime_MimeEntity``. This includes -- most notably -- the
       
    18 message object itself, attachments, MIME parts and embedded images. Each of
       
    19 these MIME entities consists of a body and a set of headers that describe the
       
    20 body.
       
    21 
       
    22 For all of the "standard" headers in these MIME entities, such as the
       
    23 ``Content-Type``, there are named methods for working with them,
       
    24 such as ``setContentType()`` and
       
    25 ``getContentType()``. This is because headers are a moderately
       
    26 complex area of the library. Each header has a slightly different required
       
    27 structure that it must meet in order to comply with the standards that govern
       
    28 email (and that are checked by spam blockers etc).
       
    29 
       
    30 You fetch the HeaderSet from a MIME entity like so:
       
    31 
       
    32 .. code-block:: php
       
    33 
       
    34     $message = Swift_Message::newInstance();
       
    35 
       
    36     //Fetch the HeaderSet from a Message object
       
    37     $headers = $message->getHeaders();
       
    38 
       
    39     $attachment = Swift_Attachment::fromPath('document.pdf');
       
    40 
       
    41     //Fetch the HeaderSet from an attachment object
       
    42     $headers = $attachment->getHeaders();
       
    43 
       
    44 The job of the HeaderSet is to contain and manage instances of Header objects.
       
    45 Depending upon the MIME entity the HeaderSet came from, the contents of the
       
    46 HeaderSet will be different, since an attachment for example has a different
       
    47 set of headers to those in a message.
       
    48 
       
    49 You can find out what the HeaderSet contains with a quick loop, dumping out
       
    50 the names of the headers:
       
    51 
       
    52 .. code-block:: php
       
    53 
       
    54     foreach ($headers->getAll() as $header) {
       
    55       printf("%s<br />\n", $header->getFieldName());
       
    56     }
       
    57 
       
    58     /*
       
    59     Content-Transfer-Encoding
       
    60     Content-Type
       
    61     MIME-Version
       
    62     Date
       
    63     Message-ID
       
    64     From
       
    65     Subject
       
    66     To
       
    67     */
       
    68 
       
    69 You can also dump out the rendered HeaderSet by calling its
       
    70 ``toString()`` method:
       
    71 
       
    72 .. code-block:: php
       
    73 
       
    74     echo $headers->toString();
       
    75 
       
    76     /*
       
    77     Message-ID: <1234869991.499a9ee7f1d5e@swift.generated>
       
    78     Date: Tue, 17 Feb 2009 22:26:31 +1100
       
    79     Subject: Awesome subject!
       
    80     From: sender@example.org
       
    81     To: recipient@example.org
       
    82     MIME-Version: 1.0
       
    83     Content-Type: text/plain; charset=utf-8
       
    84     Content-Transfer-Encoding: quoted-printable
       
    85     */
       
    86 
       
    87 Where the complexity comes in is when you want to modify an existing header.
       
    88 This complexity comes from the fact that each header can be of a slightly
       
    89 different type (such as a Date header, or a header that contains email
       
    90 addresses, or a header that has key-value parameters on it!). Each
       
    91 header in the HeaderSet is an instance of ``Swift_Mime_Header``.
       
    92 They all have common functionality, but knowing exactly what type of header
       
    93 you're working with will allow you a little more control.
       
    94 
       
    95 You can determine the type of header by comparing the return value of its
       
    96 ``getFieldType()`` method with the constants
       
    97 ``TYPE_TEXT``, ``TYPE_PARAMETERIZED``,
       
    98 ``TYPE_DATE``, ``TYPE_MAILBOX``,
       
    99 ``TYPE_ID`` and ``TYPE_PATH`` which are defined in
       
   100 ``Swift_Mime_Header``.
       
   101 
       
   102 
       
   103 .. code-block:: php
       
   104 
       
   105     foreach ($headers->getAll() as $header) {
       
   106       switch ($header->getFieldType()) {
       
   107         case Swift_Mime_Header::TYPE_TEXT: $type = 'text';
       
   108           break;
       
   109         case Swift_Mime_Header::TYPE_PARAMETERIZED: $type = 'parameterized';
       
   110           break;
       
   111         case Swift_Mime_Header::TYPE_MAILBOX: $type = 'mailbox';
       
   112           break;
       
   113         case Swift_Mime_Header::TYPE_DATE: $type = 'date';
       
   114           break;
       
   115         case Swift_Mime_Header::TYPE_ID: $type = 'ID';
       
   116           break;
       
   117         case Swift_Mime_Header::TYPE_PATH: $type = 'path';
       
   118           break;
       
   119       }
       
   120       printf("%s: is a %s header<br />\n", $header->getFieldName(), $type);
       
   121     }
       
   122 
       
   123     /*
       
   124     Content-Transfer-Encoding: is a text header
       
   125     Content-Type: is a parameterized header
       
   126     MIME-Version: is a text header
       
   127     Date: is a date header
       
   128     Message-ID: is a ID header
       
   129     From: is a mailbox header
       
   130     Subject: is a text header
       
   131     To: is a mailbox header
       
   132     */
       
   133 
       
   134 Headers can be removed from the set, modified within the set, or added to the
       
   135 set.
       
   136 
       
   137 The following sections show you how to work with the HeaderSet and explain the
       
   138 details of each implementation of ``Swift_Mime_Header`` that may
       
   139 exist within the HeaderSet.
       
   140 
       
   141 Header Types
       
   142 ------------
       
   143 
       
   144 Because all headers are modeled on different data (dates, addresses,
       
   145 text!) there are different types of Header in Swift Mailer. Swift Mailer
       
   146 attempts to categorize all possible MIME headers into more general groups,
       
   147 defined by a small number of classes.
       
   148 
       
   149 Text Headers
       
   150 ~~~~~~~~~~~~
       
   151 
       
   152 Text headers are the simplest type of Header. They contain textual information
       
   153 with no special information included within it -- for example the Subject
       
   154 header in a message.
       
   155 
       
   156 There's nothing particularly interesting about a text header, though it is
       
   157 probably the one you'd opt to use if you need to add a custom header to a
       
   158 message. It represents text just like you'd think it does. If the text
       
   159 contains characters that are not permitted in a message header (such as new
       
   160 lines, or non-ascii characters) then the header takes care of encoding the
       
   161 text so that it can be used.
       
   162 
       
   163 No header -- including text headers -- in Swift Mailer is vulnerable
       
   164 to header-injection attacks. Swift Mailer breaks any attempt at header
       
   165 injection by encoding the dangerous data into a non-dangerous form.
       
   166 
       
   167 It's easy to add a new text header to a HeaderSet. You do this by calling the
       
   168 HeaderSet's ``addTextHeader()`` method.
       
   169 
       
   170 .. code-block:: php
       
   171 
       
   172     $message = Swift_Message::newInstance();
       
   173 
       
   174     $headers = $message->getHeaders();
       
   175 
       
   176     $headers->addTextHeader('Your-Header-Name', 'the header value');
       
   177 
       
   178 Changing the value of an existing text header is done by calling it's
       
   179 ``setValue()`` method.
       
   180 
       
   181 .. code-block:: php
       
   182 
       
   183     $subject = $message->getHeaders()->getHeader('Subject');
       
   184 
       
   185     $subject->setValue('new subject');
       
   186 
       
   187 When output via ``toString()``, a text header produces something
       
   188 like the following:
       
   189 
       
   190 .. code-block:: php
       
   191 
       
   192     $subject = $message->getHeaders()->getHeader('Subject');
       
   193 
       
   194     $subject->setValue('amazing subject line');
       
   195 
       
   196     echo $subject->toString();
       
   197 
       
   198     /*
       
   199 
       
   200     Subject: amazing subject line
       
   201 
       
   202     */
       
   203 
       
   204 If the header contains any characters that are outside of the US-ASCII range
       
   205 however, they will be encoded. This is nothing to be concerned about since
       
   206 mail clients will decode them back.
       
   207 
       
   208 .. code-block:: php
       
   209 
       
   210     $subject = $message->getHeaders()->getHeader('Subject');
       
   211 
       
   212     $subject->setValue('contains – dash');
       
   213 
       
   214     echo $subject->toString();
       
   215 
       
   216     /*
       
   217 
       
   218     Subject: contains =?utf-8?Q?=E2=80=93?= dash
       
   219 
       
   220     */
       
   221 
       
   222 Parameterized Headers
       
   223 ~~~~~~~~~~~~~~~~~~~~~
       
   224 
       
   225 Parameterized headers are text headers that contain key-value parameters
       
   226 following the textual content. The Content-Type header of a message is a
       
   227 parameterized header since it contains charset information after the content
       
   228 type.
       
   229 
       
   230 The parameterized header type is a special type of text header. It extends the
       
   231 text header by allowing additional information to follow it. All of the
       
   232 methods from text headers are available in addition to the methods described
       
   233 here.
       
   234 
       
   235 Adding a parameterized header to a HeaderSet is done by using the
       
   236 ``addParameterizedHeader()`` method which takes a text value like
       
   237 ``addTextHeader()`` but it also accepts an associative array of
       
   238 key-value parameters.
       
   239 
       
   240 .. code-block:: php
       
   241 
       
   242     $message = Swift_Message::newInstance();
       
   243 
       
   244     $headers = $message->getHeaders();
       
   245 
       
   246     $headers->addParameterizedHeader(
       
   247       'Header-Name', 'header value',
       
   248       array('foo' => 'bar')
       
   249       );
       
   250 
       
   251 To change the text value of the header, call it's ``setValue()``
       
   252 method just as you do with text headers.
       
   253 
       
   254 To change the parameters in the header, call the header's
       
   255 ``setParameters()`` method or the ``setParameter()``
       
   256 method (note the pluralization).
       
   257 
       
   258 .. code-block:: php
       
   259 
       
   260     $type = $message->getHeaders()->getHeader('Content-Type');
       
   261 
       
   262     //setParameters() takes an associative array
       
   263     $type->setParameters(array(
       
   264       'name' => 'file.txt',
       
   265       'charset' => 'iso-8859-1'
       
   266       ));
       
   267 
       
   268     //setParameter() takes two args for $key and $value
       
   269     $type->setParameter('charset', 'iso-8859-1');
       
   270 
       
   271 When output via ``toString()``, a parameterized header produces
       
   272 something like the following:
       
   273 
       
   274 .. code-block:: php
       
   275 
       
   276     $type = $message->getHeaders()->getHeader('Content-Type');
       
   277 
       
   278     $type->setValue('text/html');
       
   279     $type->setParameter('charset', 'utf-8');
       
   280 
       
   281     echo $type->toString();
       
   282 
       
   283     /*
       
   284 
       
   285     Content-Type: text/html; charset=utf-8
       
   286 
       
   287     */
       
   288 
       
   289 If the header contains any characters that are outside of the US-ASCII range
       
   290 however, they will be encoded, just like they are for text headers. This is
       
   291 nothing to be concerned about since mail clients will decode them back.
       
   292 Likewise, if the parameters contain any non-ascii characters they will be
       
   293 encoded so that they can be transmitted safely.
       
   294 
       
   295 .. code-block:: php
       
   296 
       
   297     $attachment = Swift_Attachment::newInstance();
       
   298 
       
   299     $disp = $attachment->getHeaders()->getHeader('Content-Disposition');
       
   300 
       
   301     $disp->setValue('attachment');
       
   302     $disp->setParameter('filename', 'report–may.pdf');
       
   303 
       
   304     echo $disp->toString();
       
   305 
       
   306     /*
       
   307 
       
   308     Content-Disposition: attachment; filename*=utf-8''report%E2%80%93may.pdf
       
   309 
       
   310     */
       
   311 
       
   312 Date Headers
       
   313 ~~~~~~~~~~~~
       
   314 
       
   315 Date headers contains an RFC 2822 formatted date (i.e. what PHP's
       
   316 ``date('r')`` returns). They are used anywhere a date or time is
       
   317 needed to be presented as a message header.
       
   318 
       
   319 The data on which a date header is modeled is simply a UNIX timestamp such as that
       
   320 returned by ``time()`` or ``strtotime()``.  The timestamp
       
   321 is used to create a correctly structured RFC 2822 formatted date such as
       
   322 ``Tue, 17 Feb 2009 22:26:31 +1100``.
       
   323 
       
   324 The obvious place this header type is used is in the ``Date:`` header
       
   325 of the message itself.
       
   326 
       
   327 It's easy to add a new date header to a HeaderSet.  You do this by calling
       
   328 the HeaderSet's ``addDateHeader()`` method.
       
   329 
       
   330 .. code-block:: php
       
   331 
       
   332     $message = Swift_Message::newInstance();
       
   333 
       
   334     $headers = $message->getHeaders();
       
   335 
       
   336     $headers->addDateHeader('Your-Header-Name', strtotime('3 days ago'));
       
   337 
       
   338 Changing the value of an existing date header is done by calling it's
       
   339 ``setTimestamp()`` method.
       
   340 
       
   341 .. code-block:: php
       
   342 
       
   343     $date = $message->getHeaders()->getHeader('Date');
       
   344 
       
   345     $date->setTimestamp(time());
       
   346 
       
   347 When output via ``toString()``, a date header produces something
       
   348 like the following:
       
   349 
       
   350 .. code-block:: php
       
   351 
       
   352     $date = $message->getHeaders()->getHeader('Date');
       
   353 
       
   354     echo $date->toString();
       
   355 
       
   356     /*
       
   357 
       
   358     Date: Wed, 18 Feb 2009 13:35:02 +1100
       
   359 
       
   360     */
       
   361 
       
   362 Mailbox (e-mail address) Headers
       
   363 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   364 
       
   365 Mailbox headers contain one or more email addresses, possibly with
       
   366 personalized names attached to them. The data on which they are modeled is
       
   367 represented by an associative array of email addresses and names.
       
   368 
       
   369 Mailbox headers are probably the most complex header type to understand in
       
   370 Swift Mailer because they accept their input as an array which can take
       
   371 various forms, as described in the previous chapter.
       
   372 
       
   373 All of the headers that contain e-mail addresses in a message -- with the
       
   374 exception of ``Return-Path:`` which has a stricter syntax --
       
   375 use this header type. That is, ``To:``, ``From:``
       
   376 etc.
       
   377 
       
   378 You add a new mailbox header to a HeaderSet by calling the HeaderSet's
       
   379 ``addMailboxHeader()`` method.
       
   380 
       
   381 .. code-block:: php
       
   382 
       
   383     $message = Swift_Message::newInstance();
       
   384 
       
   385     $headers = $message->getHeaders();
       
   386 
       
   387     $headers->addMailboxHeader('Your-Header-Name', array(
       
   388       'person1@example.org' => 'Person Name One',
       
   389       'person2@example.org',
       
   390       'person3@example.org',
       
   391       'person4@example.org' => 'Another named person'
       
   392       ));
       
   393 
       
   394 Changing the value of an existing mailbox header is done by calling it's
       
   395 ``setNameAddresses()`` method.
       
   396 
       
   397 .. code-block:: php
       
   398 
       
   399     $to = $message->getHeaders()->getHeader('To');
       
   400 
       
   401     $to->setNameAddresses(array(
       
   402       'joe@example.org' => 'Joe Bloggs',
       
   403       'john@example.org' => 'John Doe',
       
   404       'no-name@example.org'
       
   405       ));
       
   406 
       
   407 If you don't wish to concern yourself with the complicated accepted input
       
   408 formats accepted by ``setNameAddresses()`` as described in the previous chapter
       
   409 and you only want to set one or more addresses (not names) then you can just
       
   410 use the ``setAddresses()`` method instead.
       
   411 
       
   412 .. code-block:: php
       
   413 
       
   414     $to = $message->getHeaders()->getHeader('To');
       
   415 
       
   416     $to->setAddresses(array(
       
   417       'joe@example.org',
       
   418       'john@example.org',
       
   419       'no-name@example.org'
       
   420       ));
       
   421 
       
   422 .. note::
       
   423 
       
   424     Both methods will accept the above input format in practice.
       
   425 
       
   426 If all you want to do is set a single address in the header, you can use a
       
   427 string as the input parameter to ``setAddresses()`` and/or
       
   428 ``setNameAddresses()``.
       
   429 
       
   430 .. code-block:: php
       
   431 
       
   432     $to = $message->getHeaders()->getHeader('To');
       
   433 
       
   434     $to->setAddresses('joe-bloggs@example.org');
       
   435 
       
   436 When output via ``toString()``, a mailbox header produces
       
   437 something like the following:
       
   438 
       
   439 .. code-block:: php
       
   440 
       
   441     $to = $message->getHeaders()->getHeader('To');
       
   442 
       
   443     $to->setNameAddresses(array(
       
   444       'person1@example.org' => 'Name of Person',
       
   445       'person2@example.org',
       
   446       'person3@example.org' => 'Another Person'
       
   447     ));
       
   448 
       
   449     echo $to->toString();
       
   450 
       
   451     /*
       
   452 
       
   453     To: Name of Person <person1@example.org>, person2@example.org, Another Person
       
   454      <person3@example.org>
       
   455 
       
   456     */
       
   457 
       
   458 ID Headers
       
   459 ~~~~~~~~~~
       
   460 
       
   461 ID headers contain identifiers for the entity (or the message). The most
       
   462 notable ID header is the Message-ID header on the message itself.
       
   463 
       
   464 An ID that exists inside an ID header looks more-or-less less like an email
       
   465 address.  For example, ``<![CDATA[<1234955437.499becad62ec2@example.org>]]>``.
       
   466 The part to the left of the @ sign is usually unique, based on the current time and
       
   467 some random factor.  The part on the right is usually a domain name.
       
   468 
       
   469 Any ID passed the an ID header's ``setId()`` method absolutely
       
   470 MUST conform to this structure, otherwise you'll get an Exception thrown at you
       
   471 by Swift Mailer (a ``Swift_RfcComplianceException``).  This is to
       
   472 ensure that the generated email complies with relevant RFC documents and therefore
       
   473 is less likely to be blocked as spam.
       
   474 
       
   475 It's easy to add a new ID header to a HeaderSet.  You do this by calling
       
   476 the HeaderSet's ``addIdHeader()`` method.
       
   477 
       
   478 .. code-block:: php
       
   479 
       
   480     $message = Swift_Message::newInstance();
       
   481 
       
   482     $headers = $message->getHeaders();
       
   483 
       
   484     $headers->addIdHeader('Your-Header-Name', '123456.unqiue@example.org');
       
   485 
       
   486 Changing the value of an existing date header is done by calling its
       
   487 ``setId()`` method.
       
   488 
       
   489 .. code-block:: php
       
   490 
       
   491     $msgId = $message->getHeaders()->getHeader('Message-ID');
       
   492 
       
   493     $msgId->setId(time() . '.' . uniqid('thing') . '@example.org');
       
   494 
       
   495 When output via ``toString()``, an ID header produces something
       
   496 like the following:
       
   497 
       
   498 .. code-block:: php
       
   499 
       
   500     $msgId = $message->getHeaders()->getHeader('Message-ID');
       
   501 
       
   502     echo $msgId->toString();
       
   503 
       
   504     /*
       
   505 
       
   506     Message-ID: <1234955437.499becad62ec2@example.org>
       
   507 
       
   508     */
       
   509 
       
   510 Path Headers
       
   511 ~~~~~~~~~~~~
       
   512 
       
   513 Path headers are like very-restricted mailbox headers. They contain a single
       
   514 email address with no associated name. The Return-Path header of a message is
       
   515 a path header.
       
   516 
       
   517 You add a new path header to a HeaderSet by calling the HeaderSet's
       
   518 ``addPathHeader()`` method.
       
   519 
       
   520 .. code-block:: php
       
   521 
       
   522     $message = Swift_Message::newInstance();
       
   523 
       
   524     $headers = $message->getHeaders();
       
   525 
       
   526     $headers->addPathHeader('Your-Header-Name', 'person@example.org');
       
   527 
       
   528 
       
   529 Changing the value of an existing path header is done by calling its
       
   530 ``setAddress()`` method.
       
   531 
       
   532 .. code-block:: php
       
   533 
       
   534     $return = $message->getHeaders()->getHeader('Return-Path');
       
   535 
       
   536     $return->setAddress('my-address@example.org');
       
   537 
       
   538 When output via ``toString()``, a path header produces something
       
   539 like the following:
       
   540 
       
   541 .. code-block:: php
       
   542 
       
   543     $return = $message->getHeaders()->getHeader('Return-Path');
       
   544 
       
   545     $return->setAddress('person@example.org');
       
   546 
       
   547     echo $return->toString();
       
   548 
       
   549     /*
       
   550 
       
   551     Return-Path: <person@example.org>
       
   552 
       
   553     */
       
   554 
       
   555 Header Operations
       
   556 -----------------
       
   557 
       
   558 Working with the headers in a message involves knowing how to use the methods
       
   559 on the HeaderSet and on the individual Headers within the HeaderSet.
       
   560 
       
   561 Adding new Headers
       
   562 ~~~~~~~~~~~~~~~~~~
       
   563 
       
   564 New headers can be added to the HeaderSet by using one of the provided
       
   565 ``add..Header()`` methods.
       
   566 
       
   567 To add a header to a MIME entity (such as the message):
       
   568 
       
   569 Get the HeaderSet from the entity by via its ``getHeaders()``
       
   570 method.
       
   571 
       
   572 * Add the header to the HeaderSet by calling one of the ``add..Header()``
       
   573   methods.
       
   574 
       
   575 The added header will appear in the message when it is sent.
       
   576 
       
   577 .. code-block:: php
       
   578 
       
   579     //Adding a custom header to a message
       
   580     $message = Swift_Message::newInstance();
       
   581     $headers = $message->getHeaders();
       
   582     $headers->addTextHeader('X-Mine', 'something here');
       
   583 
       
   584     //Adding a custom header to an attachment
       
   585     $attachment = Swift_Attachment::fromPath('/path/to/doc.pdf');
       
   586     $attachment->getHeaders()->addDateHeader('X-Created-Time', time());
       
   587 
       
   588 Retrieving Headers
       
   589 ~~~~~~~~~~~~~~~~~~
       
   590 
       
   591 Headers are retrieved through the HeaderSet's ``get()`` and
       
   592 ``getAll()`` methods.
       
   593 
       
   594 To get a header, or several headers from a MIME entity:
       
   595 
       
   596 * Get the HeaderSet from the entity by via its ``getHeaders()`` method.
       
   597 
       
   598 * Get the header(s) from the HeaderSet by calling either ``get()`` or
       
   599   ``getAll()``.
       
   600 
       
   601 When using ``get()`` a single header is returned that matches the
       
   602 name (case insensitive) that is passed to it. When using
       
   603 ``getAll()`` with a header name, an array of headers with that
       
   604 name are returned. Calling ``getAll()`` with no arguments returns
       
   605 an array of all headers present in the entity.
       
   606 
       
   607 .. note::
       
   608 
       
   609     It's valid for some headers to appear more than once in a message (e.g.
       
   610     the Received header). For this reason ``getAll()`` exists to fetch all
       
   611     headers with a specified name. In addition, ``get()`` accepts an optional
       
   612     numerical index, starting from zero to specify which header you want more
       
   613     specifically.
       
   614 
       
   615 .. note::
       
   616 
       
   617     If you want to modify the contents of the header and you don't know for
       
   618     sure what type of header it is then you may need to check the type by
       
   619     calling its ``getFieldType()`` method.
       
   620 
       
   621     .. code-block:: php
       
   622 
       
   623         $headers = $message->getHeaders();
       
   624 
       
   625         //Get the To: header
       
   626         $toHeader = $headers->get('To');
       
   627 
       
   628         //Get all headers named "X-Foo"
       
   629         $fooHeaders = $headers->getAll('X-Foo');
       
   630 
       
   631         //Get the second header named "X-Foo"
       
   632         $foo = $headers->get('X-Foo', 1);
       
   633 
       
   634         //Get all headers that are present
       
   635         $all = $headers->getAll();
       
   636 
       
   637 Check if a Header Exists
       
   638 ~~~~~~~~~~~~~~~~~~~~~~~~
       
   639 
       
   640 You can check if a named header is present in a HeaderSet by calling its
       
   641 ``has()`` method.
       
   642 
       
   643 To check if a header exists:
       
   644 
       
   645 * Get the HeaderSet from the entity by via its ``getHeaders()`` method.
       
   646 
       
   647 * Call the HeaderSet's ``has()`` method specifying the header you're looking
       
   648   for.
       
   649 
       
   650 If the header exists, ``true`` will be returned or
       
   651 ``false`` if not.
       
   652 
       
   653 .. note::
       
   654 
       
   655     It's valid for some headers to appear more than once in a message (e.g.
       
   656     the Received header). For this reason ``has()`` accepts an optional
       
   657     numerical index, starting from zero to specify which header you want to
       
   658     check more specifically.
       
   659 
       
   660     .. code-block:: php
       
   661 
       
   662         $headers = $message->getHeaders();
       
   663 
       
   664         //Check if the To: header exists
       
   665         if ($headers->has('To')) {
       
   666           echo 'To: exists';
       
   667         }
       
   668 
       
   669         //Check if an X-Foo header exists twice (i.e. check for the 2nd one)
       
   670         if ($headers->has('X-Foo', 1)) {
       
   671           echo 'Second X-Foo header exists';
       
   672         }
       
   673 
       
   674 Removing Headers
       
   675 ~~~~~~~~~~~~~~~~
       
   676 
       
   677 Removing a Header from the HeaderSet is done by calling the HeaderSet's
       
   678 ``remove()`` or ``removeAll()`` methods.
       
   679 
       
   680 To remove an existing header:
       
   681 
       
   682 * Get the HeaderSet from the entity by via its ``getHeaders()`` method.
       
   683 
       
   684 * Call the HeaderSet's ``remove()`` or ``removeAll()`` methods specifying the
       
   685   header you want to remove.
       
   686 
       
   687 When calling ``remove()`` a single header will be removed. When
       
   688 calling ``removeAll()`` all headers with the given name will be
       
   689 removed. If no headers exist with the given name, no errors will occur.
       
   690 
       
   691 .. note::
       
   692 
       
   693     It's valid for some headers to appear more than once in a message (e.g.
       
   694     the Received header). For this reason ``remove()`` accepts an optional
       
   695     numerical index, starting from zero to specify which header you want to
       
   696     check more specifically. For the same reason, ``removeAll()`` exists to
       
   697     remove all headers that have the given name.
       
   698 
       
   699     .. code-block:: php
       
   700 
       
   701         $headers = $message->getHeaders();
       
   702 
       
   703         //Remove the Subject: header
       
   704         $headers->remove('Subject');
       
   705 
       
   706         //Remove all X-Foo headers
       
   707         $headers->removeAll('X-Foo');
       
   708 
       
   709         //Remove only the second X-Foo header
       
   710         $headers->remove('X-Foo', 1);
       
   711 
       
   712 Modifying a Header's Content
       
   713 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
   714 
       
   715 To change a Header's content you should know what type of header it is and
       
   716 then call it's appropriate setter method. All headers also have a
       
   717 ``setFieldBodyModel()`` method that accepts a mixed parameter and
       
   718 delegates to the correct setter.
       
   719 
       
   720 To modify an existing header:
       
   721 
       
   722 * Get the HeaderSet from the entity by via its ``getHeaders()`` method.
       
   723 
       
   724 * Get the Header by using the HeaderSet's ``get()``.
       
   725 
       
   726 * Call the Header's appropriate setter method or call the header's
       
   727   ``setFieldBodyModel()`` method.
       
   728 
       
   729 The header will be updated inside the HeaderSet and the changes will be seen
       
   730 when the message is sent.
       
   731 
       
   732 .. code-block:: php
       
   733 
       
   734     $headers = $message->getHeaders();
       
   735 
       
   736     //Change the Subject: header
       
   737     $subj = $headers->get('Subject');
       
   738     $subj->setValue('new subject here');
       
   739 
       
   740     //Change the To: header
       
   741     $to = $headers->get('To');
       
   742     $to->setNameAddresses(array(
       
   743       'person@example.org' => 'Person',
       
   744       'thing@example.org'
       
   745     ));
       
   746 
       
   747     //Using the setFieldBodyModel() just delegates to the correct method
       
   748     // So here to calls setNameAddresses()
       
   749     $to->setFieldBodyModel(array(
       
   750       'person@example.org' => 'Person',
       
   751       'thing@example.org'
       
   752     ));