Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Troubleshooting PHP Mail [duplicate]

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?

like image 638
Richard Testani Avatar asked Nov 01 '09 19:11

Richard Testani


People also ask

How do I fix PHP email?

If it's still not working: change the sender ($sender) to a local email (use the same email as used for recipient). Upload the modified php file and retry. Contact your provider if it still does not work. Tell your provider that the standard php "mail()" function returns TRUE, but not mail will be sent.

How do I prevent mails sent through PHP mail () from going to spam?

Try changing your headers to this: $headers = "MIME-Version: 1.0" . "\r\n"; $headers . = "Content-type: text/html; charset=iso-8859-1" .

Why is my PHP mail function not working?

Make sure the localhost mail server is configuredWithout one, PHP cannot send mail by default. You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail. You can also use SMTP to send your emails.

What SMTP does PHP mail use?

On a *nix machine, the PHP mail() function does not support SMTP, but instead uses the sendmail() or other configured mail script on the server.


2 Answers

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.

like image 183
Pekka Avatar answered Sep 22 '22 09:09

Pekka


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.

like image 20
Ewan Todd Avatar answered Sep 19 '22 09:09

Ewan Todd