fob_hot_ai_mail_auto_reply.php 5.8 KB
<?php


use Lib\Imap\Imap;
use Model\folderSql;

require_once "../vendor/autoload.php";


/**
 * 预热自动回复
 * @author:dc
 * @time 2025/7/2 9:58
 * Class fob_ai_mail_auto_reply
 */
class fob_hot_ai_mail_auto_reply
{


    public $isStop = false;


    /**
     * @var \Lib\Db
     */
    public $db;


    public $startTime = 0;

    /**
     * SyncToEsCmd constructor.
     */
    public function __construct()
    {
        $this->db = db();

        $handler = function ($signal) {
            _echo('收到进程信号 ' . $signal);
            // 可以处理其他程序
            $this->isStop = true;
        };
        pcntl_signal(SIGTERM, $handler); // 这个是kill
        pcntl_signal(SIGINT, $handler); // 这个是 ctrl+c

        $this->startTime = time();
    }


    /**
     * @return bool
     */
    public function isStop(): bool
    {
        // 检查是否接收到信号
        pcntl_signal_dispatch();

        // 是否超过来最大执行时间
        if (time() - 43200 > $this->startTime) {
            return true;
        }

        return $this->isStop;
    }

    protected $bodys = [];

    public function handler()
    {

//        $this->bodys = file_get_contents(__DIR__."/body.reply");
//        $this->bodys = explode("--------------",$this->bodys);

        while (!$this->isStop()) {

            try {
                $id = redis()->lPop('new_hot_mail_auto_reply_ids');
                if ($id) {
                    // 检查是否到时间了
                    list($did, $time) = explode('.', ((string)$id) . '.0');

//                $h = (int) date('H');
//                if($h >= 23 || $h < 6 ){
//                    sleep(5);
//                    continue;
//                }

                    // 查询数据
                    $data = $this->db->first(\Model\listsSql::first('`id` = ' . $did, '`id`,`to`,`folder_id`,`email_id`,`subject`,`is_hots`,`from`,`udate`,`uid`'));
                    if ($data && $data['is_hots']) {
                        // 在检查下是否是 收件箱
                        if ($this->db->cache(3600)->value(\Model\folderSql::has(['id' => $data['folder_id'], 'origin_folder' => 'INBOX']))) {
                            _echo('处理 ' . $data['id'] . " run " . date("Y-m-d H:i:s", $data['udate']));
                            $email = $this->db->throw()->cache(3600)->first(\Model\emailSql::first($data['email_id']));

                            // 验证是否是ai邮箱
                            if ($this->db->count(sprintf("select count(*) from `hot_mail` where `email` = '%s'", $email['email']))) {

                                // 验证邮箱状态
                                $post_id = fob_mysql()->cache(3600)->value("select `post_id` from `e_mail_binds` where `email_id` = " . $data['email_id']);
//                            // 国内项目不回复
                                if ($post_id == 10001746) {
                                    _echo('国内项目跳过');
                                    continue;
                                }

                                // 标记已读
                                $email['password'] = base64_decode($email['password']);
                                $email['host'] = $email['imap'];
                                $mailInstance = new Imap(new \Lib\Imap\ImapConfig($email));
                                if ($mailInstance->login()) {
                                    $mailInstance->folder('INBOX')->msg()->uid($data['uid'])->seen();
                                    $mailInstance = null;
                                }

                                // 来回超过5次就不回了
                                if (substr_count($data['subject'], "Re:") < 1) {
// 有配置才回复
                                    $email['hot_email'] = 1;
                                    $email['password'] = base64_encode($email['password']);
                                    $ret = \Lib\Mail\MailFun::sendEmail([
                                        'subject' => 'Re:' . $data['subject'],
                                        'tos' => [['email' => $data['from'], 'name' => explode('@', $data['from'])[0]]],
                                        'body' => $this->trimBody($data, $data['subject']),
                                        'mail-header' => [
                                            'Aicc-Hot-Mail' => 'hot' // 预热邮件
                                        ]
                                    ], $email);

                                    _echo(($ret[0] ? '回复成功 ' : '失败 ') . $data['id'] . " " . $email['email'] . ' to ' . $data['from'] . " " . $ret[1]);
                                } else {
                                    _echo('来回超过5次就不回了 ' . $data['id']);
                                }
                            }
                        }

                    }

                } else {
                    sleep(1);
                }
            } catch (Throwable $e) {
                logs($e->getMessage() . $e->getFile() . $e->getLine());
            }

        }

    }

    public function trimBody($data, $inbox)
    {
        if (count($this->bodys) < 1000) {
            $temp = @json_decode(file_get_contents("http://oa.shopk.com/api/email_template?tag=预热邮件"), true);
            if (!empty($temp['data']['body'])) {
                $this->bodys[] = $temp['data']['body'] ?? '';
            }
        }

        $reply = $this->bodys[array_rand($this->bodys)];

        return $reply . "<pre style='background-color: #e8d6d6;'>------------------ original message ------------------
from: {$data['from']};
send time: " . date("Ymd H:i", $data['udate']) . "
to: {$data['to']};
subject: {$data['subject']}

{$inbox}</pre>";
    }


}

(new fob_hot_ai_mail_auto_reply())->handler();

return 1;