PHP mail()函数的帮助

5
我正在对我的网站进行本地主机测试,尝试进行密码恢复测试,但是当我尝试发送电子邮件时,出现以下错误:

可能重复:
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为我的服务提供商的邮件服务器,但是我在一个共享办公楼工作,所以我不知道如何找出这里的网络服务提供商。

提前感谢您的帮助。

3个回答

7

PHP的mail()函数并没有直接实现SMTP协议。相反,它依赖于sendmail() MTA(SMTP服务器)或像postfix或mstmp这样的替代品。只要安装了MTA,Unix上就可以正常工作。

在Windows上(来自PHP.net手册):

mail()的Windows实现与Unix实现在许多方面都不同。首先,它不使用本地二进制文件来组合邮件,而是仅在直接套接字上运行,这意味着需要在网络套接字上侦听MTA(可以在本地主机或远程计算机上)。

所以 - 故事的寓意 - 您需要安装邮件服务器。

但是 - 如果仅用于测试目的 - 只需获取一个实际实现SMTP协议的PHP库,然后使用您的常规Gmail电子邮件地址发送电子邮件:

不要使用PHP的mail(),而要使用以下之一:

  1. PHPmailer
  2. SwiftMailer
  3. Zend\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");

2

PHP的邮件功能需要本地邮件服务器支持才能运行。

编辑:根据PHP文档中mail()的说明,你可以使用PEAR提供的Mail包来实现邮件发送。


...或者你可以使用ISP的。 - Shef

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接