I've been using PHPMailer for a long time, sending emails using Google credentials (login and password) by enabling the "Less secure apps"
option in Google settings.
Google is notifying that after May 30, 2022, Google will not allow the use of "less secure apps"
to send emails.
Less secure apps and your Google Account
Is there another way to send emails using Gmail SMTP from PHPMailer without using the "Less Secure Applications"
feature?
P粉1279012792023-10-25 12:56:10
This method works for me, you can try this:
P粉3990907462023-10-25 11:02:17
For reference, please visit GitHub:
[https://github.com/shahhussain305/esdn_classes][1] /* To use this new method of PHPMailer follow the following steps on ubuntu: Step 1: Install Composer $ sudo apt update sudo apt install php-cli unzip $ cd ~ $ curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php $ HASH=`curl -sS https://composer.github.io/installer.sig` $ php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" Output: Installer verified **Note**: If the output says Installer corrupt, you’ll need to repeat the download and verification process until you have a verified installer. * The following command will download and install Composer as a system-wide command named composer, under /usr/local/bin: $ sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer Output All settings correct for using Composer Downloading... Composer (version 2.3.5) successfully installed to: /usr/local/bin/composer Use it: php /usr/local/bin/composer To test your installation, run: $ composer
Step 2: Go to the website root directory and run the following command
$ composer require phpmailer/phpmailer */ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; class Communicator{ /* to send emails from your registered domain / hosting email server private $host = "smtp.hostinger.com"; private $userName="no-reply@example.com"; private $robots = array( 'no-reply@example.com', 'no-reply1@example.com', 'no-reply2@example.com', 'no-reply3@example.com', 'no-reply4@example.com'); private $password="jashdf &_isdf807%^$^sdf;_sdf*)465"; public $from_email='no-reply@example.com'; */ //----------Send Email From GMAIL SMTP Server -----------------// /* To activate sending emails from gmail, please do the following: 1- login to your gmail account and go to the manage accounts (Top right under the user name logo) 2- click on the security (left side menus) 3- Activate the two step varification 4- under the two step varification, click on the app password, click on the first dropdwon and select Custom name and Type any name like PHPMailer etc */ private $host = "smtp.gmail.com"; private $userName="abcd1@gmail.com"; private $password="ywhstefdirkdfhen"; private $robots = array( array('abcd1@gmail.com','ywhstefdirkdfhen'), array('abcd2@gmail.com','oejrgdbeygswjhdo'), array('abcd3@gmail.com','poswkfhrusnfhwlv'), array('abcd4@gmail.com','tgfdeswavfgdertk'), array('abcd5@gmail.com','polkhytrdwesaqzx'), array('abcd6@gmail.com','okljuhbgtrdfcxse') ); public $from_email='abcd1@gmail.com'; //------------------------------------------------------// public $replyTo="abcd1@gmail.com"; var $tempVar; public $from_name = 'NAME OF YOUR ORGANIZATION'; public $charSet = "CharSet = 'UTF-8'"; public $charSetOpt = 0; //function to send email simple and with attachements public function sendEmail($to,$from,$sender_name="",$subject,$body,$attachement_path="",$cc="",$bcc=""){ require '../../../../vendor/autoload.php'; $mail = new PHPMailer; $mail->IsSMTP(); // set mailer to use SMTP i.e. smtp1.example.com;smtp2.example.com $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->Host = $this->host; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $this->userName; // SMTP username i.e email id of an email address $mail->Password = $this->password; // SMTP password for the specified email address $mail->Port = 587; $mail->SMTPSecure = 'tls'; if($this->charSetOpt != 0){ $mail->CharSet = $this->charSet; } $mail->From = $from; $mail->FromName = $sender_name; $mail->addAddress($to); //mail,name if(isset($cc) && !empty($cc)){ $mail->addBCC($bcc); } if(isset($cc) && !empty($cc)){ $mail->addCC($cc); } $mail->addReplyTo($this->replyTo);//to, name $mail->WordWrap = 50; if(isset($attachement_path) && !empty($attachement_path)){ // set word wrap to 50 characters $mail->AddAttachment($attachement_path); // add attachments } $mail->isHTML(true); // set email format to HTML $mail->Subject = $subject; $mail->Body = $body; //$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->send()){ $this->tempVar = $mail->ErrorInfo; return false; } else{ return true; } } //for multiple recipients public function sendEmails($to=array(),$from,$sender_name="",$subject,$body,$attachement_path="",$cc="",$bcc=""){ require '../../../../vendor/autoload.php'; $mail = new PHPMailer; $mail->IsSMTP(); // set mailer to use SMTP i.e. smtp1.example.com;smtp2.example.com $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; $mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->Host = $this->host; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $this->userName; // SMTP username i.e email id of an email address $mail->Password = $this->password; // SMTP password for the specified email address $mail->Port = 587; $mail->SMTPSecure = 'tls'; if($this->charSetOpt != 0){ $mail->CharSet = $this->charSet; } $mail->From = $from; $mail->FromName = $sender_name; //$mail->addAddress($to[0]); //mail,name foreach($to as $value){ $mail->addAddress($value); } if(isset($bcc) && !empty($bcc)){ $mail->addBCC($bcc); } if(isset($cc) && !empty($cc)){ $mail->addCC($cc); } //$mail->AddAddress("ellen@example.com"); // name is optional $mail->addReplyTo($this->replyTo);//to, name $mail->WordWrap = 50; if(isset($attachement_path) && !empty($attachement_path)){ // set word wrap to 50 characters $mail->AddAttachment($attachement_path); // add attachments //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name } $mail->isHTML(true); // set email format to HTML $mail->Subject = $subject; $mail->Body = $body; //$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->send()){ $this->tempVar = $mail->ErrorInfo; return false; } else{ return true; } } //for multiple/single recipient(s) and from random robots public function sendEmailRnd($to=array(),$sender_name="",$subject,$body,$attachement_path="",$cc="",$bcc=""){ $this->get_email_user($this->robots); require '../../../../vendor/autoload.php'; $mail = new PHPMailer; $mail->IsSMTP(); // set mailer to use SMTP i.e. smtp1.example.com;smtp2.example.com $mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //$mail->SMTPDebug = SMTP::DEBUG_SERVER; $mail->Host = $this->host; // specify main and backup server $mail->SMTPAuth = true; // turn on SMTP authentication $mail->Username = $this->userName; // SMTP username i.e email id of an email address $mail->Password = $this->password; // SMTP password for the specified email address $mail->Port = 587; $mail->SMTPSecure = 'tls'; if($this->charSetOpt != 0){ $mail->CharSet = $this->charSet; } $mail->From = $this->userName; $mail->FromName = $sender_name; //$mail->addAddress($to[0]); //mail,name foreach($to as $value){ $mail->addAddress($value); } if(isset($bcc) && !empty($bcc)){ $mail->addBCC($bcc); } if(isset($cc) && !empty($cc)){ $mail->addCC($cc); } //$mail->AddAddress("ellen@example.com"); // name is optional $mail->addReplyTo($this->replyTo);//to, name $mail->WordWrap = 50; if(isset($attachement_path) && !empty($attachement_path)){ // set word wrap to 50 characters $mail->AddAttachment($attachement_path); // add attachments //$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name } $mail->isHTML(true); // set email format to HTML $mail->Subject = $subject; $mail->Body = $body; //$mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->send()){ $this->tempVar = $mail->ErrorInfo; return false; } else{ return true; $this->tempVar = $from; } $mail->smtpClose(); } private function get_email_user($robots_array=array()){ try{ if(is_array($robots_array) && count($robots_array) > 0){ //get random user id with password from the $robots array $sender_robot_ary = $this->robots[array_rand($this->robots,1)]; $this->userName = $sender_robot_ary[0]; $this->password = $sender_robot_ary[1]; } }catch(Exception $exc){ $this->tempVar = $exc; } } }