note 117960 added to function.mail

From: Date: Wed, 09 Sep 2015 08:19:07 +0000
Subject: note 117960 added to function.mail
Groups: php.notes 
Request: Send a blank email to php-notes+get-203749@lists.php.net to get a copy of this message
For those who, just like me, was wondering what happens if you overwrite the "To" and "Subject" parameters with corresponding headers, here's your answer: At least in unix environment (tested with CentOS6 + Exim), is that your custom headers are appended to email headers without any parsing. The final result is: <?php $headers = "To: header@ccc.com\r\n"; $headers .= "Subject: Subject as a header\r\n"; $headers .= "From: noreply@server.com"; mail("parameter@aaa.com", "Subject as a parameter", "Sample message", $headers); /* Output: To: parameter@aaa.com Subject: Subject as a parameter To: header@ccc.com Subject: Subject as a header From: noreply@server.com Sample message. */ ?> As you can see, both headers are used. Same thing happens when you use "" or FALSE as parameter, headers will still be duplicated: <?php mail(false, false, "Sample message", $headers); /* Output: To: Subject: To: header@ccc.com Subject: Subject as a header From: noreply@server.com */ ?> So, you SHOULD NEVER add custom "To" or "Subject" headers in your $headers parameter, as they will be placed AFTER the ones that you informed before in the MAIL() function parameters, very likely resulting in unexpected behaviours. Behaviours known so far (tests using Exim / GMail): - Exim read all "To:" headers, and send message to all of them; - If you use $to parameter and add "To: {$to}" header, recipient will get two copies of the message; - You can leave $to parameter empty, if you don't want any visible recipients (then, use only Bcc). However, anything "non-empty" (like $to="undisclosed-recipients") will be treated as a recipient, and your server will waste time and resources trying to send it and bouncing it when that delivery fails. - Gmail only consider the first "Subject" header. ---- Server IP: 72.52.91.14 Probable Submitter: 2804:7f4:6080:6c55::1 ---- Manual Page -- http://php.net/manual/en/function.mail.php Edit -- https://master.php.net/note/edit/117960 Del: integrated -- https://master.php.net/note/delete/117960/integrated Del: useless -- https://master.php.net/note/delete/117960/useless Del: bad code -- https://master.php.net/note/delete/117960/bad+code Del: spam -- https://master.php.net/note/delete/117960/spam Del: non-english -- https://master.php.net/note/delete/117960/non-english Del: in docs -- https://master.php.net/note/delete/117960/in+docs Del: other reasons-- https://master.php.net/note/delete/117960 Reject -- https://master.php.net/note/reject/117960 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#203749) next »