PHP 邮件()不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6403746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:16:34  来源:igfitidea点击:

PHP mail() doesn't work

phpemail

提问by Rivers

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.

我想用激活邮件等编写一个简单的注册表单。但出于某种原因,mail() 不发送电子邮件,或者我的 3 个不同的电子邮件帐户(hotmail、gmail、yahoo)没有收到它们,因此甚至没有将它们放入垃圾邮件文件夹。

Code:

代码:

<?php
    $mailto = '[email protected]';
    $subject = 'the subject';
    $message = 'the message';
    $from = '[email protected]';
    $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).

每次它输出'Email on the way' 所以mail() 返回true,对吗?我真的不明白,我什至试图关闭我的小告密者(虽然我没有阻止 SMTP)。

回答by Piskvor left the building

See this article by Jeff Atwood.

请参阅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 "acceptedfor delivery" - which means "Looks like an e-mail, I'll tryto deliver this", not "It is delivered". Even the manual for mail()says:

简而言之:仅仅因为您的代码已将电子邮件交给邮件传输代理,并不意味着它会被发送。是的,mail()返回 true 意味着“接受交付”——这意味着“看起来像一封电子邮件,我会尝试交付”,而不是“它已交付”。甚至手册上mail()

It is important to note that just because the mail was accepted for delivery, it does NOTmean 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.

Soooo:检查您的 MTA(电子邮件是从您的本地计算机发送的吗?),尝试发送到本地地址(如果地址是本地的,它是否已送达?),尝试从您的邮件发送电子邮件客户端,使用与您的 PHP 脚本相同的设置,尝试发送到一个较小的邮件主机,它允许您关闭反垃圾邮件(它是否传递到您的网络之外?)。另外,阅读那篇文章,并检查那里提到的要点。

回答by DENIEL

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

也许您的服务器未配置为处理mail().

<?php
    print phpinfo();  
?>

and look at sendmail_path

看看 sendmail_path

回答by Brombomb

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

您可能需要向标题添加正确的行尾字符。它可能是\n\r\n

回答by ldg

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.

检查您的 phpinfo 和/或 php.ini 的邮件设置,并确保您可以使用 php 尝试使用的任何程序发送邮件。如果命令执行但不知道邮件是否实际发出,则该函数将成功。

回答by Marc B

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.

检查您的邮件服务器的邮件日志。在 Unix-ish 系统上,它通常是 /var/log/maillog。在 Windows 上,谁知道呢,但应该在某处有一个日志。如果邮件返回 TRUE,那么它所连接的任何邮件服务器都已接受邮件以进行最终递送。之后,mail()不再以任何方式参与,由 SMTP 服务器来进行实际交付。

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.

在现实世界中,mail()您是否将一封信走下街区并将其放入邮箱。之后的一切都完全超出了 PHP 的范围和控制范围。

回答by Brad Mace

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 MXentry on the DNS server used by the linux servers which pointed to our ISP's mail server.

如果这是一个 linux 服务器,它可能设置为发送到本地邮件队列。当我遇到这个问题时,我MX通过在 Linux 服务器使用的 DNS 服务器上添加一个条目来解决它,该条目指向我们 ISP 的邮件服务器。

回答by mgrdiez

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

我在 Ubuntu 上遇到了同样的问题,我按照下一个教程解决了它:

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

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

I hope it works for you also.

我希望它也适用于你。