0

I tried to send email from my localhost using my gmail account. I also tried to find answers in the web and even in this website but nothing helped me. I am using XAMPP.

This is the code:

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer();         // create a new object
$mail->IsSMTP();                 // enable SMTP
$mail->SMTPDebug = 2;            // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true;          // authentication enabled
$mail->SMTPSecure = 'ssl';       // secure transfer enabled REQUIRED for Gmail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;               // or 587
$mail->IsHTML(true);
$mail->Username = "myUser@gmail.com";
$mail->Password = "myUserPass";
$mail->SetFrom("myUser@gmail.com");
$mail->Subject = "Test";
$mail->Body = "hello";
$mail->AddAddress("OtherUser@gmail.com");

if(!$mail->Send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message has been sent";
}

The full error which I got:

2017-04-03 10:33:15 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 CLIENT -> SERVER: EHLO localhost 2017-04-03 10:33:15 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [46.117.89.127] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH 250-ENHANCEDSTATUSCODES 250-PIPELINING 250-CHUNKING 250 SMTPUTF8 2017-04-03 10:33:15 CLIENT -> SERVER: AUTH LOGIN 2017-04-03 10:33:15 SERVER -> CLIENT: 334 VXNlcm5hbWU6 2017-04-03 10:33:15 CLIENT -> SERVER: bXlVc2VyQGdtYWlsLmNvbQ== 2017-04-03 10:33:15 SERVER -> CLIENT: 334 UGFzc3dvcmQ6 2017-04-03 10:33:15 CLIENT -> SERVER: bXlVc2VyUGFzcw== 2017-04-03 10:33:15 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 p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 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 p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:15 SMTP Error: Could not authenticate. 2017-04-03 10:33:15 CLIENT -> SERVER: QUIT 2017-04-03 10:33:16 SERVER -> CLIENT: 221 2.0.0 closing connection p7sm17594308wrc.19 - gsmtp 2017-04-03 10:33:16 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

I tried to delete that:

$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail

Or changing the port to 25/587 but it did not help. Does someone know how to fix that?

Parfait
  • 104,375
  • 17
  • 94
  • 125
Orih90
  • 77
  • 1
  • 3
  • 9
  • set `$mail->SMTPDebug = 1` it will give the error no, which will help you to debug – Dipanwita Kundu Apr 03 '17 at 10:46
  • No it won't. `$mail->SMTPDebug = 2` shows server side errors, which the OP already did. – Synchro Apr 03 '17 at 13:46
  • Read the Google links and the troubleshooting guide that the error message links to. Also, search before posting - this has been asked many times before. – Synchro Apr 03 '17 at 13:48
  • 1
    Possible duplicate of ["SMTP Error: Could not authenticate" in PHPMailer](http://stackoverflow.com/questions/3949824/smtp-error-could-not-authenticate-in-phpmailer) – Synchro Apr 03 '17 at 13:49
  • updated code and text formatting for readability – Parfait Apr 03 '17 at 19:30

2 Answers2

1

Assuming that your username and password entered is correct, you need to authorize the external application use of gmail.

Follow the next steps and authorize to make it:

  1. First, go to your Google Account Management page
  2. Under the Connected apps & sites, click Allow less secure apps: ON

In case it does not work, if your gmail account has 2-step verification enabled, use an auto-generated password.

Update:

Although turning Allow less secure apps: ON might solve your problem, it should be taken as a troubleshooting tip and not as the final solution.

Hyder B.
  • 10,900
  • 5
  • 51
  • 60
  • Thank you so much! – Orih90 Apr 03 '17 at 11:07
  • This will "fix" the reported problem, but you do not need to disable secure apps to use PHPMailer with Gmail. Read the docs. – Synchro Apr 03 '17 at 13:47
  • 2
    @Synchro just saying 'read the docs' does not help anyone in improving answers and the gmail docs DOES say to disable secure apps as a troubleshoot tip. https://support.google.com/mail/answer/7126229?visit_id=1-636268261334269899-3579076150&rd=1#cantsignin – Hyder B. Apr 03 '17 at 14:24
  • It does when the docs explicitly cover this exact situation, and they are linked directly from the error message itself that the OP posted. Clicking a link really isn't that hard... Disabling secure apps may be a *troubleshooting* step, but it's not a proper solution; it just lets you confirm where the problem is so you can fix it properly. It's like disabling TLS verification; useful tool, unsuitable for production. – Synchro Apr 03 '17 at 14:29
  • Ok, you got a point. Updated my answer with a word of caution. – Hyder B. Apr 03 '17 at 14:57
0

you should turn off Email protected in your Anti Virut or Firewall

vinh hoang
  • 39
  • 2