Phpmailer + gmail

Asked

Viewed 1,295 times

0

I am using the latest version of Phpmailer to fire e-mail using my Gmail account, this application runs locally, below my code (copied from Phpmailer’s own Github talking about sending to Gmail):

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "[email protected]";
$mail->Password = "xx";
$mail->setFrom('[email protected]', 'First Last');
$mail->addAddress('[email protected]', 'John Doe');
$mail->Subject = 'PHPMailer GMail SMTP test';
$mail->Body = 'This is a plain-text message body';
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

But of error, this one:

2018-06-29 20:15:09 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP d18-v6sm6204628qtl.32 - gsmtp
2018-06-29 20:15:09 CLIENT -> SERVER: EHLO localhost
2018-06-29 20:15:09 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [189.120.238.241]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250 SMTPUTF8
2018-06-29 20:15:09 CLIENT -> SERVER: STARTTLS
2018-06-29 20:15:09 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
SMTP Error: Could not connect to SMTP host.
2018-06-29 20:15:10 CLIENT -> SERVER: QUIT
2018-06-29 20:15:10 SERVER -> CLIENT: 
2018-06-29 20:15:10 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

What else can I try?

PS: enabling less secure applications is already marked ON in Gmail

  • 1

    Remembering that you don’t need to enable less secure applications, Phpmailer already supports Xoauth2 for a while (since 5.2.11). Using this stream, the account owner authorizes your application, which appears in the list of connected apps in your account. https://github.com/PHPMailer/PHPMailer/wiki/Using-Gmail-with-XOAUTH2

1 answer

1

The main error message is this:

SMTP Error: Could not connect to SMTP host.

This can occur for several reasons:

  • Your server/hosting network is blocking access to smtp.gmail.com
  • Some firewall or proxy are preventing you from communicating with smtp.gmail.com
  • Some proxy is preventing your network (if the test is local) from using secure connections

However the most likely problem is that your hosting or local PHP is not with the SSL extension enabled, IE PHP will only be able to communicate with unsafe connections, to fix this problem go to php.ini and uncomment that line (if it’s PHP 5 and 7.1):

Windows (and windows server):

;extension=php_openssl.dll

Leaving so:

extension=php_openssl.dll

Linux:

;extension=openssl.so

Leaving so:

extension=openssl.so

If it is php 7.2, both linux and windows and Mac the line will be just like this:

;extension=openssl

Uncompromisingly:

extension=openssl

Then save the document and restart Apache (or Lightttpd or Nginx) and Fastcgi (if your server uses this, it usually restarts itself, but it depends on how you set it up).

Then create a file called phpinfo.php and put this:

<?php
var_dump(extension_loaded('openssl'));

So notice if this appeared:

bool(true)

If it turns up:

bool(false)

Is because you forgot something, or edited the php.ini wrong (some servers have more than one for different things).

  • Thanks for the answer, come on, here from bool(true); (ie, it’s all right). I am testing locally, without any kind of firewall on my machine, but the error remains the same, with any Gmail account.... where is the error?

  • @caiocabale you have or have not activated the openssl extension?

  • Yes, it is active!

  • @caiocafardo create an empty php file named test.php and put it in it <?php fsockopen("tls://smtp.gmail.com", 587, $errno, $errstr, 30); var_dump($errno, $errstr); and tell me what returns.

Browser other questions tagged

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