Possible Duplicate:
php mail() function on localhost
我正尝试在我的站点上进行一些本地主机测试以恢复密码,但是当我尝试发送电子邮件时,出现以下错误:
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
这是我的php.ini文件中的相关设置。
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 25
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain
我不确定为本地主机测试设置什么。我意识到我需要将 SMTP
设置为我的提供商的邮件服务器,但我在共享办公大楼工作,所以我什至不知道如何找出谁在这里提供互联网。
提前致谢。
PHP 的 mail() 函数不直接实现 SMTP 协议(protocol)。相反,它依赖于 sendmail() MTA(SMTP 服务器)或类似 postfix 或 mstmp 的替代品。
只要安装了 MTA,它就可以在 Unix 上正常工作。
在 Windows 上(来自 PHP.net 手册):
The Windows implementation of mail() differs in many ways from the
Unix implementation. First, it doesn't use a local binary for
composing messages but only operates on direct sockets which means a
MTA is needed listening on a network socket (which can either on the
localhost or a remote machine).
所以 - 这个故事的寓意 - 您需要安装邮件服务器。
但是 - 如果它仅用于测试目的 - 只需获取一个实际实现 SMTP 协议(protocol)的 PHP 库并使用您的常规 gmail 电子邮件地址发送电子邮件:
不使用 PHP 的 mail(),而是使用其中之一:
- PHP邮件程序
- SwiftMailer
- Zend\邮件
这些 PHP 库实际上实现了 SMTP 协议(protocol),因此可以轻松地从任何平台发送电子邮件,而无需在同一台机器上安装电子邮件服务器:
PHPMAILER 示例:
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "stmp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port for the GMAIL server
$mail->Username = "some_email@gmail.com"; // GMAIL username
$mail->Password = "pass111"; // GMAIL password
$mail->SetFrom('some_email@gmail.com', 'My name is slim shady');
$mail->AddReplyTo("some_email@gmail.com","My name is slim shady");
$mail->Subject = "Hey, check out http://www.site.com";
$mail->AltBody = "Hey, check out this new post on www.site.com"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "some_email@gmail.com";
$mail->AddAddress($address, "My name is slim shady");