Search code examples
phpswiftmailersmtpclient

How to add header via Swift mailer


I am trying to add header to message sent via swift mailer.

$message = Swift_Message::newInstance($title)
          ->setFrom(array('mail@mail.com' => 'Name'))
          ->setTo(array($email => $email))
          ->setBody($content, 'text/html');

tried this, returns error

 $message-> addTextHeader('List-Unsubscribe', $url_unsub);

This returns does nothing, but does not return error also

$headers = $message->getHeaders();
$headers->addTextHeader('List-Unsubscribe', $url_unsub);      
$result = $mailer->send($message); 

Any idea what to do?


Solution

  • First, change to:

    $message->getHeaders()->addTextHeader('List-Unsubscribe', $url_unsub); 
    

    because you doesn't set/relate your $headers to $message after calling getHeaders().

    Second. Check if $url_unsub contains really proper format for header "List-Unsubscribe". Look e.g. here => http://www.list-unsubscribe.com/