11

How can I check a problem with mail being sent on my server? I run a simple test:

if(mail($to, $subject, $message)) {
echo 'Mail Sent';
}

which the test outputs the text; but, no mail ever arrives.

How can I go about tracking down the issue?

Ben
  • 51,770
  • 36
  • 127
  • 149
Richard Testani
  • 1,474
  • 4
  • 15
  • 29

6 Answers6

13

That is quite a long story. A few bullet points (Assuming that mail() returns true and there are no errors in the error log) :

  • Does the sender address ("From") belong to a domain on your server? If not, make it so.
  • Is your server on a blacklist (e.g. check IP on spamhaus.org)? This is a remote possibility with shared hosting.
  • Are mails filtered by a spam filter? Open an account with a freemailer that has a spam folder and find out. Also, try sending mail to an address without a spam filter.
  • Do you possibly need the fifth parameter "-f" of mail() to add a sender address? (See mail() command in the PHP manual)
  • If you have access to log files, check those, of course, as suggested above.
  • Do you check the "from:" address for possible bounce mails ("Returned to sender")? You can also set up a separate "errors-to" address.

For german speakers, I have written a quite exhaustive "what to do" on this issue some time ago. See here.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
4

Following Myles, if you are on a Linux box, do this on the command line:

# echo “Body text.” | mail -s “Hello world” you@example.com

If you don't receive that email, you have a problem with the mail system on that box. That is a different question from the PHP question you asked.

Ewan Todd
  • 7,315
  • 26
  • 33
  • Hmmm, command not found. echo "Body text." | mail -s "hello world" richtestani@mac.com -bash: mail: command not found – Richard Testani Nov 01 '09 at 21:27
  • I think I'd start investigating sendmail, looking at `ps`, `rpm -qa`, typical sendmail log locations, perhaps the rc.* dirs, and whatever else I could think of. It may be that mail just isn't set up on there? – Ewan Todd Nov 02 '09 at 01:57
  • btw, you might want to remove your first comment here, so that your email address doesn't get harvested any more than it has already. I'll flag it for attention. – Ewan Todd Nov 02 '09 at 02:04
  • Nothing happens. Damn, i hate linux – Sliq May 20 '13 at 20:03
3

From the PHP manual:

Return Values

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

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**.

Not sure how to take that next step, but that's an important point here.

Alex Mcp
  • 19,037
  • 12
  • 60
  • 93
2

If all the troubleshooting fails - now assuming that mail() returns false for reasons unknown - switch to a mailing script like PHPMailer that allows you to bypass mail() altogether and connect directly through SMTP, and offers an extensive debug mode. That way, you should be able to either set up a working solution or find the core of the problem.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I actually tried htmlMimeMail5, which I think also uses SMTP as well but was having the same luck. – Richard Testani Nov 02 '09 at 17:55
  • Does the class have a debug mode? You should get SMTP error messages back without having to check any logs. PHPMailer definitely can. – Pekka Nov 02 '09 at 19:11
1

The first place I'd start is the PHP error log, then your sendmail log. Also try sendmail from the command line and check the PHP configuration to make sure that is setup correctly for sending mail.

Myles
  • 20,860
  • 4
  • 28
  • 37
1

Are you working on a live webserver here, or something more along the lines of a personal development sandbox? (Ie, your home machine?) If it's the latter, I can tell you that I've had a lot of problems in the past with my ISP (Cox) filtering my outbound mail ports. (They'll tell you that they don't do that, but I'm certain they do.)

I've also gotten some outbound messages that did make it through get caught up in the Cox mail spool for days before they were delivered to me.

I hope that's helpful.

Chris Allen Lane
  • 6,334
  • 5
  • 25
  • 31