1

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 <info@mydomain.se>";
$noreply  = "Dont reply <noreply@mydomain.se>";
$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("mygmail@gmail.com", "myoutlook@outlook.com", "myyahoo@yahoo.se");
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 'mygmail@gmail.com'
Sent mail to 'myoutlook@outlook.com'
Failed sending to 'myyahoo@yahoo.se'

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

Gowire
  • 1,046
  • 6
  • 27
  • 2
    Here's a good general guide to debugging email problems in PHP: https://stackoverflow.com/a/24644450/5947043 . For a start, you potentially need to check the mailserver logs to see if there's a more detailed description of the failure. – ADyson Feb 10 '21 at 11:25
  • 1
    You may well be better using something like PHPMailer rather than `mail()`. – droopsnoot Feb 10 '21 at 11:31
  • @droopsnoot I would like that, but unfortunately I can't install or use it (i believe) since I have no administrative rights on the web hotel. – Gowire Feb 10 '21 at 12:01
  • 1
    PHPMailer is deployed as a set of PHP files (just like all other PHP code). If you have permission to update the PHP files in your application, and add new ones, then you can use it. – ADyson Feb 10 '21 at 12:12

0 Answers0