Set the return-path by email sending with ZF3?


#1

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?

Regards,
Alex


#2

You can set any headers on the Zend\Mail\Message class. If you read the documenation on: https://zendframework.github.io/zend-mail/message/intro/#quick-start you can see that you can do the following to set specific headers:

use Zend\Mail\Message;

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

/*
 * Mail headers created:
 * X-API-Key: FOO-BAR-BAZ-BAT
 */
$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:

Sendmail

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

$transport = new SendMailTransport();
$transport->send($message);

Smtp

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'              => '127.0.0.1',
    'connection_class'  => 'login',
    'connection_config' => [
        'username' => 'user',
        'password' => 'pass',
    ],
]);
$transport->setOptions($options);
$transport->send($message);

Reference: https://zendframework.github.io/zend-mail/transport/intro/


#3

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

The test code looks like:

<?php

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');
$dom->loadHTML($html_content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
    $url = $image->getAttribute('src');

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

    $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();
$body->addPart($alternatives_part);
$body->addPart($image);

// Create the email message
$message = new Message();
$message->setEncoding('UTF-8');
$message->setBody($body);
$message->getHeaders()->get('content-type')->setType(Mime::MULTIPART_RELATED);
$message->getHeaders()->addHeaderLine('Return-Path', 'return@example.com');
$message->setFrom('noreply@example.com');
$message->addTo('alex@example.com');
$message->setSubject('Test subject');

$smtpOptions = new SmtpOptions();
$smtpOptions->setHost('mail.example.com')
    ->setConnectionClass('login')
    ->setName('mail.example.com')
    ->setConnectionConfig([
        'username' => 'noreply@example.com',
        'password' => 'verysecretpassword',
        'ssl' => [
            'verify_peer'  => false,
            'verify_peer_name'  => false,
            'allow_self_signed' => true
        ]
    ]);

$transport = new Smtp($smtpOptions);
$transport->send($message);

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”.