PHPMailer 关闭连接

PHPMailer closes connection

我正在尝试使用 PHPMailer 向我在另一个 Ubuntu 16.04 VPS 上使用 Mail-In-A-Box(使用 Postfix)托管的 SMTP 服务器发送电子邮件,但是我有一个错误
邮件 vps 托管在 Digital Ocean 上,website/php 一个
也是如此 这是我 运行 代码时得到的:

2019-02-26 17:54:01     CLIENT -> SERVER: EHLO mail
2019-02-26 17:54:01     SERVER -> CLIENT: 250-mail.ultracore.it
                                          250-PIPELINING
                                          250-SIZE 134217728
                                          250-VRFY
                                          250-ETRN
                                          250-STARTTLS
                                          250-ENHANCEDSTATUSCODES
                                          250-8BITMIME
                                          250-DSN
                                          250 SMTPUTF8
2019-02-26 17:54:01     CLIENT -> SERVER: STARTTLS
2019-02-26 17:54:01     SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2019-02-26 17:54:01     SMTP Error: Could not connect to SMTP host.
2019-02-26 17:54:01     CLIENT -> SERVER: QUIT
2019-02-26 17:54:01     SERVER -> CLIENT:
2019-02-26 17:54:01     SMTP ERROR: QUIT command failed:
2019-02-26 17:54:01     SMTP Error: Could not connect to SMTP host.

这是我用来发送电子邮件的代码:

function sendEmail($from, $password, $email, $subject, $messageHtml, $message)
    {
        $mail = new PHPMailer(true);
        //$pop = POP3::popBeforeSmtp('pop3.ultracore.it', 110, 1, $from, $password, 2);
        try {
            //Server settings
            $mail->SMTPDebug = 2;                                 // Enable verbose debug output
            $mail->isSMTP();                                      // Set mailer to use SMTP
            $mail->Host = 'smtp.ultracore.it';                    // Specify main and backup SMTP servers
            $mail->SMTPAuth = true;                               // Enable SMTP authentication
            $mail->Username = $from;                              // SMTP username
            $mail->Password = $password;                          // SMTP password
            $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
            $mail->Port = 587;                                    // TCP port to connect to

            //Recipients
            $mail->setFrom($from, $from);
            $mail->addAddress($email, 'Joe User');                // Add a recipient
            $mail->addReplyTo('support@ultracore.it', 'Support');

            //Content
            $mail->isHTML(true);                                  // Set email format to HTML
            $mail->Subject = $subject;
            $mail->Body    = $messageHtml;
            $mail->AltBody = $message;

            $mail->send();
            echo 'The message has been sent';
        } catch (Exception $e) {
            echo $mail->ErrorInfo;
        }
    }

修改了PHPMailer的例子。 如您所见,我尝试使用 popBeforeSMTP 函数,但我遇到了同样的错误,之前还有一个错误:

<pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238</pre><pre>Connecting to the POP3 server raised a PHP warning:errno: 2 errstr: fsockopen(): unable to connect to pop3.ultracore.it:110 (Connection refused); errfile: /var/www/html/vendor/phpmailer/phpmailer/src/POP3.php; errline: 238Failed to connect to server pop3.ultracore.it on port 110. errno: 111; errstr: Connection refused</pre>

我尝试使用 ssl 而不是 tls,但我收到了这个:

