PHPMailer SMTP错误:EHLO命令失败。无法连接到SMTP主机

时间:2017-11-28 15:35:17

标签: php smtp phpmailer

我尝试升级到PHPMailer 6并遇到了一些问题。我首先安装了composer,然后使用命令require phpmailer/phpmailer

安装了PHPMailer

然后我在我的Sites文件夹中创建了一个composer.json文件,并在终端中使用了composer install

{
    "require": {
        "phpmailer/phpmailer": "~6.0"
    }
}

安装似乎成功,供应商文件夹是在Sites中创建的,但是当我尝试使用phpmailer中的示例脚本时,我收到以下错误:

2017-11-28 12:03:51 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP o88sm45054643wrc.10 - gsmtp
2017-11-28 12:03:51 CLIENT -> SERVER: EHLO localhost
2017-11-28 12:04:09 SERVER -> CLIENT: 
2017-11-28 12:04:09 SMTP ERROR: EHLO command failed: 
2017-11-28 12:04:09 SMTP NOTICE: EOF caught while checking if connected
SMTP Error: Could not connect to SMTP host.
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

我的PHP代码是:

if(isset($_POST['submit'])) {

    $mail = new PHPMailer(true);                    // Passing `true` enables exceptions
    try {
        //Server settings
        $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 = 'myusername@gmail.com';   // SMTP username
        $mail->Password = 'mypassword';             // SMTP password
        $mail->SMTPSecure = 'tls';                  // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                          // TCP port to connect to

        //Recipients
        $mail->setFrom('myusername@gmail.com', 'My Name');
        $mail->addAddress('myusername@gmail.com', 'My Name');       // Add a recipient
        $mail->addReplyTo('myusername@gmail.com', 'My Name');
        $mail->addCC('another-email');
        $mail->addBCC('another-email');

        //Attachments
        $mail->addAttachment('images/facebook.png');         // test attachment

        //Content
        $mail->isHTML(true);                        // Set email format to HTML
        $mail->Subject = 'Here is the subject';
        $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
        $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

        $mail->send();
        echo 'Message has been sent';
    } catch (Exception $e) {
        echo 'Message could not be sent. ';
        echo 'Mailer Error: ' . $mail->ErrorInfo;
    }

}

我已经看了很多类似错误的答案,并尝试了以下所有方法: 使用密码注销并返回Google,以便我知道是正确的。

我已按照Google中的安全设置进行操作 https://myaccount.google.com/lesssecureapps - 允许安全性较低的应用启用 https://accounts.google.com/DisplayUnlockCaptcha - 已启用帐户访问权限

还查看了故障排除 https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

在终端中使用telnet smtp.gmail.com 587成功

虽然看着这个我注意到虽然我安装了php7并且正在工作但我找不到php7文件夹并且仍然有php5,它似乎包含所有的php文件。

还有两个php.ini文件 - 在phpinfo中,php.ini的路径是usr / local / php5 / lib,而终端命令php -- ini表示已加载的配置文件:/etc/php.ini

当我重新启动Apache时,我在日志中也会出现以下错误 - 不确定它是否已连接但我认为我发布以防万一

[ServerName设置为localhost时删除了Apache错误]

我不确定双php.ini文件是否会影响PHPMailer中的任何内容,但我现在已经用尽了。

请帮忙!希望我之前没有升级过。

0 个答案:

没有答案