Postfix and setting Return-path in PHP

Zveřejněno autor   avatar

I noticed recently that some mails from our server have been marked as spam. I’m using Zend_Mail whenever I can for sending mail in PHP. For example this little email.

$mail = new Zend_Mail();
$mail->setSubject('Hello World')
->setBodyText('This mail is going to change your life.')
->addTo('vaclav.krajnak [at] clipsan [dot] com', 'Vaclav Krajnak')
->setFrom('info [at] clipsan [dot] com', 'Clipsan')
->setReturnPath('bounce [at] clipsan [dot] com')
->setReplyTo('info [at] clipsan [dot] com', 'Clipsan')
->send();

Mails from one server were ok, from other not. I found out that the ones that have been send via Postfix causing trouble. I found out that the Return path is ignored by Postfix and replaced with www-data [at] clipsan [dot] com where clipsan.com is in /etc/mailname. Markus has written a post PHP und Postfix: Return-path setzen how you can fix this using common mail() function of PHP.

mail("vaclav.krajnak [at] clipsan [dot] com", "Hello World", "This mail is going to change your life.", "From: info [at] clipsan [dot] com", "-fbounce [at] clipsan [dot] com");

Yeah but I hate to work with the mail() function directly:) So I went deep into Zend Framework and searched the class Zend_Mail_Transport_Sendmail. It passes the parameters given in constructor as the fifth parameter to the mail() function it calls. That’s what we need. I have to create the Transport object by myself and not leave it to Zend Framework to handle it.

$transport = new Zend_Mail_Transport_Sendmail("-fbounce [at] clipsan [dot] com");

Of course we don’t want to prevent any other Transport that is used when sending, so I set the Default Transport instead of passing $tranport directly to send() method. The final example is this:

$mail = new Zend_Mail();
$mail->setDefaultTransport(new Zend_Mail_Transport_Sendmail("-fbounce [at] clipsan [dot] com"));
$mail->setSubject('Hello World')
->setBodyText('This mail is going to change your life.')
->addTo('vaclav.krajnak [at] clipsan [dot] com', 'Vaclav Krajnak')
->setFrom('info [at] clipsan [dot] com', 'Clipsan')
->setReturnPath('bounce [at] clipsan [dot] com')
->setReplyTo('info [at] clipsan [dot] com', 'Clipsan')
->send();

Please notice that the setDefaultTransport method does not support method chaining.

Hope this helps, happy mailing…