This time, my task was to create an email in an zend framework 2 environment.
The complicated part was the fact, that I wanted to set the "Return-Path". I did it this way
$headers->addHeader('Return-Path', ' but it always got overwritten.
After a while and a chat with an other technical guy and some readings of some SMTP configuration files and specifications, we found the solution. You have to envelope the mail and the zend framework has a class for this. Following is a generic and general solution for this problem.
Hopefully, this will speed up your problem solving. It took me longer than wished and expected to fix this issue.
<?php //@see: https://artodeto.bazzline.net/archives/835-Creating-an-Email-with-a-return-path-needed-for-bounce-management-that-is-not-overwritten-by-the-SMTP-by-using-the-PHP-Zendframework-2.html //full qualified class names since I wanted to keep the code snippet // as short as possible //begin of parameters $body = new \Zend\Mime\Message(); $bounceEmailAddress = '<unique bounce email address>'; $encoding = 'UTF-8'; $envelope = new \Zend\Mail\Transport\Envelope(); $fromEmailAddress = '<[email protected]>'; $fromName = '<your name>'; $htmlContent = '<p>html content</p>'; $message = new \Zend\Mail\Message(); $options = new \Zend\Mail\Transport\SmtpOptions( array( 'connection_class' => '<login>', 'connection_config' => array( 'password' => '<password>', 'username' => '<user name>' ), 'host' => '<smtp host>', 'name' => '<smtp name>' ) ); $subject = '<subject>'; $textContent = 'text content'; $toEmailAddress = '<[email protected]>'; $toName = '<user name>'; $transporter = new \Zend\Mail\Transport\Smtp(); //end of parameters //begin of text content creation $textPart = new \Zend\Mime\Part($textContent); $textPart->setCharset($encoding ); $textPart->setType(\Zend\Mime\Mime::TYPE_TEXT); $body->addPart($textPart); //end of text content creation //begin of html content creation $htmlPart = new \Zend\Mime\Part($htmlContent); $htmlPart->setCharset($encoding ); $htmlPart->setType(\Zend\Mime\Mime::TYPE_HTML); $body->addPart($htmlPart); //end of html content creation //begin of building and sending the mail $envelope->setFrom($bounceEmailAddress); $envelope->setTo($toEmailAddress); $message->setBody($body); $message->addFrom($fromEmailAddress, $fromName); $message->addReplyTo($bounceEmailAddress); $message->setSubject($subject); $message->addTo($toEmailAddress, $toName ; $message->setEncoding($encoding ); $transporter->setEnvelope($envelope); $transporter->setOptions($options); $transporter->send($message); //end of building and sending the mail
It was strange to do an header dump and seeing the correct "Return-Path", because the smtp is rewriting it on its own.
Following some links I used to fix this problem:
- Simple Mail Transfer Protocol
- Envelope Sender
- Bounce Message
- Header (E-Mail)
- postfix possible to overwrite return path via smtp
- using return path when sending mail through smtp using php
By the way and just to put another simple example into the web. Following an easy way to handle bounce emails (return path) via sendmail.
$bounceEmailAddress = '<unique bounce email address>'; $encoding = 'UTF-8'; $fromEmailAddress = '<[email protected]>'; $fromName = '<your name>'; $subject = '<subject>'; $textContent = 'text content'; $toEmailAddress = '<[email protected]>'; $toName = '<user name>';
$header = 'From: ' . $fromEmailAddress . ' <' . $fromEmailAddress . '>' . "\r\n"; iniset('sendmailfrom', $fromEmailAddress); mail($toEmailAddress, $subject, $textContent, $header, '-f' . $bounceEmailAddress); //-f is the magic trigger to set an return path