Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mail() fails, but returns true

Tags:

php

sendmail

I am trying to use the php mail() function on my server. Weirdly, it returns true but I do not receive anything in my email inbox.

Yet the cpanel email forwarder is working fine.

So prolly it's not a configuration thing since the forwarder sends me emails?

I tried adding in:

ini_set("sendmail_from", "[email protected]");

But that didn't work.

Here's my code:

$subject = "My Subject";
$body = "Email Body ";
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

if (mail($email,$subject,$body,$headers))
    echo "Sent!";
else
    echo "Fail!";
like image 452
John Avatar asked May 27 '11 19:05

John


People also ask

What is return value mail () in PHP?

Return Value: Returns the hash value of the address parameter, or FALSE on failure. Note: Keep in mind that even if the email was accepted for delivery, it does NOT mean the email is actually sent and received!


1 Answers

There are a myriad of reasons that could cause this problem. Here are a few:

  1. The mail was received, but marked as spam.
  2. The recipient's address was incorrect.
  3. There is a slow mail queue on the sending mail server.
  4. There is a slow mail queue on the receiving mail server.

mail() returns true when the outgoing mail server accepts the message for delivery. You will need to troubleshoot the other possibilities to find the point of failure.

like image 145
George Cummins Avatar answered Sep 20 '22 20:09

George Cummins