PHP Mailer - "Could not connect to SMTP host"

Asked

Viewed 2,044 times

1

I am trying to send an email using Phpmailer, but without success.

My code is this:

<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.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 = 'minhasenha123';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');     // Add a recipient

    //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. Mailer Error: ', $mail->ErrorInfo;
}
?>

The error message I receive is as follows:

2018-02-18 16:22:22 SERVER -&gt; CLIENT: 220 smtp.gmail.com ESMTP k2sm16846924qtk.60 - gsmtp<br> 2018-02-18 16:22:22 CLIENT -&gt; SERVER: EHLO localhost<br> 2018-02-18 16:22:23 SERVER -&gt; CLIENT: 250-smtp.gmail.com at your service, [2804:431:b705:30c:3c68:a6f0:fd8e:b468]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8<br> 2018-02-18 16:22:23 CLIENT -&gt; SERVER: STARTTLS<br> 2018-02-18 16:22:23 SERVER -&gt; CLIENT: 220 2.0.0 Ready to start TLS<br> SMTP Error: Could not connect to SMTP host.<br> 2018-02-18 16:22:23 CLIENT -&gt; SERVER: QUIT<br> 2018-02-18 16:22:23 <br> 2018-02-18 16:22:23 <br> SMTP Error: Could not connect to SMTP host.<br> Message could not be sent. Mailer Error: SMTP Error: Could not connect to SMTP host.

Note: I made several changes, including some suggested in certain topics here, but without success. Someone has already gone through something similar?

Note 2: I have already enabled permission for less reliable applications in gmail.

Edit: There are several topics talking about the problem, however, none of these worked for my case. I added the solution to the answers.

3 answers

2

Change the door to 465. Ex:

$mail->Port = 465;
  • 1

    But it is necessary to have SSL --> https://support.google.com/a/answer/176600?hl=pt-BR

  • Thanks friend, I tried here but it didn’t work. Now the error is as follows: Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

2


I found the solution by adding the following excerpt:

$mail->SMTPOptions = array(
'ssl' => array(
    'verify_peer' => false,
    'verify_peer_name' => false,
    'allow_self_signed' => true
 )
);
  • This just turns off the security, which is really something that can be a problem... your problem is either with the SSL extension that is inactive, or with the version of your PHP or with the version of phpmailer that you are using (most problems are that people download phpmailer from unofficial links and so end up using obsolete versions)

  • I downloaded Phpmailer via Composer, command taken from Github, so I think this is the most current version. But thank you for this information friend, I believe will be useful.

  • Does the error occur only on your localhost or does it occur on your site as well? Does it occur on both? Send information of your environment that is failing, for this use the phpinfo();

  • For now I am working on the site running it localhost via WAMP Guilherme, I have not tested put on the air.

  • Then post phpinfo content, it can be the version of PHP or openssl that is inactive.

  • I can’t send it through here, it’s a very long text.

Show 2 more comments

1

The problem is that Google (for security reasons) is blocking access to the account through its application.

To free up access, do the following:

1. Log into your Gmail account

2. Access https://accounts.google.com/b/0/DisplayUnlockCaptcha

3. Click on Carry on

Wait a few seconds and run a new test and your Phpmailer should work. You can send both by ssl (port 465) how much per tls (gate 587).

Edit

After discussion in the comments, it was verified that the above solution applies to authorization problems and not connection with Gmail smtp. However the above solution will be published if someone has similar problem, because there may be some confusion between one thing and another.

The problem of the question was solved from of this reply in Soen and answered by AP in this answer.

  • Thanks for the reply friend. I tried this, but the error persists.

  • I tested it on Windows server and it worked great. Are you using Linux? You have access to php.ini?

  • I am using Windows server (WAMP). Yes, I have access to php.ini

  • Yes, it is normal.

  • Man, thank you so much, in this link you sent I found the solution!!!!

  • Tell me what it was so I can add to the answer.

  • I just added.

  • Leave her, it might be useful to someone.

  • Blz, I’ll leave then. Abs!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.