简体   繁体   English

Gmail SMTP错误与PHP Mailer

[英]Gmail SMTP Error With PHP Mailer

I know this question has been asked a lot and I've read through most of them. 我知道这个问题已经问了很多,我已经阅读了大部分。

I'm getting this error when trying to use my Gmail account to send email from PHP using PHP Mailer and WAMP Server. 尝试使用我的Gmail帐户通过PHP Mailer和WAMP Server从PHP发送电子邮件时,出现此错误。

  1. It was working till yesterday without any problems (since about 3 months). 它一直工作到昨天没有任何问题(大约三个月以来)。
  2. I have not changed any code in the mailer helper function. 我没有更改邮件助手功能中的任何代码。
  3. php-openssl.dll is enabled in my wamp settings. 在我的Wamp设置中启用了php-openssl.dll。
  4. Tried with ssl & 465, Doesn't connect. 尝试过ssl和465,无法连接。
  5. I have "Allow less secured apps" turned on in my google settings. 我在Google设置中启用了“允许安全性较低的应用程序”。
  6. I've tried using gethostbyname(). 我试过使用gethostbyname()。 it doesn't work. 它不起作用。
  7. It is working with my organisation's email server, which doesn't use any security. 它与我组织的电子邮件服务器配合使用,该服务器不使用任何安全性。

This is my phpmailer function code. 这是我的phpmailer功能代码。 All the settings are being retrieved from the database. 正在从数据库中检索所有设置。 As it is a multi user application. 由于它是一个多用户应用程序。

// Email Settings
    $from=$details->from;
    $mailer=$details->mailer;
    $subject=$details->subject;

    $mail = new PHPMailer(true);                                // Passing `true` enables exceptions
    try {
        //Server settings
        $mail->SMTPDebug = $settings->debug;                    // Enable verbose debug output
        $mail->isSMTP();                                        // Set mailer to use SMTP
        $mail->Host = $settings->host;          // Specify main and backup SMTP servers
        $mail->SMTPAuth = true;                                 // Enable SMTP authentication
        $mail->Username = $settings->login;                     // SMTP username
        $mail->Password = $pass;                                // SMTP password
        $mail->SMTPSecure = $settings->security;                // Enable TLS encryption, `ssl` also accepted
        $mail->Port = $settings->port;                          // TCP port to connect to

        //From
        $mail->setFrom($from, $mailer);

        //Recipients
        $mail->addAddress($details->to,$details->recipientName);     // Add a recipient           // Name is optional
        $mail->addReplyTo($from, $mailer);
        foreach($quote->cc as $cc)
        {
            $mail->addCC($cc);
        }
        //Attachments
        foreach($quote->attachments as $file)
        {
            $mail->addAttachment($file);    // Add attachments  // Optional name
        }
        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->AltBody = 'Non-HTML Mail Clients Not Supported, Please View This email in a browser or get a HTML friendly Email Client.';
        $mail->send();
    }
    catch (Exception $e)
    {
        $return=array(1,'Error: '.$mail->ErrorInfo);
    }
return $return;
}

The error That I'm getting is 我得到的错误是

2017-12-04 07:15:26 SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP w9sm23070345pfk.16 - gsmtp<br>
2017-12-04 07:15:26 CLIENT -&gt; SERVER: EHLO projects<br>
2017-12-04 07:15:26 SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [49.213.37.11]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8<br>
2017-12-04 07:15:26 CLIENT -&gt; SERVER: STARTTLS<br>
2017-12-04 07:15:27 SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS<br>
SMTP Error: Could not connect to SMTP host.<br>
2017-12-04 07:15:27 CLIENT -&gt; SERVER: QUIT<br>
2017-12-04 07:15:27 <br>
2017-12-04 07:15:27 <br>
SMTP Error: Could not connect to SMTP host.<br>
[1,"Error: SMTP Error: Could not connect to SMTP host."]

Any help is appreciated. 任何帮助表示赞赏。

PS : The settings I'm using are PS:我使用的设置是

  1. Host : smtp.gmail.com. 主持人:smtp.gmail.com。
  2. Username and password : I've checked and I can log into my account on gmail web. 用户名和密码:我已经检查过,可以登录gmail网站上的帐户。
  3. Security : tsl 安全性:TSL
  4. Port : 587. 端口:587。

I doubt if its my code. 我怀疑是否是我的代码。 coz if it was my code, then It would't have worked with my organisation's email server, which is not as sophisticated as gmail. 如果这是我的代码,那么它就不能与我组织的电子邮件服务器一起使用,该服务器不如gmail复杂。

i see an error like your question, and i solve my problem by: 我看到类似您的问题的错误,并且我通过以下方式解决了我的问题:

  1. Enable extentsion ssl from apache 从Apache启用扩展SSL
  2. Update the lastest code from PHPMailer 更新来自PHPMailer的最新代码
  3. Add some code: 添加一些代码:

     $mail->SMTPOptions = array( 'ssl' => array( 'verify_peer' => false, 'verify_peer_name' => false, 'allow_self_signed' => true ) 

    ); );

And it's work! 它的工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM