SMTP 连接() 失败。未发送邮件。邮件程序错误:SMTP 连接() 失败

2022-08-30 15:24:05

我尝试将邮件发送到gmail地址,但它不断收到此错误“SMTP ->错误:无法连接到服务器:连接超时(110)SMTP Connect()失败。未发送邮件。邮件程序错误: SMTP 连接() 失败。可能是什么问题?

        require 'class.phpmailer.php'; // path to the PHPMailer class
        require 'class.smtp.php';

            $mail = new PHPMailer();


            $mail->IsSMTP();  // telling the class to use SMTP
            $mail->SMTPDebug = 2;
            $mail->Mailer = "smtp";
            $mail->Host = "ssl://smtp.gmail.com";
            $mail->Port = 587;
            $mail->SMTPAuth = true; // turn on SMTP authentication
            $mail->Username = "myemail@gmail.com"; // SMTP username
            $mail->Password = "mypasswword"; // SMTP password 
            $Mail->Priority = 1;

            $mail->AddAddress("myemail@gmail.com","Name");
            $mail->SetFrom($visitor_email, $name);
            $mail->AddReplyTo($visitor_email,$name);

            $mail->Subject  = "Message from  Contact form";
            $mail->Body     = $user_message;
            $mail->WordWrap = 50;  

            if(!$mail->Send()) {
            echo 'Message was not sent.';
            echo 'Mailer error: ' . $mail->ErrorInfo;
            } else {
            echo 'Message has been sent.';
            }

答案 1

删除或注释掉该行-

$mail->IsSMTP();

它将为你工作。

我已经检查并尝试了来自不同站点的许多答案,但除了上述解决方案之外,没有任何解决方案。


答案 2

你必须已经安装了php_openssl.dll,如果你使用wampserver,这很容易,搜索并应用PHP的扩展。

在示例中,更改以下内容:

    //Set the hostname of the mail server
    $mail->Host = 'smtp.gmail.com';

    //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission 465 ssl
    $mail->Port = 465;

    //Set the encryption system to use - ssl (deprecated) or tls
    $mail->SMTPSecure = 'ssl';

然后,您从gmail接收了一封电子邮件,以启用此处的“安全性较低的访问应用程序”选项 https://www.google.com/settings/security/lesssecureapps

我建议您更改密码并不断加密


推荐