【问题标题】:phpmailer does not connect to SMTP serversphpmailer 无法连接到 SMTP 服务器
【发布时间】:2014-07-19 18:41:27
【问题描述】:

我一直在尝试使用 phpmailer 并尝试使用 live.com 和 gmail.com 但它总是无法连接到 SMTP 服务器。这是完整的代码(我已经使用 smtp.live.com 尝试了 live.com,但我遇到了同样的问题“无法发送消息。邮件程序错误:SMTP 连接()失败。”)

require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'my emil address';                 // SMTP username
$mail->Password = 'my password';                           // SMTP password
$mail->SMTPSecure = 'TLS';                            // Enable encryption, 'ssl' also accepted

$mail->From = 'the same email address';
$mail->FromName = 'Mailer';
//$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('another email address');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$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';
}

【问题讨论】:

    标签: smtp phpmailer


    【解决方案1】:

    我怀疑是因为你没有设置端口,而 SMTPSecure 应该是小写的。改变这个:

    $mail->SMTPSecure = 'tls';
    $mail->Port = 587;
    

    除此之外,请检查您的 ISP/防火墙是否允许您发送出站邮件,并且您的 DNS 是否正常工作。

    【讨论】:

      【解决方案2】:

      试试这个:

      $mail->SMTPSecure = 'tls'; 
      $mail->Host = 'tls://smtp.gmail.com';
      $mail->Port = 587; //You have to define the Port
      $mail->SMTPDebug  = 3;
      

      删除这个:

      $mail->Host = 'smtp.gmail.com'; 
      $mail->SMTPSecure = 'TLS';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-07-20
        • 2018-05-24
        • 2016-03-14
        • 2014-07-10
        • 1970-01-01
        • 2021-09-18
        • 1970-01-01
        • 2014-11-19
        相关资源
        最近更新 更多