3

Following is the code that I am using for sending mail using PHPMailer but getting error.

 public function changePassword(){
   if($this->request->is('post')){
   require_once(ROOT .DS. 'vendor' . DS  . 'PHPMailer' . DS .  'class.phpmailer.php');
   $email = 'abc@gmail.com';
   $mail = new PHPMailer(); // create a new object
   $mail->IsSMTP(); // enable SMTP
   $mail->isHTML(true);
   $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
   $mail->SMTPAuth = true; // authentication enabled
   $mail->SMTPSecure = 'TLS'; // secure transfer enabled REQUIRED for Gmail
   $mail->Host = "smtp.gmail.com";
   $mail->Port = 587; // or 587
   $mail->IsHTML(true);
   $mail->Username = "xyz@gmail.com";
   $mail->Password = "dead_gone";
   $mail->SetFrom("xyz@gmail.com");
   $mail->AddAddress($email);
   $mail->Subject = "password recovery";
   $mail->Body = "your password is:- sdx_12345  click here to log in <a href ='http://localhost/cake/logins'> click here  </a>  ";

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

}

ERROR:

SMTP -> ERROR: AUTH not accepted from server: 530 5.7.0 Must issue a STARTTLS command first. v3sm8416695par.17 - gsmtp SMTP Error: Could not authenticate. Mailer Error: SMTP Error: Could not authenticate.

Rupinder Kaur
  • 311
  • 5
  • 20

3 Answers3

1

This property is case-sensitive:

$mail->SMTPSecure = 'tls'

It would help if you based your code on the gmail example provided with PHPMailer - it's not as if you're the first to connect to gmail!

Synchro
  • 29,823
  • 14
  • 69
  • 85
  • Solved it by turning on Allowing less secure apps to access your account option in gmail. – Rupinder Kaur Apr 07 '16 at 09:14
  • This is a non-sequitur: enabling access for less secure apps will not solve `Must issue a STARTTLS`. It may solve an entirely different problem, as covered in [the PHPMailer docs](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting), but that's not the question you asked. – Synchro Apr 07 '16 at 09:18
  • After changing the TLS to tls got the error of unauthorized access. – Rupinder Kaur Apr 07 '16 at 09:24
  • I know you did, but it's still not relevant to the question you asked. Imagine someone reading this question and seeing your comment - they would think that enabling less secure apps would solve the `STARTTLS` problem, which it does not. – Synchro Apr 07 '16 at 09:28
  • Seriously i didnot issue any STARTTLS command. Implemented your solution and turned on the enabling less less secure apps. It worked in my case. Not saying that it is perfect solution. – Rupinder Kaur Apr 07 '16 at 10:12
  • Setting `SMTPSecure = 'tls'` means that PHPMailer will issue a STARTTLS command to enable encryption. That's what it's for, and is how SMTP+STARTTLS encryption works. – Synchro Apr 07 '16 at 10:42
0

try a SSL connection over the port 465 and look at you security settings in you account from google

-2

You should also put

$mail->IsHTML(true);

after you set your body and attachments.

Edit:

It also might be possible that TLS has to be lowercase.

Noneatme
  • 144
  • 3
  • 11
  • i have down voted because $mail->IsHTML(true); is already there in function body and question is not about the setting of html. – Vishnu Sharma Apr 07 '16 at 06:00
  • yes, but at the wrong location. He have to set it under the $mail->Body() method to avoid problems. See http://stackoverflow.com/a/16256464/5230497. It's also a recommendation. – Noneatme Apr 07 '16 at 06:13
  • It doesn't matter where it's set. Once is enough, before is fine. – Synchro Apr 07 '16 at 06:16
  • do you think is it really matters?Anyways you can put this as comment not as an answer. – Vishnu Sharma Apr 07 '16 at 06:17
  • Yes it matters, because some mail servers tend to reject mails with a bad / malformed content / without content – Noneatme Apr 07 '16 at 06:20
  • That has nothing to do with what you suggested. Read the code, don't just make baseless assertions. – Synchro Apr 07 '16 at 06:46