PHP mail stopped working

Solution 1:

Could it be that E-Mails are being sent fine, but are caught by a spam filter? If this could be, allow me to cross-post myself:


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.

Solution 2:

When you send an email using mail() php hands the data over to the application you configured in sendmail_path, i.e. it spawns a new process for <sendmail_path> and passes some parameters and the email data. This application is supposed to inject the email into the queue of a Mail Transfer Agent (MTA).
The return value of php's mail() function "only" reflects if php was able to spawn that process, stream the data to it and the process exits without an error code. I.e. mail()==true only tells you that the email was (supposedly) injected into the queue of the first MTA on the route.

The MTA then decides what to do with the email. You're probably not working for google and your own MTA is not "within" gmail.com. So your MTA has to send it to the next MTA on the path to gmail.com (forward-path). This may work or not, but mail()===true doesn't tell you anything about that.
Relaying the mail from MTA to MTA may fail on any of the steps. And when the mail finally arrives "at" gmail.com the last MTA or the Mail Delivery Agent (MDA) may also reject it for various reasons.

If an error occurs the "current" MTA may (actually it must, but that's assuming all is configured well ;-)) send back an error report. This error report follows the forward-path but in reverse order (reverse-path) and finally (or "hopefully") the originator receives an "undeliverable mail" email.

(and that's the short version. It's probably inaccurate and I'm neither an admin nor an email/smtp expert ;-))

So...what can you do?

  1. Tell us more about your server. Is it your own (home/test) server? Which operating system. Do you know which "mailing system" was installed (sendmail, qmail, ...)? Who configured it?

  2. Ask on Server Fault how to set up your server's mailing system, how it might try to tell you if something went wrong and how to convince google to accept your emails.

  3. Eliminate the first MTA or more to the point let the php script itself become the first MTA. You can do so by using e.g. Swiftmailer (utilizing its smtp transport module) instead of mail(). This way your server's local mail system doesn't have to work properly. The script will "directly" contact google's SMTP server, authenticate "you" and deliver the mail to google. It still doesn't guarantee the mail will be delivered but it much more likely an error is reported immediately to your script, i.e. if swiftmailer signals "Ok" it's much more likely it really is ok than mail() returning "true".

Solution 3:

The SMTP server handling your mail might be rejecting the message because it claims to come from gmail.com.

Try changing the 'From' field in $headers to an address from your domain.