首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHPMailer,2022年5月30日之后,当“不太安全的应用程序”不再是一种选择?

如何使用PHPMailer,2022年5月30日之后,当“不太安全的应用程序”不再是一种选择?
EN

Stack Overflow用户
提问于 2022-05-04 13:17:57
回答 5查看 8.6K关注 0票数 8

我已经使用PHPMailer很长一段时间使用谷歌凭据(登录和密码)发送电子邮件,在谷歌设置中启用"Less secure app"选项。

由于谷歌通知说,在2022年5月30日之后,谷歌将不允许使用"Less secure app"发送电子邮件。

不太安全的应用程序&你的谷歌账户

在没有PHPMailer功能的情况下,还有其他方法可以使用Gmail从"Less secure app"发送电子邮件吗?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2022-10-15 04:59:51

如需参考,请访问GitHub网站:

代码语言:javascript
复制
  [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:转到您的网站根目录并运行以下命令

代码语言:javascript
复制
$   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
EN

Stack Overflow用户

发布于 2022-05-04 18:11:35

您需要做的是使用XOAUTH2再次切换到Oauth2或Gmail。

代码语言:javascript
复制
$mail->oauthUserEmail = "[Redacted]@gmail.com";
$mail->oauthClientId = "[Redacted]";
$mail->oauthClientSecret = "[Redacted]";
$mail->oauthRefreshToken = "[Redacted]";

你不能在没有用户许可的情况下发送电子邮件。您试图发送邮件的gmail帐户的所有者将需要授权该应用程序一次,如果您存储刷新令牌,您应该能够使用它没有任何问题。

请记住,您将需要通过应用程序验证过程与谷歌。因为您将使用敏感范围。

使用他们的登录和密码不再是一个选项。

票数 5
EN

Stack Overflow用户

发布于 2022-07-18 12:09:42

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

  1. 首先,进入您的谷歌帐户管理和安全。
  2. 确保启用了2步验证。
  3. 然后转到应用密码。
  4. 在Select应用下拉菜单中选择其他选项,并命名任何您喜欢的名称。
  5. 然后点击generate,google会给你一个密码。确保你复制它,并将它保存在其他地方。
  6. 相反,在PHPMailer设置中使用真正的google密码,使用您刚刚生成的密码。
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72113637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档