【问题标题】:SMTP connection failedSMTP 连接失败
【发布时间】:2017-10-08 09:39:57
【问题描述】:

在此脚本完美运行之前,我在过去三天就遇到了这个问题。现在出现错误:

SMTP 错误:无法连接到服务器:(0) 2017-10-06 21:05:34 SMTP connect() 失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 邮件未发送。邮件程序错误:SMTP 连接()失败。 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting ahsanazhar12@gmail.com

这是我的脚本:

require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->SMTPDebug = 2;                  // Enable verbose debug output
$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 = 'example@gmail.com'; // SMTP username
$mail->Password = 'mypassword';        // SMTP password
$mail->SMTPSecure = 'tls';             // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;   
$mail->setFrom('example@gmail.com', 'Your Name');
$mail->addAddress('example@gmail.com', 'My Friend');
$mail->Subject  = 'First PHPMailer Message';
$mail->Body     = 'Hi! This is my first e-mail sent through PHPMailer.';
if (!$mail->send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
}

我认为 Gmail 可能更改了使用 SMTP 或类似方式发送电子邮件的设置。

【问题讨论】:

  • 这是一个疯狂的想法 - 尝试点击错误消息中的链接,它会告诉您有关此问题的所有信息以及如何解决它。

标签: php email phpmailer mailer


【解决方案1】:

我终于可以从本地主机发送电子邮件了。这是我的代码。

要安装:

  1. 下载PHPMailer
  2. 将它添加到您的项目中(我把它放在根目录下)
  3. 将 Autoload 类添加到您的脚本中。
  4. 其余代码如下

            require "PHPMailer/PHPMailerAutoload.php";
            $mail = new PHPMailer;
            $mail->SMTPOptions = array(
                'ssl' => array(
                    'verify_peer' => false,
                    'verify_peer_name' => false,
                    'allow_self_signed' => true
                )
            );
            //$mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $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 = 'example@gmail.com';                 // SMTP username
            $mail->Password = 'securepass';                           // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 465;                                    // TCP port to connect to
            //Recipients
            $mail->setFrom('example@gmail.com', "Mailer");
            $mail->addAddress("example@gmail.com","receiver Name");  
            $mail->isHTML(true);                                  
            $mail->Subject = "Subject";
            $mail->Body    = "Body";
            $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
               if( $mail->send()){
                return array("msg"=>msg("success","Email has been sent.<br>"));
                } else {
                    return array("msg"=>msg("error","Email can't send.Try Again<br>"));
                }
    

【讨论】:

    猜你喜欢
    • 2015-06-05
    • 2014-06-13
    • 2015-08-09
    • 1970-01-01
    • 2016-11-10
    • 2014-05-20
    • 2018-09-03
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多