【发布时间】:2014-10-05 12:38:00
【问题描述】:
我正在尝试通过 PHP 邮件程序发送电子邮件,但失败得很惨。我得到的错误信息如下:
2014-08-12 12:21:40 SMTP ERROR: Failed to connect to server: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. (10060) 2014-08-12 12:21:40 SMTP connect() failed. Mailer Error: SMTP connect() failed.
我的代码如下。我不知道我哪里错了。我很确定所有信息都是正确的,除了端口。鉴于这是使用 microsoft exchange 我正在使用端口 587 - 这是我出错的地方吗?
<?php
include("phpmailer/class.phpmailer.php");
$mail = new PHPMailer();
$body = "HellooooO";
$mail->IsSMTP(); // telling the class to use SMTP
$mail->SMTPSecure = "tls";
$mail->SMTPDebug = 1; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SMTPAuth = true;
$mail->Host = "My server name";
$mail->Port = 587;
$mail->Username = "My MS exchange email address";
$mail->Password = "Password";
$mail->SetFrom('My MS exchange email address', 'First Last');
$mail->Subject = "PHPMailer Test Subject via smtp, basic with authentication";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$address = "test email address";
$mail->AddAddress($address, "John Doe");
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
-----------编辑-------
根据 Synchro 的说明我没有使用最新版本的 PHP Mailer,我将代码修改如下。我仍然无法发送电子邮件并且错误消息相同...如何检查 TLS 端口是否打开并按预期工作?
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail ->SMTPDebug = 1;
$mail->isSMTP();
$mail->Host = 'My Server';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'My email address';
$mail->Password = 'My Password';
$mail->SMTPSecure = 'tls';
$mail->From = 'My email address';
$mail->FromName = 'Mailer';
$mail->addAddress('My Test Email address', 'Joe User');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
问候和感谢, G.
【问题讨论】:
-
确保端口正确,通常TLS端口为465。
-
感谢您的帖子。我将端口更改为 465,但这并没有解决问题。
-
TLS 端口是 587,而不是 465。这是 SSL 端口,至少从 1999 年就已经过时了。您使用的是旧版本的 PHPMailer。您在 设置
AltBody之后调用msgHTML,因此AltBody将被覆盖。您看到的错误很可能是由于 DNS 故障、防火墙阻止或其他网络问题。 -
您好 - 非常感谢您的帮助。我从这里下载了PHPMailer:github.com/Synchro/PHPMailer ...这不是最新版本吗?
-
撞有人知道吗?
标签: php ms-office phpmailer outlook-2010 exchange-server-2010