Eliethesaiyan
Eliethesaiyan

Reputation: 2322

PHPMailer SMTP Error: Could not authenticate using gmail as smtp server

I am failing to send email in my application hosted on appfog i am using the following code which works fine on localhost but fail on appfog. JPhpMailer extend class.PhpMailer.php

                    $mailer = new JPhpMailer(true);
                    $mailer->IsSMTP();
                    $mailer->Mailer = "smtp";
                    //$mailer->SMTPSecure == 'tls'; 
                    $mailer->Host = 'ssl://smtp.gmail.com';
                    $mailer->Port = '465';
                    $mailer->SMTPAuth = true;
                    //$mailer->SMTPSecure = true; 
                    $mailer->Username = '[email protected]';
                    $mailer->Password = 'zzzzzzz';
                    $mailer->SetFrom($to['from'], $to['from_name']); 
                    $mailer->AddAddress($to['to'],$to['to_name'] ); 
                    $mailer->Subject = $to['subject'];
                    $mailer->Body = $to['body'];
                    $mailer->Send();

here is the line that in phpMailer that fails to execute`if ($tls) { if (!$this->smtp->StartTLS()) { throw new phpmailerException($this->Lang('tls')); }

         //We must resend HELO after tls negotiation
        $this->smtp->Hello($hello);
      }

       $connection = true;
      if ($this->SMTPAuth) {
         if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
         **strong text throw new phpmailerException($this->Lang('authenticate')); **            }
      }
     }
   $index++;
    if (!$connection) {
       throw new phpmailerException($this->Lang('connect_host'));
     }

Upvotes: 2

Views: 50106

Answers (6)

Mark
Mark

Reputation: 13

In most cases you need to create a 2-Step Verification and sign in with an App password.

Small tip: you should carefully read phpmailer's debug message. There could be a proper link to an answer of the problem.

I had this one: https://support.google.com/mail/answer/7126229?visit_id=1-636190350518295662-3485238940&rd=2#cantsignin

Upvotes: 0

Arshid KV
Arshid KV

Reputation: 10037

Step 1:- Go to https://myaccount.google.com/security#signin then App passwords generate app password.

Step 2:- Paste that 16 digit password $mailer->Password

Upvotes: 0

Developer
Developer

Reputation: 3998

I encountered the same problem. To get it working, I had to go to myaccount.google.com -> "connected apps & sites", and turn "Allow less secure apps" to "ON" (near the bottom of the page).

Hope it helps some one

enter image description here

Upvotes: 4

UWU_SANDUN
UWU_SANDUN

Reputation: 1193

Print your PHPMailer object and check PORT on object and you given PORT

echo "<pre>", print_r($mailer, true);
exit;

Upvotes: 1

user851939
user851939

Reputation:

The code below is working for me :

require("phpmailer/class.phpmailer.php");

$mail = new PHPMailer();

$mail->IsSMTP();                                      // set mailer to use SMTP
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.gmail.com";  // specify main and backup server
$mail->Port = 587;
$mail->Username = "[email protected]";  // SMTP username
$mail->Password = "mypass"; // SMTP password

$mail->From = "[email protected]";
$mail->FromName = "myname";
$mail->AddAddress("[email protected]", "myname");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters
$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. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";

Upvotes: 6

Gavin
Gavin

Reputation: 6394

After signing up to appfog, I was able to get PHPMailer working with the following.

I was unable to find JPHPMailer, although I suspect that isn't the cause of your issue but the fact that you were putting ssl://smtp.gmail.com as the host.

ini_set('display_errors', 1);
error_reporting(E_ALL);

include('class.phpmailer.php');
$mailer = new PHPMailer(true);
$mailer->IsSMTP();
$mailer->SMTPSecure = 'ssl';
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 465;
$mailer->SMTPAuth = true;
$mailer->Username = '[email protected]';
$mailer->Password = 'password';
$mailer->SetFrom('[email protected]', 'Name'); 
$mailer->AddAddress('[email protected]'); 
$mailer->Subject = 'This is a test';
$mailer->Body = 'Test body';
$mailer->Send();

Hope this helps?

Upvotes: 1

Related Questions