可能重复:
localhost上的php mail()函数
我正在尝试对我的网站上的密码恢复进行一些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()
Run Code Online (Sandbox Code Playgroud)
以下是我的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
Run Code Online (Sandbox Code Playgroud)
我不知道如何为localhost测试设置这些.我意识到我需要设置SMTP我的提供商的邮件服务器,但我在共享的办公楼工作,所以我不知道如何找到谁在这里提供互联网.
提前致谢.
PHP的mail()函数不直接实现SMTP协议.相反,它依赖于sendmail()MTA(SMTP服务器),或者像postfix或mstmp这样的替代品.只要安装了MTA,它在Unix上运行良好.
在Windows上(来自PHP.net手册):
mail()的Windows实现在许多方面与Unix实现有所不同.首先,它不使用本地二进制文件来编写消息,而只是在直接套接字上运行,这意味着需要在网络套接字(可以在本地主机或远程机器上)上侦听MTA.
所以 - 故事的寓意 - 你需要安装邮件服务器.
但是 - 如果它仅用于测试目的 - 只需获取实际实现SMTP协议的PHP库并使用常规gmail电子邮件地址发送电子邮件:
而不是使用PHP的mail()使用以下其中一个:
这些PHP库实际上实现了SMTP协议,因此可以轻松地从任何平台发送电子邮件,而无需在同一台机器上安装电子邮件服务器:
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");
Run Code Online (Sandbox Code Playgroud)