14

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", "do-not-reply@gmail.com");

But that didn't work.

Here's my code:

$subject = "My Subject";
$body = "Email Body ";
$headers = 'From: do-not-reply@domain.com' . "\r\n" . 
    'Reply-To: do-not-reply@domain.com' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

if (mail($email,$subject,$body,$headers))
    echo "Sent!";
else
    echo "Fail!";
John
  • 141
  • 1
  • 1
  • 3
  • 6
    One thing to note: "It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination." – onteria_ May 27 '11 at 19:37
  • 2
    Check your SMTP server's mail log. It'll explain why the message is not being delivered. – Marc B May 27 '11 at 19:46
  • I had an issue with goDaddy shared hosting when some of my emails for some reason were listed as spam. I have no idea how they generate that list, but it's complete and utter bs. Your server might have the same issue. read up here: http://community.godaddy.com/groups/email/forum/topic/not-receiving-all-of-my-e-mails/ - maybe you have several issue with the email you're trying to send message from. "the most common is the possibility that the sending mailserver was sending from an IP address with a poor mail reputation from public mailing lists such as senderbase.org" – AR. May 27 '11 at 19:51

6 Answers6

6

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.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
4

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.

Spudley
  • 166,037
  • 39
  • 233
  • 307
1

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.

rAm
  • 1,086
  • 2
  • 16
  • 23
1

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 :)

Felix Eve
  • 3,811
  • 3
  • 40
  • 50
1

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.

Dave Kiss
  • 10,289
  • 11
  • 53
  • 75
0

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

Juraj
  • 1