Search code examples
phpemailsmtpphpmailer

PHPMailer closes connection


I'm trying to send an e-mail with PHPMailer to my SMTP server hosted with Mail-In-A-Box (which uses Postfix) on another Ubuntu 16.04 VPS, but I have an error
The mail vps is hosted on Digital Ocean, same for the website/php one
This is what I get when I run the code:

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.

This is the code I use to send the email:

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('[email protected]', '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;
        }
    }

It's the PHPMailer's example modified. As you can see I tried using the popBeforeSMTP function, but I had the same error, and another one before it:

<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>

I tried with ssl instead of tls, but I'm receiving this:

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`

I know some other user have asked a question like this, but none of the solutions I've tried worked for me.

I've imported PHPMailer through the composer with this line

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

Update

I changed the code to this (still coming from the PHPMailer examples)

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

        $smtp_settings = array(

            "debug" => 2,
            "host" => "smtp.ultracore.it",
            "port" => 587,
            "auth" => true,
            "encryption" => "tls",
            "reply_address" => "[email protected]",
            "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!';
        }
    }

But I have the same error

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

I tried switching the cafile to all of this files: files I tried

Am I doing this wrong? The files are on the website's VPS, I've applied the certificates to the server through Mail-In-A-Box's admin panel. I have CloudFlare, but the address smtp.ultracore.it is not under cloudflare's protection, and all the certificates are created on the Mail VPS' address, so ultracore.it is created with the address of mail.ultracore.it, not the website's one...

The code

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

returns this

SSL loaded

Solution

  • You're using SMTPSecure = 'ssl' (implicit TLS) on port 587, which expects explicit TLS (STARTTLS). This is covered in all the examples and the docs.

    When STARTTLS fails, it's most likely due to an invalid certificate, or an invalid CA certificate, and again, that is covered in the troubleshooting guide.

    Don't use POP-before-SMTP; that's a really ancient auth mechanism and should not be used.