【发布时间】:2011-07-23 08:09:00
【问题描述】:
为此,我已经尝试过在 StackOverflow 和其他网站上发布的每一个脚本/代码/方法,但没有成功。我在 GoDaddy 上托管。我已经设置了一个 Google App 帐户,设置了 MX 记录所需的一切(为此使用 GoDaddy 工具),甚至尝试从我的站点的 GMAIL 界面发送一些电子邮件,以及通过我的一个 unix 上的终端中的 SMTP机器。这一切都奏效了。
但是,当我尝试使用 PHP 时,它没有!是不是像 GoDaddy 以某种方式阻止它?
我总是收到:
SMTP -> 错误:连接失败 服务器:连接被拒绝 (111) SMTP 错误:无法连接到 SMTP 主机。 邮件程序错误:SMTP 错误:无法 连接到 SMTP 主机。
这是我用于 PHPMailer 的代码:
<html>
<head>
<title>PHPMailer - SMTP (Gmail) advanced test</title>
</head>
<body>
<?php
require_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$mail = new PHPMailer(true); // the true param means it will throw exceptions on errors, which we need to catch
$mail->IsSMTP(); // telling the class to use SMTP
try {
$mail->Host = "smtp.gmail.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
$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 = "MYFROMADDRESSHERE"; // GMAIL username
$mail->Password = "MYFROMPASSWORDHERE"; // GMAIL password
$mail->AddReplyTo('MYFROMADDRESSHERE', 'Sender Name');
$mail->AddAddress('TESTTOADDRESSHERE', 'Recipient Name');
$mail->SetFrom('MYFROMADDRESSHERE', 'Sender Name');
$mail->AddReplyTo('MYFROMADDRESSHERE', 'Sender Name');
$mail->Subject = 'PHPMailer Test Subject via mail(), advanced';
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML(file_get_contents('contents.html'));
$mail->AddAttachment('images/phpmailer.gif'); // attachment
$mail->AddAttachment('images/phpmailer_mini.gif'); // attachment
$mail->Send();
echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
?>
</html>
谢谢!
【问题讨论】:
-
简单的测试方法,如果您有 shell 访问权限:登录服务器,然后尝试“telnet smtp.gmail.com 465”。您将无法执行 SSL 的操作,但如果存在防火墙问题,您也会在此处收到“连接被拒绝”。如果它连接然后挂起输入,它不是防火墙,而是与您的代码有关。
-
我这台服务器上没有开启SHELL,需要48小时才能激活;需要在几个小时内解决这个问题。还有其他建议吗?