SyncMail.php 6.3 KB
<?php

namespace Event;

use Lib\Imap\Parse\MessageItem;
use Model\emailSql;
use Model\folderSql;
use Model\listsSql;
use Swlib\Saber;
use Swlib\SaberGM;

/**
 * 邮件同步成功的事件
 * @author:dc
 * @time 2024/8/22 15:47
 * Class syncMail
 * @package Event
 */
class SyncMail {
    /**
     * @var \Lib\Db|\Lib\DbPool
     */
    private $db;


    public function __construct(int $id, array $data)
    {
//        if(php_sapi_name() == 'cli'){
            $this->db = db();

            // 是否是预热邮件 aicc专用
            if(!empty($header['Aicc-Hot-Mail']) || !empty($header['aicc-hot-mail'])){
                return $this->hot($id);
            }

            // 是否在指定文件夹内
            $f = $this->db->value(folderSql::first($data['folder_id'],'folder'));
            if(!$f){
                return true;
            }
            $f = folderAlias($f);
            if($f=='发件箱'){
                if(empty($data['to_name'])){
                    $data['to_name'] = [];
                }

                $data['to_name'] = is_array($data['to_name'])?$data['to_name']:json_decode($data['to_name']);

                $w = ['email' => array_map('strtolower',array_column($data['to_name'],'email'))];
            }else{
                $w = ['email' => strtolower($data['from'])];
            }
            // 是否在 预热邮箱中
            if($w['email'] && $this->db->count('select count(*) from `hot_mail` where '.dbWhere($w))){
                return $this->hot($id);
            }


            // 不是预热邮箱
            if($f=='收件箱'){

                $this->auto_mail($id,$data);

                $this->black_mail($id,$data);

                // 邮件过滤 这些邮箱都是系统邮箱
//            if(!$this->checkEmail($data['from']) && !$this->checkSubject($data['subject'])){
                //只提醒re
                if(stripos(trim($data['subject']),'re:')===0){
                    // 通知黑格 2024-08-22 新上 这个是异步的不会阻塞当前进程
                    try {
                        SaberGM::post('https://fob.ai.cc/api/email_new_push',[
                            'sign'  =>  md5(date('ymd').'fob.ai.cc.email'),
                            'id'    =>  $id,
                            'subject'   =>  $data['subject'],
                            'udate'   =>  $data['udate'],
                            'from'   =>  $data['from'],
                            'tos'    =>  array_column($data['to_name'],'email')
                        ]);
                    }catch (\Throwable $e){
                        // 就算异常了也不在推送了
                    }

                }


            }

//        }



    }

    private function hot($id){
        return $this->db->update(listsSql::$table,['is_hots'=>1],dbWhere(['id'=>$id]));
    }

    /**
     * 验证邮箱
     * @param $email
     * @author:dc
     * @time 2024/8/28 14:33
     */
    public function checkEmail($email){
        // mimecast@wsa.aero
        $filterEmail = [
            'mimecast@wsa.aero',
            'support@sugardaddymeet.com',
            'registration@facebookmail.com',
        ];
        if(in_array($email,$filterEmail)){
            return true;
        }

        if(preg_match("/^((no-?reply)|(postmaster)|(mailer-daemon)|(email-notifications)|(googleplay-noreply)|(postmaster-noreply)|(privacy-noreply))@/i",$email)){
            return true;
        }

        if(preg_match("/((no-?reply)|(postmaster)|(mailer-daemon)|(email-notifications))/i",$email)){
            return true;
        }


        return false;
    }

    /**
     * 验证标题是否存在某些关键词
     * @param string $subject
     * @return bool
     * @author:dc
     * @time 2024/8/24 15:09
     */
    public function checkSubject(string $subject){

        $keys = [
            'Automatic reply: ',
            'Delivery Failure',
            'Automatische Antwort:',
            'Automatic_reply:',
            'Undelivered Mail Returned',
            'Mail delivery failed:',
            'Delivery Status Notification',
            'Undeliverable',
            'failure',
            'Undelivered',
        ];

        foreach ($keys as $key){
            if(stripos($subject,$key)!==false){
                return true;
            }
        }

        return false;
    }


    /**
     * 自动回复邮箱
     * @param $id
     * @param $data
     * @author:dc
     * @time 2024/9/12 15:29
     */
    private function auto_mail($id,$data){
        $filter = '2	Automatic reply
2	Delivery
2	Automatische Antwort
2	Undeliverable
2	Failure
2	Undelivered
1	noreply
1	postmaster
1	email-notifications
1	mailer-daemon
1	no-reply
2	自动回复
2	Returned mail
2	Autosvar
2	Out Of Office Re
2	Change_of_email_address
2	delivered
2	automatique
2	Reply auto
2	automatic
2	Request received
2	Automatisch
2	Unzustellbar
2	Notification
2	Invitation
2	Automatyczna
2	expired';
        $filter = explode("\n",$filter);

        foreach ($filter as $f){
            if(!$f) continue;
            list($t,$str) = [
                intval(mb_substr($f,0,1)),
                trim(mb_substr($f,1,99)),
            ];
            $haystack = '';
            if($t==2){
                $haystack = $data['subject'];
            }elseif ($t==1){
                $haystack = $data['from'];
            }
            if(stripos($haystack,$str)!==false){
                if(!$this->db->count("select count(*) from `lists_auto` where `list_id` = ".$id)){
                    $this->db->create('lists_auto',['list_id'=>$id],false);
                }
                break;
            }
        }



    }

    /**
     * 黑名单邮箱,直接delete
     * @param $id
     * @param $data
     * @author:dc
     * @time 2024/9/12 15:29
     */
    private function black_mail($id,$data){

        if(!empty($data['from'])){
            // 是否是ai邮件
            if($this->db->count("select count(*) from `hot_mail` where ".dbWhere([
                    'email'=> $this->db->value(emailSql::first($data['email_id'],'`email`'))
                ]))){
                // 是否在黑名单中
                if($this->db->count("select count(*) from `ai_black_email` where ".dbWhere(['email'=>$data['from']]))){

                    $this->db->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>$id]));
                }

            }

        }

    }


}