Sep 17, 2009

PHP: mail() Sender domain must exist - UPDATE

Regarding my last post, it seems that you can't set the Return-Path in the header parameter of the mail() function. The workaround is in the fifth argument of mail() - the additional parameters. If you set the "[email protected]" it should set the Return-Path. Here's my mail() wrapper function:
function construct_mail($to,$subject,$content,$sender='') {
$from = ADMIN_EMAIL;
if(!empty($sender)) $from = $sender;
$header  = "From: $from\r\n";
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Date: ".date("r")."\r\n";
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "X-Mailer: PHP\r\n";
$content = ''.$content.'';
$subject = ' =?UTF-8?B?'. base64_encode($subject) ."?=";
return mail($to,$subject,$content,$header,"-f$from");
}
Also, the -f switch might trigger a E_WARNING if you don't set the trusted users (the user that executes the script - webserver) in the /etc/mail/trusted-users

UPDATE: http://dev.kafol.net/2013/01/sendmail-x-authentication-warning-user.html

PHP: mail() Sender domain must exist

Lately I've found out that a lot of emails sent from PHP's mail() get bounced (they weren't before). Servers bounce the message with the "Sender domain must exist" error, or more precisely, the domain in question was the servers (the server that the script runs on, not the email server) hostname. Well of course that domain doesn't exist (localhost.localdomain), so after some googling, I figured that some additional headers might help, like Return-Path and Reply-To.
$from = "[email protected]";
$header  = "From: $from\r\n";
$header .= "Content-Type: text/html; charset=utf-8\r\n";
$header .= "Date: ".date("r")."\r\n";
$header .= "Reply-To: $from\r\n";
$header .= "Return-Path: $from\r\n";
$header .= "X-Mailer: PHP\r\n";
I'll test it now, when this post reaches my newsletter subscribers.

UPDATE: http://dev.kafol.net/2009/09/php-mail-sender-domain-must-exist.html

UPDATE #2: http://dev.kafol.net/2013/01/sendmail-x-authentication-warning-user.html