2

When I trying to send a email using PHP SMTP email server, following error has occurred.

SMTP Error: Could not authenticate. Message could not be sent.

Mailer Error: SMTP Error: Could not authenticate.


Following is my code that I have used.

function supervisorMail(){

global $error;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->Username = "***@gmail.com";
$mail->Password = "****";
$mail->SetFrom("***@gmail.com", "Employee Leave Management System");

$userID=$_SESSION['userID'];

$select_query = mysql_query("SELECT * FROM employee WHERE emp_id = '$userID'");
$select_sql = mysql_fetch_array($select_query);
$name=$select_sql['manager_name'];
var_dump($name);

$select_query1 = mysql_query("SELECT email FROM employee WHERE emp_id='$name'");
$select_sql1 = mysql_fetch_array($select_query1);
$email=$select_sql1['email'];
    var_dump($email);

$mail->Subject = "subject ";
$mail->Body = "something.";
$mail_to = $email;
$mail->AddAddress($mail_to);

if(!$mail->Send())
{
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

echo "Message has been sent";

}

How can I fixed this error.

Chathurika
  • 419
  • 2
  • 6
  • 18

6 Answers6

8

The error message is very clear "Could not authenticate". I would say you are correctly using the PHPMailer class, but just not filling in the correct gmail account details.

I suppose that in your question the fields username and password look like

$mail->Username = "@gmail.com";
$mail->Password = "";

just because you, obviously, don't want to share them, I would suggest to present them like this in the question

$mail->Username = "********@gmail.com";
$mail->Password = "********";

So if you are using the correct username and password there are other two things to check

  1. Maybe your setting "Access for less secure apps" is turned off. After you login from the web you can turn it on from this page https://www.google.com/settings/u/0/security/lesssecureapps

  2. If that is On, it might be possible you have 2-Step Verification enabled in your gmail account. If this is the case, you need to create an App specific password.

Follow this link for a step by step guide on how to do it http://email.about.com/od/gmailtips/fl/How-to-Get-a-Password-to-Access-Gmail-By-POP-or-IMAP.htm

Igor S Om
  • 735
  • 3
  • 12
  • I added correct username & password, but still remain the problem – Chathurika May 30 '16 at 15:00
  • @Chathurika I'll try with my own account credentials and see what happen... will let you know soon – Igor S Om May 30 '16 at 15:02
  • It works for me on php 5.4, but in php 5.5 or higher the connection fails. What is your php version? Have you enable POP connection on your gmail settings (this will also enable smtp) – Igor S Om May 30 '16 at 15:34
  • I also used php version 5.4 – Chathurika May 30 '16 at 15:37
  • I didn't used enable POP connection, how does it effect? – Chathurika May 30 '16 at 15:38
  • can you please edit your question and put in your entire script as it is, just replace account credentials with some ***, this might help to see if there is anything to fix. Also check in your gmail setting in Forwarding and POP/IMAP if pop is enabled – Igor S Om May 30 '16 at 15:41
  • I have edited my all script of send mail, let me know if there is any problem with this – Chathurika May 30 '16 at 15:46
  • I executed your script, just replacing the credentials, commenting out the db queries and replacing the $email variable with a string and it worked correctly. I might suggest you to set error_reporting(E_ALL); ini_set('display_errors', 1); and also $mail->SMTPDebug = 2; for further debugging – Igor S Om May 30 '16 at 15:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113342/discussion-between-igor-s-om-and-chathurika). – Igor S Om May 30 '16 at 15:57
4

Please unable your less secure app and then Try to comment this line

//$mail->IsSMTP();

I hope it will work!

Barshi
  • 191
  • 2
  • 6
2

You may try to use google app password for authentication:

https://support.google.com/accounts/answer/185833?hl=en

  1. 2-Step-Verification & 2. App Passwords

enter image description here

bharat
  • 1,762
  • 1
  • 24
  • 32
1

As mentioned in comment, you need to enter valid gmail id and password.

Please refer below demo code.

<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$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 = '*****001@gmail.com';  // SMTP username
$mail->Password = 'ma****02';    // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom('ravi******@gmail.com', 'Mailer'); // Add set from id
$mail->addAddress('er.ravihirani@gmail.com', 'Ravi Hirani');     // 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';
}
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
0

I ran into this error message and wanted to post an answer in case it can help someone else. I have phpmailer installed with composer and am running php 7. The implementation of the phpmailer script that threw the error is in a php object method. The password variable, set in a config file elsewhere, needed to be declared as a global in the object context otherwise it did not contain the actual password and thus caused the authentication error..

        global $password; // <- this added to make script work

        $mail = new PHPMailer(true);   
        try {
            $mail->isSMTP();            

            // Set mailer to use SMTP               
            $mail->Host = '*******.com'; 
            $mail->SMTPAuth = true;   
            $mail->Username = 'no-reply@*******.com'; 
            $mail->Password = $password;     
            $mail->SMTPSecure = 'tls';      
            $mail->Port = 587;        
            $mail->setFrom('no-reply@*******.com', '******* - DO NOT REPLY'); 

            //$mail->isHTML(true);  
            $mail->AddAddress($user->email);

            $mail->Subject = "*******";
            $mail->Body    = "*******";

            $mail->send();

        } catch (Exception $e) {
            echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
        } 
Mike Volmar
  • 1,927
  • 1
  • 22
  • 31
0

In my case it was because I didn't turn on the Less secure app access from here: https://myaccount.google.com/lesssecureapps

Victor Rusu
  • 107
  • 1
  • 1
  • 9