简体   繁体   中英

SMTP connect failed - PHPMailer Error on Localhost in phpmailer

I am using phpmailer latest version . I am using the following code :

<?php
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // 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 = 'sultan.ahmed.sagor@gmail.com';                 // SMTP username
$mail->Password = 'simplePass';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to
$mail->SMTPDebug=2;
$mail->setFrom('sultan.ahmed.sagor@gmail.com', 'Mailer');
$mail->addAddress('zakir@era.com.bd', 'Joe User');     // Add a recipient
//$mail->addAddress('ellen@example.com');               // Name is optional
//$mail->addReplyTo('info@example.com', 'Information');
//$mail->addCC('cc@example.com');
//$mail->addBCC('bcc@example.com');

//$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$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';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
?>

But when I run this code I am giving the following output :

2017-02-09 11:20:34 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP w70sm17914821wrc.47 - gsmtp 
2017-02-09 11:20:34 CLIENT -> SERVER: EHLO localhost 
2017-02-09 11:20:34 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHU*KING 250 SMTPUTF8 
2017-02-09 11:20:34 CLIENT -> SERVER: STARTTLS 
2017-02-09 11:20:37 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS 
2017-02-09 11:20:38 CLIENT -> SERVER: EHLO localhost 
2017-02-09 11:20:39 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [50.118.143.87] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 
2017-02-09 11:20:39 CLIENT -> SERVER: AUTH LOGIN 
2017-02-09 11:20:39 SERVER -> CLIENT: 334 VXNlcm5hbWU6 
2017-02-09 11:20:39 CLIENT -> SERVER: xxx== 
2017-02-09 11:20:40 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 
2017-02-09 11:20:40 CLIENT -> SERVER: xxx== 
2017-02-09 11:20:40 SERVER -> CLIENT: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp 
2017-02-09 11:20:40 SMTP ERROR: Password command failed: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials w70sm17914821wrc.47 - gsmtp 
2017-02-09 11:20:40 SMTP Error: Could not authenticate. 
2017-02-09 11:20:40 CLIENT -> SERVER: QUIT 
2017-02-09 11:20:41 SERVER -> CLIENT: 221 2.0.0 closing connection w70sm17914821wrc.47 - gsmtp 
2017-02-09 11:20:41 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Message could not be sent.Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

And the email is not sent . How can I use phpmailer to send email ?

This should fix your issue:

$mail->AuthType = 'LOGIN';

https://www.emailarchitect.net/easendmail/sdk/html/authtype.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM