【问题标题】:Uncaught exception 'PHP mailer Exception' with message SMTP connect() failed带有消息 SMTP 连接()的未捕获异常“PHP 邮件程序异常”失败
【发布时间】:2018-11-17 09:17:48
【问题描述】:

我收到了这个错误

致命错误:带有消息的未捕获异常“phpmailerException” 'SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'在 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php:1465 堆栈跟踪:#0 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php(1301): PHPMailer->smtpSend('Date: Tue, 17 O...', 'This is Yousaf ...') #1 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php(1181): PHPMailer->postSend() #2 C:\xampp\htdocs\muhasibb\epaper\1.php(33): PHPMailer->send() #3 {main} 抛出 C:\xampp\htdocs\muhasibb\epaper\PHPMailer-master\class.phpmailer.php 在第 1465 行

当我使用代码时

<?php 
                            // Passing `true` enables exceptions

    //Server settings

    require 'PHPMailer-master/PHPMailerAutoload.php';
    $mail = new PHPMailer(true);                                // Enable verbose debug output
    $mail->isSMTP(); 
    $mail->SMTPDebug = 2;                                     // Set mailer to use SMTP
    $mail->Host = 'smtp.gmail.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'yousaf.farooq906@gmail.com';                 // SMTP username
    $mail->Password = '********';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;
    $mail->Mailer = "smtp";                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('yousaf.farooq906@gmail.com', 'Yousaf Farooq');
    $mail->addAddress('yousaf.farooq906@gmail.com', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional


    //Attachments
        // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Yousaf Farooq';
    $mail->Body    = 'This is Yousaf Farooq';


    if($mail->send())
    echo 'Message has been sent';
    else
    echo 'Message could not be sent.';
?>

【问题讨论】:

  • 既然你已经要求它抛出异常(通过将true 传递给构造函数),尝试捕获它们可能是个好主意。您还使用的是旧版本的 PHPMailer,并且您的代码没有基于它提供的 gmail 示例,它不会犯这些错误。
  • 大部分内容都包含在错误消息链接到的故障排除指南中。

标签: php phpmailer


【解决方案1】:

尝试设置你的

$mail-&gt;SMTPSecure

$mail-&gt;SMTPSecure = 'tls';

另外,为您的谷歌帐户创建应用密码,并使用它来交换您在$mail-&gt;Password 中的密码。

【讨论】:

    【解决方案2】:

    这段代码对我有用

    $mail = new PHPMailer(true); // create a new object
    

    我评论了这一行 //$mail->isSMTP(); // 启用 SMTP

     $mail->SMTPDebug = 4; 
     $mail->SMTPAuth = true; // authentication enabled
    

    你必须使用 587 端口的 tls

     $mail->SMTPSecure = 'tls'; // secure transfer enabled REQUIRED for Gmail
     $mail->Host = "smtp.gmail.com";
     $mail->Port = 587; 
     $mail->Mailer = "smtp";
     $mail->isHTML(true);
     $mail->Username = "yourmail@gmail.com";
     $mail->Password = "yourpassword";
     $mail->From="yourmail@gmail.com";
     $mail->FromName="YOUR NAME";
     $mail->Subject = $subject;
     $mail->Body = $body;
     $mail->addAddress("somemail@gmail.com");
    
     if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
     } else {
       echo "Email Sent";
     }
    

    此外,当您使用 gmail 时,您可以转到具有帐户访问权限的应用并设置允许不太安全的应用:开

    GMAIL CONFIGURATION

    【讨论】:

      【解决方案3】:

      如果在本地工作,

      在php.ini中:去掉“sendmail_from = postmaster@localhost”前面的分号,重启xampp。

      在 gmail 帐户中:在安全设置中,打开“允许安全性较低的应用”。

      最后在 PHP 脚本中:添加两个函数: "date_default_timezone_set('Etc/UTC')" “gethostbyname('ssl://smtp.gmail.com')”作为“$mail->Host”的值

      【讨论】:

      • 1) 这与 php.ini 设置无关,因为它使用的是直接 SMTP; 2) 不好的建议,没有必要——阅读 PHPMailer 文档; 3) 如果您使用 IPv6,将会中断,并且是 gethostbyname 的错误语法。除此之外,太好了:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 2014-08-08
      • 1970-01-01
      • 2013-02-01
      • 2016-02-27
      • 2016-05-19
      • 1970-01-01
      相关资源
      最近更新 更多