Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
640 views
in Technique[技术] by (71.8m points)

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      = '[email protected]';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: [email protected]' . "
" .
    'Reply-To: [email protected]' . "
" .
    '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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...