Skip to Content

Return-Path Header For Mail Coming From PHP's mail() Function

Posted in

By default, mail sent using the PHP mail() function will contain an envelope sender address consisting of the server's fully qualified domain name (FQDN). This can cause mail originating from apps like Drupal or MediaWiki that use PHP mail() for notifications to be filtered by mail servers rejecting sender domains not having an MX record.

Perhaps you are aware of the 'sendmail_from' PHP configuration directive and have tried to remedy the problem with this parameter. Though your nix server will recognize 'sendmail_from', it is completely ignored by PHP. Apparently, it is recognized by Windows, but I won't be talking about that here.

To fix, use the -f flag in the 'sendmail_path' directive in php.ini.

Get the current sendmail_path:
php -r 'phpinfo();' | grep sendmail_path

Edit php.ini:
sendmail_path = "/usr/sbin/sendmail -t -i -fnoreply@domain.tld"

Edit mailtest.php:
<?php
$to = 'noreply@domain.tld';
$subject = 'the subject';
$message = 'hello';
$headers =
'From: noreply@domain.tld' . "\r\n" .
'Reply-To: noreply@domain.tld' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo "mail sent\r\n";
?>

Run mailtest.php:
php mailtest.php
mail sent

Look at the Return-Path header in your raw message source:
Return-Path: noreply@domain.tld
Received: from mail.domain.tld (LHLO mail.domain.tld) (68.180.206.184) by
mail.domain.tld with LMTP; Thu, 25 Sep 2008 00:51:34 -0400 (EDT)
Received: from localhost (localhost [127.0.0.1])
by mail.domain.tld (Postfix) with ESMTP id 1F22A146778E
for noreply@domain.tld; Thu, 25 Sep 2008 00:51:34 -0400 (EDT)
Received: from mail.domain.tld ([127.0.0.1])
by localhost (mail.domain.tld [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id EodAWxGObTrv for user@domain.tld;
Thu, 25 Sep 2008 00:51:33 -0400 (EDT)
Received: from dev.domain.tld (dev.domain.tld [64.233.187.99])
by mail.domain.tld (Postfix) with ESMTP id 239BC1467789
for user@domain.tld; Thu, 25 Sep 2008 00:51:33 -0400 (EDT)
Received: from dev.domain.tld (localhost.localdomain [127.0.0.1])
by dev.domain.tld (8.13.1/8.13.1) with ESMTP id m7NJ3IFo001070
for user@domain.tld; Sat, 23 Aug 2008 15:03:18 -0400
Received: (from root@localhost)
by dev.domain.tld (8.13.1/8.13.1/Submit) id m7NJ3IdY001069;
Sat, 23 Aug 2008 15:03:18 -0400
Date: Sat, 23 Aug 2008 15:03:18 -0400
Message-Id: <200808231903.m7NJ3IdY001069@dev.domain.tld>
To: user@domain.tld
Subject: the subject
From: noreply@domain.tld
Reply-To: noreply@domain.tld
X-Mailer: PHP/5.2.5


hello