无法使用PHPMailer连接到gmail SMTP

时间:2018-08-19 14:38:28

标签: email smtp gmail gmail-imap

尝试使用gmail smtp从服务器发送邮件时出现以下错误:

2018-08-19 14:30:45 Connection: opening 2018-08-19 14:30:55 SMTP ERROR: Failed to connect to server: Connection timed out (110) SMTP connect() failed. Mailer Error: SMTP connect() failed.

如果我使用其他提供商(例如mailgun),则工作正常。但是用gmail我得到了同样的错误。我在Google帐户上打开了安全性较低的应用,并且我的帐户上没有两步验证设置。

以下是我正在使用的代码:

//Instantiate Mailer
        $mail = new Mail();

        //Enable SMTP debugging. 
        $mail->SMTPDebug = 3;                               
        //Set PHPMailer to use SMTP.
        $mail->isSMTP();            
        //Set SMTP host name                          
        $mail->Host = "smtp.gmail.com";
        //Set this to true if SMTP host requires authentication to send email
        $mail->SMTPAuth = true;                          
        //Provide username and password     
        $mail->Username = "myemail@gmail.com";                 
        $mail->Password = "my_password";                           
        //If SMTP requires TLS encryption then set it
        $mail->SMTPSecure = "tls";                           
        //Set TCP port to connect to 
        $mail->Port = 587;                                   

        $mail->From = "myemail@gmail.com";
        $mail->FromName = "My Name";

        $mail->addAddress($to);

        $mail->isHTML(true);

        $mail->Subject = $subject;
        $mail->Body = $body;

        if(!$mail->send()) 
        {
            echo "Mailer Error: " . $mail->ErrorInfo;exit;
            return false;
        } 
        else 
        {
            //echo "Message has been sent successfully";
            return true;
        }   

我正在使用数字海洋托管帐户。我没有添加任何域,只是在IP地址上工作。而且我的帐户中没有添加MX记录。我找不到任何教程来添加MX记录或连接SMTP所需的其他邮件设置,也无法阻止电子邮件转入gmail垃圾邮件。

下面是来自数字海洋支持的答复,他曾经用过后缀这个词,但我不知道它是什么。

Hello,

There are no blocks or issues on our end to prevent you from sending mail, so the best place to start looking for the cause of this error is your mail log. Typically, you would find this error log at the following location:

/var/log/maillog

If you have customized the configuration of Postfix, the error log may be at a different location, like /var/log/mail or /var/log/mail.log. Please see this StackExchange thread for more information:

http://unix.stackexchange.com/questions/23385/where-to-find-the-error-messages-of-postfix

For a very in depth guide, please see Postfix's official documentation regarding debugging:

http://www.postfix.org/DEBUG_README.html

If you are not using Postfix to send mail, you will need to check the error logs for your specific mail service. If you need help locating those error logs, just let us know which mail service you are using, and we will do our best to help you out.

Please keep in mind that, as a self-managed provider, we are not able to directly access the Droplet to review, install or manage software or the environment. This will need to be done directly by you, though we are glad to provide guidance on things you encounter.

0 个答案:

没有答案