Phpmailer: "Could not connect to SMTP host" after STARTTLS

Created on 24 Dec 2016  ·  5Comments  ·  Source: PHPMailer/PHPMailer

I can't seem to get the right configuration to send mail with PHPMailer. It connects to the server, but after STARTTLS, it comes back and says it can't connect to the server. My code is:

<?php
// some includes 

    $mail = new PHPMailer();

    $mail->isSMTP();
    $mail->Host = 'myhost.com';
    $mail->Port = 587;
    $mail->SMTPAuth = false;
    $mail->Username = '[email protected]';
    $mail->Password = 'password';
    $mail->SMTPDebug = 2;
//    $mail->SMTPAutoTLS = false;
//    $mail->SMTPSecure = 'tls';

    $mail->Sender = '[email protected]';
    $mail->From = '[email protected]';
    $mail->FromName = 'Giant Frying Pan';

    $mail->AddAddress('[email protected]');
    $mail->Subject = 'Test Email';
    $mail->Body = 'Body of Test Email';

    if (!$mail->Send()) {
        echo "Mailer Error: " . $mail->ErrorInfo . PHP_EOL;
    }

What comes back is:

2016-12-24 19:34:06 SERVER -> CLIENT: 220 myhost.com ESMTP Postfix
2016-12-24 19:34:06 CLIENT -> SERVER: EHLO devsite.dev
2016-12-24 19:34:06 SERVER -> CLIENT: 250-myhost.com
                                      250-PIPELINING
                                      250-SIZE 10240000
                                      250-ETRN
                                      250-STARTTLS
                                      250-ENHANCEDSTATUSCODES
                                      250-8BITMIME
                                      250 DSN
2016-12-24 19:34:06 CLIENT -> SERVER: **STARTTLS**
2016-12-24 19:34:06 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2016-12-24 19:34:06 **SMTP Error: Could not connect to SMTP host.**
2016-12-24 19:34:06 CLIENT -> SERVER: QUIT
2016-12-24 19:34:06 SERVER -> CLIENT: M
IA�;�l�d��Am��m��K�lX�wYv�K�z�����
                                      ;>��u��A2��S\�H,��1�����-u��y��Jˡ�]��;E�Q��>8w��N�E})���$�v}�:�=�
�όk
(
ǧ�  U1�q��ߠ�ژ�WO
                                      �Zuk�������Xo��)��q��t���%}l
m_���][��5ٹ�In._�V�TŨ��i��ܢ�!-�v�R
�l�]b�A�SaR֡~-t:v�V�a��c�ЪE��7�3�\')�s��RE��Ի������Y�X���W��������Tm8�8!�D�b��d$p�O��n��W
2016-12-24 19:34:06 SMTP ERROR: QUIT command failed: M
IA�;�l�d��Am��m��K�lX�wYv�K�z�����
                                      ;>��u��A2��S\�H,��1�����-u��y��Jˡ�]��;E�Q��>8w��N�E})���$�v}�:�=�
�όk
(
ǧ�  U1�q��ߠ�ژ�WO
                                      �Zuk�������Xo��)��q��t���%}l
m_���][��5ٹ�In._�V�TŨ��i��ܢ�!-�v�R
�l�]b�A�SaR֡~-t:v�V�a��c�ЪE��7�3�\')�s��RE��Ի������Y�X���W��������Tm8�8!�D�b��d$p�O��n��W
2016-12-24 19:34:06 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I've tried turning authentication off and on, changing the security settings, etc. Any ideas where I should look next? (I can send and receive mail on this server using a mail client)

Most helpful comment

Hello, here is what I found on the net 👍

$mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );

All the best

All 5 comments

The troubleshooting guide includes some info about how to test SSL config using the openssl command-line client. See what that says.

Being a n00b for SMTP, I didn't realize TLS and SSL were related. I ran test from WIKI and found that it was a self-signed certificate. I added options to allow it and it now works. Looks like I need to resolve the certificate issue.

Thanks, and thanks for PHPMailer!

Hello, here is what I found on the net 👍

$mail->SMTPOptions = array(
                    'ssl' => array(
                        'verify_peer' => false,
                        'verify_peer_name' => false,
                        'allow_self_signed' => true
                    )
                );

All the best

Oh Oh just out of shear curiosity, just cause Im curious, what were the settings that you changed.
"I ran test from WIKI and found that it was a self-signed certificate. I added options to allow it and it now works."

HAHAHAHAHAHA, I TRY FOR 3 DAYS TO SEND ONE EMAIL FROM GMAIL PROGRAMATICALLY WITH PHP.

HAHA, DO I WANT TO PROGRAM FOR THE REST OF MY LIFE/

OF COURRRRRRRRSE HAHAHAHAHAHAHAHAHAHA./ i LOVE ESPECIALLY THE COMMUNITY'S HELP WITH PROBLEMATIC QUESTIONS, THEYRE SOOOOO UNDERSTANDING AND FORGIVING . THEY ALWAYS KEEP IN MIND THAT THEY WERE ALSO ONCE NEW

HAHAHAHHAA

YES I LOVE LIFE.

@mantissefr You saved my day! Cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

velocius picture velocius  ·  4Comments

Synchro picture Synchro  ·  5Comments

webmasterHPCR picture webmasterHPCR  ·  3Comments

wilon picture wilon  ·  4Comments

weirdeagle picture weirdeagle  ·  3Comments