note 117960 deleted from function.mail by cmb
| From: | cmb@php.net | Date: | Sun, 21 Jul 2019 08:54:05 +0000 |
| Subject: | note 117960 deleted from function.mail by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211651@lists.php.net to get a copy of this message | ||
Note Submitter: Alexandre T.
----
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.