note 94446 added to function.mail
| From: | Jeff at osu1 dot php dot net | Date: | Thu, 05 Nov 2009 22:34:03 +0000 |
| Subject: | note 94446 added to function.mail | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162424@lists.php.net to get a copy of this message | ||
I really don't think this is a bug in code or a bug in PHP itself, but I've just spent
about 4 hours debugging the mail() function on my Qmail server, only to finally go into the command
line and run the mail() command and find that Qmail reports an injection error in the headers
section of the mail() function. It looks like this:
=====
[root@mail ~]# php -r "mail('email@domain.com, email2@domain.com', 'CLI
Test', 'This is a CLI Test', 'From: me@domain.com\r\nReply-To:
return@domain.com\r\nX-Mailer: PHP/'.phpversion().'\r\n');"
qmail-inject: fatal: unable to parse this line:
From: me@domain.com\r\nReply-To: return@domain.com\r\nX-Mailer: PHP/5.1.6\r\n
[root@mail ~]#
=====
Therefore, for other people's sanity, make sure that you use double-quotes, not single-quotes
(a.k.a. ticks) when setting up email headers on a Qmail server.
Make the change to the header section as stated and it works:
=====
[root@mail ~]# php -r "mail('me@domain.com, you@domain.com', 'CLI Test',
'This is a CLI Test', \"From: me@domain.com\r\nReply-To:
return@domain.com\r\nX-Mailer: PHP/\".phpversion().\"\r\n\");"
[root@mail ~]#
=====
Here's the actual PHP code. This code works:
<?php
$headers = "From: $fromName <$fromEmail>" . "\r\n" .
"Reply-To: $fromName <$fromEmail>" . "\r\n" .
"Return-Path: return@domain.com" . "\r\n" .
"Content-Type: text/plain; charset=ISO-8859-1; format=flowed;
boundary=\"$mime_boundary\"" . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
"X-Mailer: PHP/" . phpversion() . "\r\n" .
"Organization: Your Organization";
mail($to, $subject, $body, $headers);
?>
Where this code does not:
<?php
$headers = 'From: $fromName <$fromEmail>' . '\r\n' .
'Reply-To: $fromName <$fromEmail>' . '\r\n' .
'Return-Path: return@domain.com' . '\r\n' .
'Content-Type: text/plain; charset=ISO-8859-1; format=flowed;
boundary=\'$mime_boundary\'' . '\r\n' .
'MIME-Version: 1.0' . '\r\n' .
'X-Mailer: PHP/' . phpversion() . '\r\n' .
'Organization: Your Organization';
mail($to, $subject, $body, $headers);
?>
The only change is the single versus double quotes. Once I switched it to double-quotes, all emails
are received.
This was difficult to debug because Sendmail and PHP were returning 'FALSE' on the mail()
function due to the Qmail injection failure that I mentioned above and the mail() function
doesn't seem to have much debugging assitance.
Hopefully that save someone else 4 hours that it cost me. :(
----
Server IP: 208.69.120.35
Probable Submitter: 64.140.176.30
----
Manual Page -- http://www.php.net/manual/en/function.mail.php
Edit -- https://master.php.net/note/edit/94446
Del: integrated -- https://master.php.net/note/delete/94446/integrated
Del: useless -- https://master.php.net/note/delete/94446/useless
Del: bad code -- https://master.php.net/note/delete/94446/bad+code
Del: spam -- https://master.php.net/note/delete/94446/spam
Del: non-english -- https://master.php.net/note/delete/94446/non-english
Del: in docs -- https://master.php.net/note/delete/94446/in+docs
Del: other reasons-- https://master.php.net/note/delete/94446
Reject -- https://master.php.net/note/reject/94446
Search -- https://master.php.net/manage/user-notes.php