views:

658

answers:

6

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? Thanks Rich

A: 

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

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
+7  A: 

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
It's probably http://spamhaus.org/
MiseryIndex
Corrected. thanks.
Pekka
Not listed on spamhaus.org (phew!)The sender domain does belong to the server.I've tried to send to a number of email accounts all which don;t receive it. (.Mac, gmail, yahoo)I'll go through the log files.Any chance you know where mail logs would reside?Thanks
Richard Testani
What kind of machine / system / platform are you on?
Pekka
Apache/2.2.3 (Debian) PHP/5.2.0-8+etch13
Richard Testani
+1  A: 

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

# echo “Body text.” | mail -s “Hello world” [email protected]

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
Hmmm, command not found.echo "Body text." | mail -s "hello world" [email protected]: mail: command not found
Richard Testani
How about `which mail`
Ewan Todd
odd - nothing returned.
Richard Testani
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
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
A: 

Hi!

I have similar problem. I tried almost everything. If I send mail using command line on my linux box I get it with no trouble (sendmail or mail doesn't matter). But when I try to send it with mail() function I allways get FALSE.

Here is my config from php.ini:

sendmail_from = [email protected] (real domain is used here)
sendmail_path = /usr/sbin/sendmail -t -i

Is there any way to turn on extensive logging so I could track the cause of that?

Tone
Won't mail() give you an error message? Maybe with error_reporting(E_ALL);?
Pekka
+1  A: 

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
I actually tried htmlMimeMail5, which I think also uses SMTP as well but was having the same luck.
Richard Testani
Does the class have a debug mode? You should get SMTP error messages back without having to check any logs. PHPMailer definitely can.
Pekka