Monday 9 December 2019

email - PHP mail stopped working



Some days ago when using mail() I had it working.



But now it doesn't work. And I don't know what the problem is.



$to      = 'testmail@gmail.com';
$subject = 'the subject';
$message = 'hello';

$headers = 'From: sender@gmail.com' . "\r\n" .
'Reply-To: sender@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

$mail_sent = @mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";



It displays "Mail sent".



I haven't touched anything in Apache or this code. I have tested the code in an empty PHP file with the same result. How can I debug this problem?


Answer



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.


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...