您必须有 SMTP 服务器,才能发送邮件
替代解决方案是使用外部设备,例如 gmail
首先你应该有一个有效的 gmail 凭证,然后去 php.ini 并配置它如下:
[mail function]
SMTP = ssl://smtp.gmail.com
smtp_port = 465
username = YOUR_GOOGLE_USERNAME
password = YOUR_GOOGLE_PASSWORD
重新启动您的网络服务器并尝试使用经典的 php mail() 函数
这是我用于 symfony 项目的 yml SMTP 服务器配置:
parameters:
mailer_transport: gmail
mailer_host : 127.0.0.1
mailer_user : anis.halayem@gmail.com
mailer_password : ***my_gmail_password_secret***
locale : en
您可以设置自己的本地 SMTP 服务器local SMTP Server
__EDIT__
尝试使用 PHPMailer:PHPMailer - A full-featured email creation
它的好处是,它可以让您调试导致使用 gmail smtp 服务器发送邮件的问题
第一件事:你在代理后面吗?如果是这种情况,则必须配置HTTP_PROXY/HTTPS_PROXY 环境变量
您可以找到有用的文档 here 和 here
现在,您可以创建一个 php 测试脚本,您可以通过命令行或使用 Web 服务器直接启动它
<?php
require_once ('PHPMailer-master/class.phpmailer.php');
require_once ('PHPMailer-master/class.smtp.php');
$mail = new PHPMailer();
$body = "<h1> Sending HTML Mails using gmail</h1><p>it's great !!</p>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "tls"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 587; // set the SMTP port for the GMAIL server
$mail->Username = "YOUR_GMAIL_ACCOUNT" ; // GMAIL username
$mail->Password = 'YOUR_GMAIL_PASSWORD' ; // GMAIL password
$mail->SetFrom('VALID_USER@gmail.com', 'Anis Halayem');
$mail->Subject = "Test Send Mails";
$mail->MsgHTML($body);
$address = "VALID_USER@gmail.com";
$mail->AddAddress($address, "USER NAME");
// $mail->AddAttachment("images/phpmailer.gif"); // attachment
// $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
else {
echo "Message sent!";
}