php 函数 mail() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21836282/
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
php function mail() isn't working
提问by user1195450
I used mail() function in php coding but I failed to send any mail. Before proceeding ahead I want to elaborate the context of using the mail() function.
我在 php 编码中使用了 mail() 函数,但我没有发送任何邮件。在继续之前,我想详细说明使用 mail() 函数的上下文。
I didnt host my site so it is on localhost. I did set smtp, port sendmail_path etc.
我没有托管我的网站,所以它在本地主机上。我确实设置了 smtp、端口 sendmail_path 等。
After searching a lot I it seems that I need to download a mail server. I downloaded a sendmail server that is free and configured it as the site suggested. However, all in vain moreover, someone told me that I can't use mail function until I host my site not only on localhost. Please guide me.
经过大量搜索后,我似乎需要下载邮件服务器。我下载了一个免费的 sendmail 服务器,并按照网站的建议进行了配置。然而,这一切都是徒劳的,有人告诉我,除非我不仅在本地主机上托管我的站点,否则我不能使用邮件功能。请指导我。
<?php
$from = "[email protected]"; // sender
$subject = " My cron is working";
$message = "My first Cron :)";
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
ini_set("SMTP","localhost");
ini_set("smtp_port","25");
ini_set("sendmail_from","[email protected]");
ini_set("sendmail_path", "C:\wamp\bin\sendmail.exe -t");
mail("[email protected]",$subject,$message,"From: $from\n");
echo "Thank you for sending us feedback";
?>
this my sendmail configuration file:
这是我的 sendmail 配置文件:
smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
;default_domain=domain.com
[email protected]
auth_password=8888
force_sender=j*****@gmail.com
( ! ) SCREAM: Error suppression ignored for
( ! ) Warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\wamp\www\test.php on line 20
回答by ??q?? zo???
I think you are not configured properly,
我认为你没有正确配置,
if you are using XAMPP then you can easily send mail from localhost.
如果您使用的是 XAMPP,那么您可以轻松地从本地主机发送邮件。
for example you can configure C:\xampp\php\php.iniand c:\xampp\sendmail\sendmail.inifor gmail to send mail.
例如,您可以配置C:\xampp\php\php.ini和c:\xampp\sendmail\sendmail.ini让 gmail 发送邮件。
in C:\xampp\php\php.inifind extension=php_openssl.dlland remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.
在C:\xampp\php\php.ini查找extension=php_openssl.dll并删除该行开头的分号以使 SSL 可用于 localhost 的 gmail。
in php.ini file find [mail function]and change
在 php.ini 文件中查找[mail function]并更改
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "C:\xampp\sendmail\sendmail.exe -t"
(use the above send mail path only and it will work)
(仅使用上面的发送邮件路径,它会起作用)
Now Open C:\xampp\sendmail\sendmail.ini. Replace all the existing code in sendmail.ini with following code
现在打开C:\xampp\sendmail\sendmail.ini。用以下代码替换 sendmail.ini 中的所有现有代码
[sendmail]
smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=my-gmail-password
[email protected]
Now you have done!! create php file with mail function and send mail from localhost.
现在你已经完成了!!使用邮件功能创建php文件并从本地主机发送邮件。
Update
更新
First, make sure you PHP installation has SSL support (look for an "openssl" section in the output from phpinfo()).
首先,确保您的 PHP 安装支持 SSL(在 输出中查找“openssl”部分phpinfo())。
You can set the following settings in your PHP.ini:
您可以在 PHP.ini 中设置以下设置:
ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");

