首页  >  问答  >  正文

2022 年 5 月 30 日后,在没有“安全性较低的应用程序”选项的情况下导航 PHPMailer

我已经使用 PHPMailer 很长时间了,通过在 Google 设置中启用 “不太安全的应用程序” 选项,使用 Google 凭据(登录名和密码)发送电子邮件。

Google 通知,2022 年 5 月 30 日之后,Google 将不允许使用 “安全性较低的应用” 发送电子邮件。

安全性较低的应用和您的 Google 帐户

是否有其他方法可以从 PHPMailer 使用 Gmail SMTP 发送电子邮件而不使用 “安全性较低的应用程序” 功能?

P粉039633152P粉039633152335 天前627

全部回复(2)我来回复

  • P粉127901279

    P粉1279012792023-10-25 12:56:10

    这个方法对我有用,你可以试试这个:

    1. 首先转到您的 Google 帐户管理,然后转到安全性。
    2. 确保您的两步验证已启用。
    3. 然后转到应用密码。
    4. 在选择应用下拉菜单中选择其他,并根据您的喜好命名。
    5. 然后点击“生成”,Google 会给您一个密码。确保复制它并将其保存在其他地方。
    6. 不要在 PHPMailer 设置中使用您的真实 Google 帐户密码,而是使用您刚刚生成的密码。

    回复
    0
  • P粉399090746

    P粉3990907462023-10-25 11:02:17

    如需参考,请访问 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

    第 2 步:转到网站根目录并运行以下命令

    $   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;
                }
            }
     
    }

    回复
    0
  • 取消回复