PHP 的 mail() 函数不直接实现 SMTP 协议。相反,它依赖于 sendmail() MTA(SMTP 服务器),或类似 postfix 或 mstmp 的替代品。
只要安装了 MTA,它就可以在 Unix 上正常工作。
在 Windows 上(来自 PHP.net 手册):
mail() 的 Windows 实现在许多方面与
Unix 实现。首先,它不使用本地二进制文件
撰写消息,但仅在直接套接字上运行,这意味着
需要 MTA 侦听网络套接字(可以在
本地主机或远程机器)。
所以 - 故事的寓意 - 你需要安装邮件服务器。
但是 - 如果仅用于测试目的 - 只需获取一个实际实现 SMTP 协议的 PHP 库并使用您的常规 gmail 电子邮件地址发送电子邮件:
不要使用 PHP 的 mail(),而是使用以下之一:
- PHP 邮件程序
- SwiftMailer
- 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");