fob_ai_mail_auto_reply.php
6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
require_once "../vendor/autoload.php";
/**
* 自动回复
* @author:dc
* @time 2025/7/2 9:58
* Class fob_ai_mail_auto_reply
*/
class fob_ai_mail_auto_reply {
public $isStop = false;
/**
* @var \Lib\Db
*/
public $fob_db;
/**
* @var \Lib\Db
*/
public $db;
public $startTime = 0;
/**
* SyncToEsCmd constructor.
*/
public function __construct()
{
$this->db = db();
$this->fob_db = fob_mysql();
$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;
}
public function handler(){
while (!$this->isStop()){
$id = redis()->lPop('new_mail_ids');
if($id){
// 检查是否到时间了
list($did,$time) = explode('.',$id);
if($time > time()){
redis()->rPush('new_mail_ids',$id);
usleep(10000);
continue;
}
// 查询数据
$data = $this->db->throw()->first(\Model\listsSql::first('`id` = '.$did,'`id`,`folder_id`,`email_id`,`subject`,`is_hots`,`from`,`udate`'));
if($data && !$data['is_hots']){
_echo('处理 '.$data['id']);
// 不是屏蔽的
$body = getBodyHtml(getMailBody($data['id']));
if(isAiAutoMail($data['from'],$data['subject'],$body)===0){
// 在检查下是否是 收件箱
if($this->db->value(\Model\folderSql::has(['id'=>$data['folder_id'],'origin_folder'=>'INBOX']))){
// 检查 是否开启了自动回复
list($postid,$source) = $this->getPostid($data['email_id']);
if($source == 2){
$email = $this->db->throw()->first(\Model\emailSql::first($data['email_id']));
// 配置
$replySetting = $this->getAutoReplySetting($postid,$email['email']);
if($replySetting){
// 有配置才回复
$ret = \Lib\Mail\MailFun::sendEmail([
'subject' => 'Re:'.$data['subject'],
'tos' => [['email'=>$data['from'],'name'=>explode('@',$data['from'])[0]]],
'body' => $this->trimBody($replySetting['content'],$body)
],$email);
_echo('回复成功'.$data['id'].'==='.$postid);
$this->log([$data,$replySetting,$ret]); // 立即写入日志
}else{
_echo('没有找到配置'.$data['id'].'==='.$postid);
}
}
}
}
}
}else{
sleep(1);
}
}
}
public function trimBody($reply,$inbox){
$reply = explode('</body>',$reply,2);
return $reply[0]."<pre>{$inbox}</pre>".($reply[1]??'');
}
public function log($msg){
logs($msg,LOG_PATH.'fob_ai_mail_auto_reply.log')->write(); // 立即写入日志
}
/**
* 读取自动回复的配置
* @param $postid
* @param $email
* @return false|mixed
* @author:dc
* @time 2025/7/2 11:22
*/
public function getAutoReplySetting($postid,$email){
$sets = $this->fob_db->throw()->all("select * from `ai_mail_auto_reply_setting` where ".dbWhere([
'postid' => $postid
]));
if(!$sets){
return false;
}
foreach ($sets as $set){
$set['emails'] = @json_decode($set['emails'],true);
// 是否开启了
if($set['emails']&&(in_array($email,$set['emails'])||in_array('所有邮箱',$set['emails']))){
// 必须设置了内容
if($set['content']){
$set['week'] = @json_decode($set['week'],true);
// 是否满足周
if(!$set['week'] || in_array(date('w'),$set['week'])){
$hi = date('H:i');
// 是否跨天
if ($set['day2day']){
// 跨天设置
if($hi > $set['s_hour'] || $hi < $set['e_hour']){
return $set;
}
}else{
// 非夸天
if($hi > $set['s_hour'] && $hi < $set['e_hour']){
return $set;
}
}
}
}
}
}
return false;
}
/**
* 项目id
* @author:dc
* @time 2025/5/20 15:44
*/
public function getPostid($email_id){
// 没有找到历史,就找绑定表
$data = redis()->getSet('fob_bind_mail:'.$email_id,300,function ($email_id){
return $this->fob_db->throw()->first("select `post_id`,`source` from `e_mail_binds` where `email_id` = '{$email_id}' and `deleted_at` is null order by `id` desc limit 1");
},$email_id);
return [
$data['post_id']??0,
$data['source']??0,
];
}
}
(new fob_ai_mail_auto_reply())->handler();
return 1;