2019-02-26 18:03:52     SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshootingr`

我知道其他一些用户也问过这样的问题,但是 none 我尝试过的解决方案对我有用。

我已经使用此行通过作曲家导入了 PHPMailer

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

更新

我把代码改成这样(仍然来自 PHPMailer 示例)

function sendEmail($from, $password, $to, $subject, $messageHtml)
    {

        $smtp_settings = array(

            "debug" => 2,
            "host" => "smtp.ultracore.it",
            "port" => 587,
            "auth" => true,
            "encryption" => "tls",
            "reply_address" => "support@ultracore.it",
            "peer_name" => "smtp.ultracore.it",
            "verify_peer" => true,
            "verify_depth" => 3,
            "allow_self_signed" => false,
            "cafile" => "/var/www/html/certificates/ssl_private_key.pem"

        );

        $mail = new PHPMailer;
        //Tell PHPMailer to use SMTP
        $mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = $smtp_settings['debug'];
        //Set the hostname of the mail server
        $mail->Host = $smtp_settings['host'];
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = $smtp_settings['port'];
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = $smtp_settings['encryption'];
        //Custom connection options
        //Note that these settings are INSECURE
        $mail->SMTPOptions = array(
            'ssl' => [
                'verify_peer' => $smtp_settings['verify_peer'],
                'verify_depth' => $smtp_settings['verify_depth'],
                'allow_self_signed' => $smtp_settings['allow_self_signed'],
                'peer_name' => $smtp_settings['peer_name'],
                'cafile' => $smtp_settings['cafile'],
            ],
        );
        //Whether to use SMTP authentication
        $mail->SMTPAuth = $smtp_settings['auth'];
        //Username to use for SMTP authentication - use full email address for gmail
        $mail->Username = $from;
        //Password to use for SMTP authentication
        $mail->Password = $password;
        //Set who the message is to be sent from
        $mail->setFrom($from, 'First Last');
        //Set who the message is to be sent to
        $mail->addAddress($to, 'John Doe');
        //Set the subject line
        $mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $mail->msgHTML($messageHtml, __DIR__);
        //Send the message, check for errors
        if (!$mail->send()) {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message sent!';
        }
    }

但是我有同样的错误

2019-02-26 21:48:23 SERVER -> CLIENT: 220 mail.ultracore.it ESMTP Hi, I'm a Mail-in-a-Box (Ubuntu/Postfix; see https://mailinabox.email/)
2019-02-26 21:48:23 CLIENT -> SERVER: EHLO ultracore.it
2019-02-26 21:48:23 SERVER -> CLIENT: 250-mail.ultracore.it250-PIPELINING250-SIZE 134217728250-VRFY250-ETRN250-STARTTLS250-ENHANCEDSTATUSCODES250-8BITMIME250-DSN250 SMTPUTF8
2019-02-26 21:48:23 CLIENT -> SERVER: STARTTLS
2019-02-26 21:48:23 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-02-26 21:48:23 CLIENT -> SERVER: QUIT
2019-02-26 21:48:23 SERVER -> CLIENT: 
2019-02-26 21:48:23 SMTP ERROR: QUIT command failed: 
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

我尝试将 cafile 切换到所有这些文件: files I tried

我做错了吗?这些文件在网站的 VPS 上,我已经通过 Mail-In-A-Box 的管理面板将证书应用到服务器。 我有 CloudFlare,但是地址 smtp.ultracore.it 不受 cloudflare 的保护,所有证书都是在 Mail VPS 地址上创建的,所以 ultracore.it 是用邮件地址创建的。ultracore.it, 不是网站的...

密码

echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."<br>";

returns这个

SSL loaded

您正在端口 587 上使用 SMTPSecure = 'ssl'(隐式 TLS),它需要显式 TLS (STARTTLS)。这在所有示例和文档中都有介绍。

当 STARTTLS 失败时,很可能是由于无效的证书或无效的 CA 证书,同样是 covered in the troubleshooting guide

不要使用 POP-before-SMTP;那是一种非常古老的身份验证机制,不应使用。

我将设置 "verify_peer" 固定为 false,所以我的代码现在如下所示:

    function sendEmail($from, $password, $to, $subject, $messageHtml)
    {

        $smtp_settings = array(

            "debug" => 2,
            "host" => "mail.ultracore.it",
            "port" => 587,
            "auth" => true,
            "encryption" => "tls",
            "reply_address" => "support@ultracore.it",
            "peer_name" => "mail.ultracore.it",
            "verify_peer" => false,
            "verify_depth" => 3,
            "allow_self_signed" => true,
            "cafile" => "/var/www/html/certificates/ssl_certificate.pem"

        );

        $mail = new PHPMailer;
        //Tell PHPMailer to use SMTP
        $mail->isSMTP();
        //Enable SMTP debugging
        // 0 = off (for production use)
        // 1 = client messages
        // 2 = client and server messages
        $mail->SMTPDebug = $smtp_settings['debug'];
        //Set the hostname of the mail server
        $mail->Host = $smtp_settings['host'];
        //Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
        $mail->Port = $smtp_settings['port'];
        //Set the encryption system to use - ssl (deprecated) or tls
        $mail->SMTPSecure = $smtp_settings['encryption'];
        //Custom connection options
        //Note that these settings are INSECURE
        $mail->SMTPOptions = array(
            'ssl' => [
                'verify_peer' => $smtp_settings['verify_peer'],
                'verify_depth' => $smtp_settings['verify_depth'],
                'allow_self_signed' => $smtp_settings['allow_self_signed'],
                'peer_name' => $smtp_settings['peer_name'],
                'cafile' => $smtp_settings['cafile'],
            ],
        );
        //Whether to use SMTP authentication
        $mail->SMTPAuth = $smtp_settings['auth'];
        //Username to use for SMTP authentication - use full email address for gmail
        $mail->Username = $from;
        //Password to use for SMTP authentication
        $mail->Password = $password;
        //Set who the message is to be sent from
        $mail->setFrom($from, 'First Last');
        //Set who the message is to be sent to
        $mail->addAddress($to, 'John Doe');
        //Set the subject line
        $mail->Subject = $subject;
        //Read an HTML message body from an external file, convert referenced images to embedded,
        //convert HTML into a basic plain-text alternative body
        $mail->msgHTML($messageHtml, __DIR__);
        //Send the message, check for errors
        if (!$mail->send()) {
            echo 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            echo 'Message sent!';
        }
    }