When I trying to send a email using PHP SMTP email server, following error has occurred- Error: Could not connect to SMTP HOST | Sololearn: Learn to code for FREE!

0

When I trying to send a email using PHP SMTP email server, following error has occurred- Error: Could not connect to SMTP HOST

<?php // 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'; $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 = '*********@gmail.com'; // SMTP username $mail->Password = '************'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 465; // TCP port to connect to //Recipients $mail->setFrom('******@gmail.com', 'Rohit Singh'); $mail->addAddress('*********@gmail.com', 'Rohit Singh'); // Add a recipient // $mail->addAddress('[email protected]'); // Name is optional // $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 = 'Inventory system'; $mail->Body = 'Hii, Rohit we are testing email system. '; $mail->AltBody = 'Hii, Rohit we are testing email system.'; $mail->send(); echo 'Email has been sent'; } catch (Exception $e) { echo 'Email could not be sent. M

2/11/2019 10:25:19 AM

Rohit Kumar Singh

3 Answers

New Answer

+2

Wrong login information?

0

Try to use port 587

0

Toni Isotalo i have already used and occurred this error SMTP Error: Could not authenticate