Error sending mail with Phpmailer

Hello please I need your help to send mail with PHPMailer, when I send the email I am told SMTP connect () failed

Can you please share some more information about how you configured PHPmailer? Relevant information includes:

Good evening here is my configuration
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Load Composer’s autoloader
require ‘vendor/autoload.php’;

// Instantiation and passing true enables exceptions
$mail = new PHPMailer(true);

try {
//Server settings
$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtp.infinityfree.net’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘[email protected]’; // SMTP username
$mail->Password = ‘mypassword123’; // SMTP password
$mail->SMTPSecure = ‘tsl’; // Enable TLS encryption, ssl also accepted
$mail->Port = 587; // TCP port to connect to

//Recipients
$mail->setFrom('[email protected]', 'Mailer');
$mail->addAddress($email, '');

Here is the error that occurs after sending the email

2019-04-01 21:11:41 Connection: opening to smtp.infinityfree.net:587, timeout=300, options=array()
2019-04-01 21:12:44 Connection failed. Error #2: stream_socket_client(): unable to connect to smtp.infinityfree.net:587 (Connection timed out) [/home/vol2_6/epizy.com/epiz_23507031/htdocs/vendor/phpmailer/phpmailer/src/SMTP.php line 326]
2019-04-01 21:12:44 SMTP ERROR: Failed to connect to server: Connection timed out (110)
SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
Message could not be sent. Mailer Error: SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub

The reason you can’t send email is because smtp.infinityfree.net does not exist. We don’t provide an SMTP service for free hosting. You can use SMTP from your website, but you will need to use a different SMTP service for that. For example, you could use Gmail: https://infinityfree.net/support/how-to-send-email-with-gmail-smtp/

Good evening Admin Thank you very much the mail works on my local server. but the problem is that I can not connect to my FTP File Manger to test the file.
here is the mistake that when I try to access Error! The authentication operation time out

Hello admin, the connection on ftp is functional now, I try to download the file containing the php mailer script in the ftp file manager but I still have the same problem, here is the script of the file I downloaded:

<?php

date_default_timezone_set(‘Etc/UTC’);

// Edit this path if PHPMailer is in a different location.

require ‘./PHPMailer/PHPMailerAutoload.php’;

$mail = new PHPMailer;

$mail->isSMTP();

/*

  • Server Configuration

*/

$mail->Host = ‘smtp.gmail.com’; // Which SMTP server to use.

$mail->Port = 587; // Which port to use, 587 is the default port for TLS security.

$mail->SMTPSecure = ‘tls’; // Which security method to use. TLS is most secure.

$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.

$mail->Username = “[email protected]”; // Your Gmail address.

$mail->Password = “123456789”; // Your Gmail login password or App Specific Password.

/*

  • Message Configuration

*/

$mail->setFrom(‘[email protected]’, 'Mailer '); // Set the sender of the message.

$mail->addAddress('[email protected], ‘my name’); // Set the recipient of the message.

$mail->Subject = ‘PHPMailer GMail SMTP test’; // The subject of the message.

/*

  • Message Content - Choose simple text or HTML email

*/

// Choose to send either a simple text email…

$mail->Body = ‘Message envoyer avec php mailer’; // Set a plain text body.

// … or send an email with HTML.

//$mail->msgHTML(file_get_contents(‘contents.html’));

// Optional when using HTML: Set an alternative plain text message for email clients who prefer that.

//$mail->AltBody = ‘This is a plain-text message body’;

// Optional: attach a file

$mail->addAttachment(‘images/phpmailer_mini.png’);

if ($mail->send()) {

echo “Your message was sent successfully!”;

} else {

echo "Mailer Error: " . $mail->ErrorInfo;

}

?>

here is the error that occurs after sending mail
Mailer Error: SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
thank you in advance

The first thing I noticed is missing ' and the wrong address (ends with L)
image

also include debug mode in your code https://github.com/PHPMailer/PHPMailer/wiki/SMTP-Debugging

I remove the error that was at the recipient’s email address, but it always comes back to the same thing.

/*

  • Server Configuration

*/

$mail->SMTPDebug = SMTP::DEBUG_SERVER;

$mail->SMTPDebug = 2;

$mail->Host = ‘smtp.gmail.com’; // Which SMTP server to use.

$mail->Port = 465; // Which port to use, 587 is the default port for TLS security.

$mail->SMTPSecure = ‘ssl’; // Which security method to use. TLS is most secure.

$mail->SMTPAuth = true; // Whether you need to login. This is almost always required.

$mail->Username = “[email protected]”; // Your Gmail address.

$mail->Password = “myadressmailpassword”; // Your Gmail login password or App Specific Password.

/*

  • Message Configuration

*/

$mail->setFrom(‘[email protected]’, 'Mailer '); // Set the sender of the message.

$mail->addAddress(‘[email protected]’, ‘my name’); // Set the recipient of the message.

$mail->Subject = ‘PHPMailer GMail SMTP test’; // The subject of the message.

I even add the the smtpdebug here is the error code that appears.

Could not access file: images/phpmailer_mini.png
2019-04-05 17:54:47 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP x14sm4017644wmj.3 - gsmtp
2019-04-05 17:54:47 CLIENT -> SERVER: EHLO www.sitename.rf.gd
2019-04-05 17:54:47 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [185.27.134.185]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2019-04-05 17:54:47 CLIENT -> SERVER: AUTH LOGIN
2019-04-05 17:54:47 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2019-04-05 17:54:47 CLIENT -> SERVER: c2RtbTAxOUBnbWFpbC5jb20=
2019-04-05 17:54:47 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2019-04-05 17:54:47 CLIENT -> SERVER: NjYwNTc2NTIy
2019-04-05 17:54:47 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 Can't sign in to your Google Account - Gmail Help x14sm4017644wmj.3 - gsmtp
2019-04-05 17:54:47 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 Can't sign in to your Google Account - Gmail Help x14sm4017644wmj.3 - gsmtp
SMTP Error: Could not authenticate.
2019-04-05 17:54:47 CLIENT -> SERVER: QUIT
2019-04-05 17:54:47 SERVER -> CLIENT: 221 2.0.0 closing connection x14sm4017644wmj.3 - gsmtp
SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
Mailer Error: SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub

The debug output seems useful. This line caught my attention:

535-5.7.8 Username and Password not accepted. Learn more at535 5.7.8 https://support.google.com/mail/?p=BadCredentials

Can you please double check that your username and password are correct?

Also, if you’re using Two Factor Authentication on your account, you need to use an Application Specific Password to login instead: Sign in with app passwords - Google Account Help

If you don’t have Two Factor, then you may need to enable access for insecure applications to use SMTP: Less secure apps & your Google Account - Google Account Help

thank you very much the problem was at the authentication level, the password was incorrect

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.