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

Gedanken bezüglich der massiven Anzahl an "die NSA kann alles knacken"-Nachrichten

In den letzten Tagen wird eine immer größer werdende Anzahl an Nachrichten durch die digitalen (und wohl auch analogen) Kanäle geprügelt, die alle den gleichen O-Ton tragen. "Die NSA kann alles knacken und hat überall Backdoors eingebaut". Ich habe das Gefühl, dass man hier vor allem Resignation erzeugen möchte. Betrachtet man die Kommentare auf einigen Seiten, trifft man häufig Sätze wie "es bringt alles nichts" an. Mich deucht, dass dies der eigentliche Grund hinter dieser Torpedierung an Meldungen steckt.

Was einem klar sein sollte ist, dass die NSA und andere Schattenregierungen oder "Vereine", viel Macht in der Wirtschaft besitzen, sei es durch Firmenmitarbeiter die ein zweites Gehalt beziehen und die Forschung und Entwicklung in "die richtigen Bahnen" lenken, oder durch Gesetze und Geheimverträge über die niemand sprechen darf.

Aber wo Schatten ist, ist auch immer Licht. Freie Software kann euch schützen. Natürlich ist alles knackbar, aber auch eine NSA mit ihren Supercomputern braucht im Moment noch etwas Zeit um alles zu knacken. Da stellt sich dann immer die Frage wie wichtig eure Daten sind. Sobald die Quantencomputer von den Geheimdiensten und "little NSA aka elgoog" laufen, kann sich all dies relativieren, aber bis dahin ist noch etwas Zeit.

Was könnt ihr tun?

  • Haltet das Betriebssystem aktuell
  • Nutzte freie Betriebssysteme
  • Nutzt freie Verschlüsslungssoftware
  • Haltet wichtige Daten lokal, ihr braucht nicht alles auf einem Webserver oder in der Cloud
  • Nutzte eigene Zertifikate (solange man das Root Zertifikat hat, kann man alle weiteren Entschüsseln)
  • Speichert wichtige Passwörter nicht im Klartext auf dem PC (dann doch lieber der gute alte Zettel)
  • Verschlüsselt E-Mails, GnuPGP ist erlernbar
  • Nutzte Passwörter mit statischen und dynamischen Anteilen
  • Gebt keine wichtigen Informationen auf Internetseiten (und wenn, dann doch lieber nur wenn ein https davor steht)
  • Wählt die richtigen Parteien
  • Lasst euch nicht einschüchtern

web - FutureMe.org: Write a Letter to the Future

Seems like i discovered this service on December 20, 2005 - at least my received mail from the past said so :-). futureme.org is a service that provides you the ability to write yourself (or a email address you want to add to there email database ;-) ) an email and define the date of sending. Pretty cool - especially when you get an mail in six years :-D.