开发者

PHP mail() doesn't work [duplicate]

This question already has answers here: PHP mail function doesn't complete sending of e-mail (29 answers) Clos开发者_运维知识库ed 7 years ago.

I want to script a simple registration form with activation mail and so on. But for some reason mail() doesn't send the emails, or my 3 different email accounts (hotmail,gmail,yahoo) don't receive them and therefore don't even put them in the spam folder.

Code:

<?php
    $mailto = 'xxx@example.com';
    $subject = 'the subject';
    $message = 'the message';
    $from = 'system@example.net';
    $header = 'From:'.$from;

    if(mail($mailto,$subject,$message,$header)) {
        echo 'Email on the way';
    }
?>

Everytime it outputs 'Email on the way' so mail() returns true, right? I really don't get it, I've even tried to turn off my little snitch (although I didn't block SMTP).


See this article by Jeff Atwood.

In short: Just because your code has handed the e-mail to a Mail Transfer Agent, it doesn't mean it will be delivered. Yes, mail() returning true means "accepted for delivery" - which means "Looks like an e-mail, I'll try to deliver this", not "It is delivered". Even the manual for mail() says:

It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.

Soooo: check your MTA (is the e-mail sent from your local computer?), try to send to a local address (if the address is local, does it get delivered?), try to send an e-mail from your mail client, using the same settings as your PHP script, try to send to a smaller mail-hoster which allows you tu switch off antispam (is it delivered outside your network?). Also, read that article, and check the points mentioned there.


Maybe your server is not configured to handle mail().

<?php
    print phpinfo();  
?>

and look at sendmail_path


You may need to add correct end of line characters to the Headers. It may be \n or \r\n


Check your phpinfo and/or php.ini for your mail settings and make sure you can send mail with whatever program php is trying to use. The function will succeed if the command executes but doesn't know if the mail actually went out.


Check your mail server's mail log. On Unix-ish systems, it's generally /var/log/maillog. On Windows, who knows, but there should be a log somewhere. If mail is returning TRUE, then whatever mail server it's connecting to has accepted the mail for eventual delivery. After that, mail() is no longer involved in any way and it's up to the SMTP servers to do the actual delivery.

In real world terms, mail() is you walking a letter down the block and dropping it into a mail box. Everything after that is utterly outside of PHP's scope and control.


If this is a linux server it's probably set up to send to the local mail queue. When I had this problem I got it working by adding an MX entry on the DNS server used by the linux servers which pointed to our ISP's mail server.


I had the same problem on Ubuntu and I resolved it following the next tutorial:

http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/

I hope it works for you also.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