搜尋

首頁  >  問答  >  主體

2022 年 5 月 30 日後,在沒有「安全性較低的應用程式」選項的情況下導航 PHPMailer

我已經使用 PHPMailer 很長時間了,透過在 Google 設定中啟用 「不太安全的應用程式」 選項,使用 Google 憑證(登入名稱和密碼)發送電子郵件。

Google 通知,2022 年 5 月 30 日之後,Google 將不允許使用 「安全性較低的應用程式」 發送電子郵件。

安全性較低的應用程式和您的 Google 帳戶

是否有其他方法可以從 PHPMailer 使用 Gmail SMTP 發送電子郵件而不使用 「安全性較低的應用程式」 功能?

P粉039633152P粉039633152416 天前726

全部回覆(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
  • 取消回覆