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 = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\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?
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) :
For german speakers, I have written a quite exhaustive "what to do" on this issue some time ago. See here.
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?
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?
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.
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".
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.
Well, I bet you are using free/commercial server which is managed by some other guys. Unfortunately sometimes the server setting is not correct so that you cannot get the email even you have followed the correct php syntax. Try contacting its customer service center and let them do the diagnostic stuff for you.
This happens to my server for one time (justhost is to blame!) so this might be helpful to you.
check these attributes from your phpinfo(); (if it allows you to see that)
sendmail_from no value no value
sendmail_path /usr/sbin/sendmail -t -i /usr/sbin/sendmail -t -i
Chances are, these attributes are not configured well.
I don't see anything wrong with your php statement:)
edited : you can also upload the mail script written by another language (Perl is the best as most Linux servers have it installed as default). See if you can send email from there. If so, then I assume it is the server configuration error(php.ini problem?), not yours. If the Perl script cannot send email either, well... let guys in the customer service center know and see if you can get your money back:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With