PHPMailer使用oauth2 gmail SMTP-如何防止电子邮件进入已发送文件夹?

时间:2019-02-22 15:28:55

标签: php phpmailer

我正在使用League OAuth2客户端库使用Google的XOAuth2 SMTP身份验证。我不想将已发送的电子邮件存储在gmail的已发送文件夹中。通过gmail帐户成功发送的电子邮件,并在已发送邮件中显示,但我不想在已发送邮件中显示。让我知道是否有方法。

这是我的剧本。

 $mail = new PHPMailer\PHPMailer\PHPMailer();
    try {
        $mail->SMTPDebug = 2; 
        $mail->isSMTP();                                     
        $mail->Host = 'smtp.gmail.com';             
        $mail->SMTPAuth = true;
        $mail->SMTPSecure = 'tls';                           
        $mail->AuthType = 'XOAUTH2';
        $mail->Port = 587;                                   

        $email = '***@****.com';
        $clientId = '********************************************.apps.googleusercontent.com';
        $clientSecret = '*******************';
        $refreshToken = '*********************************************';
        $provider = new League\OAuth2\Client\Provider\Google(
            [
                'clientId' => $clientId,
                'clientSecret' => $clientSecret,
            ]
        );
        $mail->setOAuth(
            new PHPMailer\PHPMailer\OAuth(
                [
                    'provider' => $provider,
                    'clientId' => $clientId,
                    'clientSecret' => $clientSecret,
                    'refreshToken' => $refreshToken,
                    'userName' => $email,
                ]
            )
        );

        //Recipients
        $mail->setFrom($FromEmail,$FromName);

        if(is_array($to)){
            foreach($to as $eachEmail){
                $mail->addAddress($eachEmail);
            }
        }else{
            $mail->addAddress($to);
        }

        if($replyTo != ''){
            $mail->addReplyTo($replyTo);
        }else{
            $mail->addReplyTo($FromEmail);
        }

        if($bcc != ''){
            $mail->addBCC($bcc);
        }

        if(is_array($attachments) AND !empty($attachments)){
            foreach($attachments as $File){
                $mail->addAttachment($File); 
            }
        }else if($attachments != ''){
            $mail->addAttachment($attachments);
        }

        //Content
        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body    = $message;
        $mail->send();
        return true;
    }
    catch (Exception $e)
    {
       return false;
    }
    catch (\Exception $e)
    {
       return false;
    }

1 个答案:

答案 0 :(得分:1)

如果它出现在您的已发送文件夹中,则表示gmail将副本放在其中,而不是您的发送代码中。您可以编写一个脚本,该脚本通过IMAP连接到已发送邮件文件夹,然后将其删除,但这超出了PHPMailer的功能。