PHP mail() doesn't work [duplicate]

2020-02-01 03:23发布

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

标签: php email
7条回答
干净又极端
2楼-- · 2020-02-01 03:53

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.

查看更多
成全新的幸福
3楼-- · 2020-02-01 04:00

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

查看更多
够拽才男人
4楼-- · 2020-02-01 04:01

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.

查看更多
Bombasti
5楼-- · 2020-02-01 04:02

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.

查看更多
贼婆χ
6楼-- · 2020-02-01 04:06

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.

查看更多
一夜七次
7楼-- · 2020-02-01 04:17

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

<?php
    print phpinfo();  
?>

and look at sendmail_path

查看更多
登录 后发表回答