Skip to content

Creating an Email with a return path (needed for bounce management) that is not overwritten by the SMTP by using the PHP Zendframework 2

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.


<?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   = '<from@your-doma.in>';
$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     = '<your@user-doma.in>';
$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
Hopefully, this will speed up your problem solving. It took me longer than wished and expected to fix this issue.
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:

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   = '<from@your-doma.in>';
$fromName           = '<your name>';
$subject            = '<subject>';
$textContent        = 'text content';
$toEmailAddress     = '<your@user-doma.in>';
$toName             = '<user name>';

$header    = 'From: ' . $fromEmailAddress . ' <' . $fromEmailAddress . '>' . "\r\n";
ini_set('sendmail_from', $fromEmailAddress);
mail($toEmailAddress, $subject, $textContent, $header, '-f' . $bounceEmailAddress); //-f is the magic trigger to set an return path

Trackbacks

Keine Trackbacks

Kommentare

Ansicht der Kommentare: Linear | Verschachtelt

Noch keine Kommentare

Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.

Kommentar schreiben

Die angegebene E-Mail-Adresse wird nicht dargestellt, sondern nur für eventuelle Benachrichtigungen verwendet.
Um einen Kommentar hinterlassen zu können, erhalten Sie nach dem Kommentieren eine E-Mail mit Aktivierungslink an ihre angegebene Adresse.

Um maschinelle und automatische Übertragung von Spamkommentaren zu verhindern, bitte die Zeichenfolge im dargestellten Bild in der Eingabemaske eintragen. Nur wenn die Zeichenfolge richtig eingegeben wurde, kann der Kommentar angenommen werden. Bitte beachten Sie, dass Ihr Browser Cookies unterstützen muss, um dieses Verfahren anzuwenden.
CAPTCHA

Markdown-Formatierung erlaubt
Formular-Optionen