Phpmailer: SMTP server ready, yet fail to connect to host

Created on 2 Nov 2015  ·  9Comments  ·  Source: PHPMailer/PHPMailer

Hi there,

I have been experiencing issues with sending mail through a SMTP host. The host does not require authentication (accepts unencrypted connections over port 25 from whitelisted IPs). I have tried the telnet command, and it checks out fine:

Trying 10.88.2.62...
Connected to smtp01.uni.au.dk.
Escape character is '^]'.
220 smtp01.uni.au.dk Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at  Mon, 2 Nov 2015 22:00:26 +0100 

However, when I attempt to send a test mail, it fails after the "SMTP server ready message":

2015-11-02 20:48:33 SERVER -> CLIENT: 220 smtp01.uni.au.dk Microsoft ESMTP MAIL Service, Version: 8.5.9600.16384 ready at Mon, 2 Nov 2015 21:48:34 +0100
2015-11-02 20:48:33 CLIENT -> SERVER: EHLO lotus.au.dk
2015-11-02 20:48:33 SERVER -> CLIENT: 250-smtp01.uni.au.dk Hello [10.29.0.121] 250-TURN 250-SIZE 26214400 250-ETRN 250-PIPELINING 250-DSN 250-ENHANCEDSTATUSCODES 250-8bitmime 250-BINARYMIME 250-CHUNKING 250-VRFY 250-TLS 250-STARTTLS 250 OK
2015-11-02 20:48:33 CLIENT -> SERVER: STARTTLS
2015-11-02 20:48:33 SERVER -> CLIENT: 220 2.0.0 SMTP server ready
2015-11-02 20:48:33 SMTP Error: Could not connect to SMTP host.
2015-11-02 20:48:33 CLIENT -> SERVER: QUIT
2015-11-02 20:48:33 SERVER -> CLIENT:
2015-11-02 20:48:33 SMTP ERROR: QUIT command failed:
2015-11-02 20:48:33 SMTP Error: Could not connect to SMTP host.

And in the PHP file, I am using the following code:

$mail->IsSMTP();
$mail->IsHTML(true);
$mail->SMTPDebug    = 2;        
$mail->Host         = 'smtp01.uni.au.dk';
$mail->Port         = 25;
$mail->SMTPAuth     = false;
$mail->SMTPSecure   = '';
$mail->AddAddress($email, 'John Smith');
$mail->SetFrom('[email protected]', 'Lotus Base');
$mail->CharSet      = "utf-8";
$mail->Subject      = "Lotus Base user message: Test message";
$mail->AltBody      = "To view the message, please use an HTML compatible email viewer."; // optional, comment out and test
$mail->MsgHTML('<body><p><strong>Message below</strong>.</p><blockquote>'.$message.'</blockquote></body>');

$mail->Send();

I have little experience with dealing with SMTP servers, so it would be nice to know if there is something I could do on my end. Thanks!

Most helpful comment

It's using opportunistic STARTTLS. You have set SMTPSecure = '', but it spots that your server speaks TLS and enables it automatically. To disable that set SMTPAutoTLS = false. Beyond that, and if you want TLS even without auth, its most likely it's failing a certificate check - try setting SMTPDebug = 4 - it will be noisier but may show you something useful.

All 9 comments

It's using opportunistic STARTTLS. You have set SMTPSecure = '', but it spots that your server speaks TLS and enables it automatically. To disable that set SMTPAutoTLS = false. Beyond that, and if you want TLS even without auth, its most likely it's failing a certificate check - try setting SMTPDebug = 4 - it will be noisier but may show you something useful.

@Synchro Thanks for the input. I have found out that setting SMTPAutoTLS to false helps, in conjunction with turning off peer verification off through the SMTPoptions.

Thanks :+1:

I'm glad it worked, but as always, I'd note that disabling verification is something you only need to do if your server is broken; the correct solution is to fix the server.

Hello, I'm the same, I just got this message: SMTP Error: Could not connect to SMTP server.
What can be

@luismartinez0518 Please don't hijack unrelated tickets. Diagnosing connection problems is covered extensively in the troubleshooting guide and on Stack Overflow.

@Synchro It's help a lot thank you!!!

sorry about my language but anyone help me with this error i do 3 step about google account

2019-06-02 02:44:03 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP c6sm21284152pfm.163 - gsmtp
2019-06-02 02:44:03 CLIENT -> SERVER: EHLO localhost
2019-06-02 02:44:04 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [2001:ee0:4041:39a7:58ac:ad2c:cb28:b9b]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-06-02 02:44:04 CLIENT -> SERVER: STARTTLS
2019-06-02 02:44:04 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2019-06-02 02:44:04 CLIENT -> SERVER: QUIT
2019-06-02 02:44:04 SERVER -> CLIENT: F
2019-06-02 02:44:04 SMTP ERROR: QUIT command failed: F
SMTP Error: Could not connect to SMTP host.
Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

here is my code php

$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 = '[email protected]';                 // SMTP username
        $mail->Password = 'xxx';                           // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, 'ssl' also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        //Recipients
        $mail->CharSet = 'UTF-8';
        $mail->setFrom('[email protected]', 'Vietpro Mobile Shop');             // Gửi mail tới Mail Server
        $mail->addAddress($user_mail);               // Gửi mail tới mail người nhận
        //$mail->addReplyTo('[email protected]', 'Information');
        $mail->addCC('[email protected]');
        //$mail->addBCC('[email protected]');

        //Attachments
        //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
        //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

        //Content
        $mail->isHTML(true);                                  // Set email format to HTML
        $mail->Subject = 'Xác nhận đơn hàng từ Vietpro Mobile Shop';
        $mail->Body    = $str_body;
        $mail->AltBody = 'Mô tả đơn hàng';

        $mail->send();
        header('location:index.php?page_layout=success');
    } catch (Exception $e) {
        echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
    }   
}

please help me :((((

Read the troubleshooting guide. It covers this exact problem (your connection is failing immediately after STARTTLS).

@Synchro où se trouve le guide ??
Moi j'ai le même problème

Was this page helpful?
0 / 5 - 0 ratings