我查看了以下链接:
通过 PHP Mailer 使用 Gmail SMTP 服务器发送电子邮件
http://uly.me/phpmailer-and-gmail-smtp/
...并尝试为自己实现这些组合但是...大多数时候它会发送此消息...
无法发送消息。
邮件程序错误:SMTP 连接()失败。
然而,当我在“tls”和“ssl”之间进行实验时,它曾经发送过这个......
SMTP 错误:无法连接到服务器:连接超时 (110) SMTP connect() 失败。无法发送消息。
邮件程序错误:SMTP 连接()失败。
我的代码已附上...我是否以某种方式错过了什么?我问网络托管服务他们是否阻止并给了他们我的代码模板 - 他们说服务器允许连接到 Gmail 的 SMTP。
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail -> IsSMTP();
$mail -> SMTPDebug = 2;
$mail -> SMTPAuth = 'true';
$mail -> SMTPSecure = 'tls';
$mail -> SMTPKeepAlive = true;
$mail -> Host = 'smtp.gmail.com';
$mail -> Port = 587;
$mail -> IsHTML(true);
$mail -> Username = "myemail@gmail.com";
$mail -> Password = "mypassword";
$mail -> SingleTo = true;
$to = xxx;
$from = xxx;
$fromname = xxx;
$subject = xxx;
$message = xxx
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$mail -> From = $from;
$mail -> FromName = $fromname;
$mail -> AddAddress($to);
$mail -> Subject = $subject;
$mail -> Body = $message;
if(!$mail -> Send()){
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail-> ErrorInfo;
exit;
}