Set the return-path by email sending with ZF3?


Hello. I spend whole day with searching for solution how to set the return-path header by sending email with ZF3.
I send emails through SMTP. I found no information about, so I think maybe its only possible to set it by using sendmail? So its not possible to set the return-path while using SMTP?



You can set any headers on the Zend\Mail\Message class. If you read the documenation on: you can see that you can do the following to set specific headers:

use Zend\Mail\Message;

$message = new Message();
$message->addFrom('', 'Matthew Somelli');
$message->setSubject('Sending an email from Zend\Mail!');
$message->setBody('This is the message body.');

 * Mail headers created:
$message->getHeaders()->addHeaderLine('X-API-Key', 'FOO-BAR-BAZ-BAT');

And you can use the different type of Transport adapters. So you can use on of the following transport adapters:


use Zend\Mail\Message;
use Zend\Mail\Transport\Sendmail as SendmailTransport;

$transport = new SendMailTransport();


use Zend\Mail\Transport\Smtp as SmtpTransport;
use Zend\Mail\Transport\SmtpOptions;

// Setup SMTP transport using LOGIN authentication
$transport = new SmtpTransport();
$options   = new SmtpOptions([
    'name'              => 'localhost.localdomain',
    'host'              => '',
    'connection_class'  => 'login',
    'connection_config' => [
        'username' => 'user',
        'password' => 'pass',



I saw all this postings before and ducumentation already, but it didn’t helped me.

The test code looks like:


use Zend\Mail\Message;
use Zend\Mail\Transport\Smtp;
use Zend\Mail\Transport\SmtpOptions;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Mime;
use Zend\Mime\Part as MimePart;

define('ROOT', dirname(__FILE__) . '/../../');

require_once(ROOT . 'include/bootstrap.php');

$html_content = file_get_contents('template.html');

$dom = new DOMDocument(null, 'UTF-8');
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
    $url = $image->getAttribute('src');

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $url);

    $image = new MimePart(fopen($url, 'r'));
    $image->id = md5($url);
    $image->encoding = Mime::ENCODING_BASE64;
    $image->type = $mime_type;

    $html_content = str_replace($url, 'cid:' . md5($url), $html_content);

// HTML message
$html = new MimePart($html_content);
$html->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$html->type = Mime::TYPE_HTML;
$html->charset = 'UTF-8';
$html->disposition = Mime::DISPOSITION_INLINE;

// Plain text message
$text = new MimePart('Textmessage');
$text->encoding = Mime::ENCODING_QUOTEDPRINTABLE;
$text->type = Mime::TYPE_TEXT;
$text->charset = 'UTF-8';
$text->disposition = Mime::DISPOSITION_INLINE;

// Assemble them into a multipart/alternative object
$alternatives = new MimeMessage();
$alternatives->setParts([$text, $html]);
$alternatives_part = new MimePart($alternatives->generateMessage());
$alternatives_part->type = sprintf('multipart/alternative; boundary="%s"', $alternatives->getMime()->boundary());

// Now add the multipart/alternative object to a multipart/related object
$body = new MimeMessage();

// Create the email message
$message = new Message();
$message->getHeaders()->addHeaderLine('Return-Path', '');
$message->setSubject('Test subject');

$smtpOptions = new SmtpOptions();
        'username' => '',
        'password' => 'verysecretpassword',
        'ssl' => [
            'verify_peer'  => false,
            'verify_peer_name'  => false,
            'allow_self_signed' => true

$transport = new Smtp($smtpOptions);

echo 'done!' . "\n";

But the “reserved”(?) header “Return-Path” want be setted / overwritten. The Return-Path in the imcoming email is the sender address. Custom header I can set, but not “Return-Path”.