php SMTP connect()在发送邮件时失败


php SMTP connect() failed while sending mail

我正试图使用PHPMailer通过SMTP发送电子邮件,但到目前为止我运气不佳。我已经看了很多问题,PHPMailer教程和论坛帖子,但我仍然找不到让它发挥作用的方法。我仍然收到一个错误Message could not be sent.Mailer Error: SMTP connect() failed.

这是代码:

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Username = 'myemail@gmail.com';
$mail->Password = '************';
$mail->Port = "995";
$mail->From = 'myemail@gmail.com';
$mail->FromName = 'Tim Cullen';
$mail->addAddress('james@example.com', 'James Johnson');
$mail->addReplyTo('myemail@gmail.com', 'Tim Cullen');
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = 'Using PHPMailer';
$mail->Body    = 'Hi Iam using PHPMailer library to sent SMTP mail from localhost';
if(!$mail->send()) {
   echo 'Message could not be sent.';
   echo 'Mailer Error: ' . $mail->ErrorInfo;
   exit;
}
echo "EMail has been sent!";
//echo 'Message saved to Send folder!';
?> 

您可以尝试SMTPDebug来查看实际发生了什么

$mail->SMTPDebug = 2;

看起来你用错了端口。试试465打开那个端口。

iptables -A INPUT -p tcp --dport 465 -j ACCEPT