0

I am composing a email function for sending newsletters to many recipients in PHP.

<?php

  require_once('class.phpmailer.php');
  require_once('class.smtp.php');

  $mailer = new Mailer();
  Class Mailer{

    function __construct(){
      $dbCon = mysqli_connect("localhost", "root", "", "dbppa");
    }



    public function sendMail($e,$message,$subject){
        $mail = new PhpMailer;

        $body = $message;

        $subject = "PPA Newsletter";
        $clientName = $e;
        $mail->SMTPDebug = 2;
        $mail->CharSet = 'utf-8';
        $mail->isSMTP();
        $mail->SMTPAuth = true;
        $mail->Host = 'tls://smtp.gmail.com:587';
        $mail->Port = 587;
        $mail->Username = "myemail@mail.com";
        $mail->Password = "password";
        $mail->SMTPSecure = 'tls';
        $mail->setFrom("myemail@mail.com","PPA Administrator");
        $mail->addReplyTo('myemail@mail.com', 'PPA Admin');
        $mail->Subject = $subject;
        $mail->AltBody = "This is the official news letter from PPA ";
        $mail->Body = $body;
        $address = $e;
        $mail->AddAddress($address, $clientName);

        if(!$mail->Send()){
            echo "NOT SENT";

        }
        else{
            echo "SENT";


        }


    }
  }//class mailer

?>

In the code given above. I provided correct email username and email password. From Gmail. But it does not send the given email message to the recipient(s). It always return an ErrorInfo of SMTP Error:

Could not authenticate. SMTP connect() failed. 

Why? Please help.

tinlyx
  • 18,900
  • 26
  • 81
  • 148
  • 1
    For an example of how to send to a list efficiently, look at [the mailing list example provided with PHPMailer](https://github.com/PHPMailer/PHPMailer/blob/master/examples/mailing_list.phps). You're probably running into a well-known issue with gmail, so read [the troubleshooting guide](https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting). – Synchro Mar 27 '16 at 19:08

1 Answers1

-1

You can use an old version of phpmailer, it will work with your code. I use version 5.2.8.

Artjom B.
  • 58,311
  • 24
  • 111
  • 196
Carlos
  • 1
  • 1