我正试图使用PHPMailer通过SMTP发送电子邮件,但到目前为止还没有成功。我已经浏览了许多SO问题、PHPMailer教程和论坛帖子,但仍然无法使其发挥作用。为了节省时间,我会尽可能多地记录我的失败尝试,但首先是我正在使用的代码:
<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors','On');
require('includes/class.phpmailer.php');
include('includes/class.smtp.php');
$mail = new PHPMailer();
$name = $_POST["name"];
$guests = $_POST["guests"];
$time = $_POST["time"];
$message = "<h1>".$name." has booked a table for ".$guests." at ".$time."</h1>";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "ssl://smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 26; // set the SMTP port for the GMAIL server
$mail->Username = "myEmail@gmail.com"; // SMTP account username
$mail->Password = "myPassword"; // SMTP account password
$mail->SetFrom('myEmail@gmail.com', 'James Cushing');
$mail->AddReplyTo("myEmail@gmail.com","James Cushing");
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
$mail->MsgHTML($message)
$address = "myOtherEmail@me.com";
$mail->AddAddress($address, "James Cushing");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
首先,当我现在运行这个代码时,我会得到两个不同的错误。在本地服务器上,我收到错误:
SMTP->错误:无法连接到服务器:操作超时(60)
以下发件人地址失败:myEmail@gmail.com:在未连接的情况下调用了Mail()
Mailer错误:以下发件人地址失败:myEmail@gmail.com:在未连接的情况下调用了Mail()
在我的web服务器上运行相同的代码时,我或多或少会遇到相同的错误,但第一行是:
SMTP->错误:无法连接到服务器:无法访问网络(101)
显然,值得指出的是,我没有使用字面意思"myEmail@gmail.com"但我已经用我自己的电子邮件代替了这篇文章
我尝试过的东西
-使用iCloud SMTP服务器
-使用其他端口
-在我的php.ini文件中启用OpenSSL扩展
-从各种PHPMailer示例复制代码
-使用谷歌的"DisplayUnlockCaptcha"系统启用连接
-发送到不同地址和从不同地址发送-从Username属性中删除"@gmail.com"-其他一些事情我记不清了
这件事已经让我抓狂了大约一天,所以如果有人能解决它,他们将成为英雄。
感谢
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = "myemail@gmail.com";
$mail->Password = "**********";
$mail->Port = "465";
这是一种有效的配置。
尝试替换
不要在端口465上使用SSL,自1998年以来一直不推荐使用,只有没有收到备忘录的Microsoft产品才会使用它;在端口587上使用TLS:因此,下面的代码应该非常适合您。
mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "smtp.gmail.com"; // SMTP server
$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
首先,将这些设置用于Google:
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls"; //edited from tsl
$mail->Username = "myEmail";
$mail->Password = "myPassword";
$mail->Port = "587";
但是,你设置了什么防火墙?
如果您要过滤掉TCP端口465/995,可能还有587,则需要配置一些异常或将其从规则列表中删除。
https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
每当我的客户端机器更改网络连接(例如,家庭与办公室网络)并以某种方式重新启动网络服务(或重新启动机器)为我解决问题时,我的SMTP也会出现类似的故障。不确定这是否适用于您的情况,但以防万一。
sudo /etc/init.d/networking restart # for ubuntu
首先,谷歌创建了"使用不太安全的账户方法"功能:
https://myaccount.google.com/security
然后创建了另一个权限:
https://accounts.google.com/b/0/DisplayUnlockCaptcha
希望能有所帮助。