John
John

Reputation: 141

mail() fails, but returns true

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!";

Upvotes: 14

Views: 11166

Answers (6)

Juraj
Juraj

Reputation: 1

In my case, changing server settings from IPv6 addresses to IPv4 addresses solved the problem.

Upvotes: 0

Felix Eve
Felix Eve

Reputation: 3864

Check your mail.log after sending a test email:

sudo tail -f /var/log/mail.log

Then you can see the ID assigned to the last email you tried to send. E.g. I see:

Jan 11 23:03:14 vagrant-ubuntu-trusty-64 postfix/pickup[17228]: 69F3441529: uid=33 from=<www-data>

69F3441529 is the unique id assigned to that email. You can then grep the log for all lines that have that ID...

sudo grep 69F3441529 /var/log/mail.log

You should then be able to spot any error message that may be present, and then Google them :)

Upvotes: 1

George Cummins
George Cummins

Reputation: 28936

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.

Upvotes: 6

rAm
rAm

Reputation: 1106

We faced the exact same issue. The solution was in php.ini set the appropriate sendmail_path.

sendmail_path = "/usr/sbin/sendmail -t -i"

Setting the above solved the issue.

Upvotes: 1

Spudley
Spudley

Reputation: 168853

The mail() function very rarely returns anything other than true. It only cares about the fact that it's successfully given the email to the MTA (Mail Transfer Agent - ie the program that actually sends it).

The MTA will only reject an email immediately if it is badly formed. In this case, you'll get an error in PHP. But it can also reject an email or fail to send after it has accepted it from PHP, for a variety of reasons, non of which PHP will have any idea about because it has already received it's true response.

Your MTA will have an error log which you can examine for more information if the error is occurring locally.

Upvotes: 4

Dave Kiss
Dave Kiss

Reputation: 10495

I had this problem with a script that I was using that was sending from and receiving at the same domain. Have you tried sending the email to an email address the resides on a different domain? This might help narrow down the possible issues.

Upvotes: 1

Related Questions