6

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?

Ivan
  • 171
  • 2
  • 12
  • [The documentation is really straight forward](https://swiftmailer.symfony.com/docs/headers.html). What does the error say? Can you check if the headers are actually added(see docs)? – Andrei Sep 29 '17 at 11:05
  • I am using B2B cockpit of SAP.I have to remove headers from SWIFT message for my interface and java code or predefined process available to achieve this. – Anil Kumar Oct 16 '18 at 09:25

1 Answers1

6

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/

voodoo417
  • 11,861
  • 3
  • 36
  • 40
  • 2
    *"because you doesn't set/relate your $headers to $message after calling getHeaders()"* How is your one-liner different from the OP's last code block? Whether you assign the object returned by `getHeaders()` to a variable or not, does not change its identity, it's still linked to `$message` if it was in the first place. – BenMorel Oct 16 '19 at 21:11