How to add header via Swift mailer
Asked Answered
B

1

6

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

$message = Swift_Message::newInstance($title)
          ->setFrom(array('[email protected]' => '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?

Berget answered 29/9, 2017 at 11:3 Comment(2)
The documentation is really straight forward. What does the error say? Can you check if the headers are actually added(see docs)?Nananne
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.Waspish
F
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/

Fragmentary answered 29/9, 2017 at 11:37 Comment(1)
"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.Alteration

© 2022 - 2024 — McMap. All rights reserved.