I have encountered a mysterious error that I can't explain or understand. When trying to use PHP's mail()
function with yahoo-addresses it returns failure and the mail isn't sent or delivered.
The code below tries to send the same message to three different recipients - @gmail.com, @outlook.com, @yahoo.se. Mails sent to gmail or outlook always works, but for yahoo-recipients it often fails for some reason.
<?php
$subject = "An email from me";
$from = "Just Me <[email protected]>";
$noreply = "Dont reply <[email protected]>";
$phpver = phpversion();
$body = <<<EOD
<html>
<head></head>
<body>
<div>Hello there! How are you?</div>
</body>
</html>
EOD;
$headers = array(
"From" => $from,
"Reply-To" => $noreply,
"Return-path" => $noreply,
"Organisation" => "Just Me",
"X-priority" => "3",
"X-Mailer" => "PHP/{$phpver}",
"MIME-Version" => "1.0",
"Content-Type" => "text/html; charset=utf-8"
);
// Send an email to three recipients...
$recipients = array("[email protected]", "[email protected]", "[email protected]");
foreach ($recipients as $recipient) {
$res = mail($recipient, $subject, $body, $headers);
if (!$res) {
echo "<div>Failed sending to '$recipient'</div>";
} else {
echo "<div>Sent mail to '$recipient'</div>";
}
}
?>
Output is (sometimes):
Sent mail to '[email protected]'
Sent mail to '[email protected]'
Failed sending to '[email protected]'
Why is it failing only for yahoo recipients? The failure indicates that something fails within the mail()
function before even delivering it.
NB. The recipients exists, but the addresses have been changed for illustration