SMTP Connect()失败.消息未发送.Mailer错误:SMTP Connect()失败

Say*_*ari 24 php email smtp

我试图将邮件发送到gmail地址,但它继续收到此错误"SMTP - >错误:无法连接到服务器:连接超时(110)SMTP连接()失败.邮件未发送.Mailer错误: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.';
            }
Run Code Online (Sandbox Code Playgroud)

小智 59

删除或注释掉该行 -

$mail->IsSMTP();
Run Code Online (Sandbox Code Playgroud)

它会对你有用.

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

  • 如果我删除该行我得到另一个错误:邮件程序错误:无法实例化邮件功能. (6认同)

小智 9

你必须安装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';
Run Code Online (Sandbox Code Playgroud)

然后你收到了一封来自gmail的电子邮件,其中讨论了如何在此处启用不安全访问应用程序选项https://www.google.com/settings/security/lesssecureapps

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


Dmi*_* DB 4

您没有 SMTPSecure 设置来定义所使用的身份验证类型,并且您正在使用不必要的“ssl://”运行主机设置(PS - ssl 通过端口 465 运行,如果您需要通过端口运行)相反,请参阅此处接受的答案。以下是要添加/更改的行:

+ $mail->SMTPSecure = 'tls';

- $mail->Host = "ssl://smtp.gmail.com";
+ $mail->Host = "smtp.gmail.com";
Run Code Online (Sandbox Code Playgroud)