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

时间:2015-03-02 23:10:17

标签: php email

我正在尝试使用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!';
?> 

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用SMTPDebug查看实际发生的情况

$mail->SMTPDebug = 2;

看起来你正在使用错误的端口。尝试465并打开该端口。

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