我正在尝试使用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 -> ERROR: Failed to connect to server: Operation timed out (60)
The following From address failed: myEmail@gmail.com : Called Mail() without being connected
Mailer Error: The following From address failed: myEmail@gmail.com : Called Mail() without being connected
在我的Web服务器上运行相同的代码时,我也更或多少会得到相似的错误,但是第一行是:
SMTP -> ERROR: Failed to connect to server: Network is unreachable (101)
显然值得指出的是,我没有使用文字“myEmail@gmail.com”,而是替换为了本帖中的自己的电子邮件地址。
我尝试过的方法
- 使用iCloud SMTP服务器
- 使用不同的端口
- 在php.ini文件中启用OpenSSL扩展
- 从各种PHPMailer示例中复制代码
- 使用Google的“DisplayUnlockCaptcha”系统来启用连接
- 发送到和从不同的地址
- 从用户名属性中删除“@gmail.com”
- 我记不清的其他事情
这已经让我发疯了一天,所以如果有人能解决它,他们将是英雄。
谢谢
SMTP -> 错误:服务器不接受 HELO:
SMTP -> 错误:服务器不接受 AUTH:
SMTP -> 注意:在检查连接时捕获到 EOF - James Cushing
$mail->SMTPSecure = 'tls';
或$mail->SMTPSecure = 'ssl';
。 - symcbean