SendJob.php
9.9 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace App\Mail\Jobs;
use App\Http\Mail\lib\MailFun;
use App\Http\Mail\Models\Email;
use App\Http\Mail\Models\EmailSendJob;
use App\Http\Mail\Models\EmailSendJobStatu;
use App\Http\Models\EmailSendTemplate;
use App\Sk;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
/**
* @author:dc
* @time 2022/11/9 11:52
* Class SendJob
*/
class SendJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $job_id;
/**
* SendServiceMsg constructor.
* @param $id
*/
public function __construct($id)
{
// 使用的链接
$this->connection = Sk::QUEUE_EMAIL_QUN;
$this->job_id = $id;
}
/**
* Execute the job.
*
* @return bool
*/
public function handle()
{
// 查询邮件任务主体
$jobData = EmailSendJob::where('id',$this->job_id)
->whereIn('status',[EmailSendJob::STATUS_WAIT,EmailSendJob::STATUS_RUNING])
->first();
if(!$jobData){
$this->log('任务不存在');
return false;
}
// 发送时间
if(trim($jobData->send_time) != 'now'){
// 定时发送
list($start,$end) = explode(',',$jobData->send_time);
// 系统是中国时区,按照美国时区要慢13个小时
if(!(date('H') >= $start || date('H') < $end)){
// 也就是 美国早上 8点到晚上10点
// $this->log('休息中');
SendJob::dispatch($this->job_id)->delay(600);
return true;
}
}
$this->log('开始检查任务:'.$this->job_id);
if(!$jobData){
$this->log('没有找到任务');
return true;
}
// 查询需要发送的邮件
$jobStdata = EmailSendJobStatu::where(['job_id'=>$this->job_id,'status'=>EmailSendJobStatu::STATUS_WAIT])->first();
if(!$jobStdata){
$this->log('完成所有了');
// 是否完成
$jobData->status = EmailSendJob::STATUS_SUCCESS;
$jobData->save();
return true;
}
$this->log('找到任务:'.$jobStdata['id']);
// 防止重复
$cachekey = 'email_send_job:'.$this->job_id.":".$jobStdata['id'];
// 存在
if(Cache::has($cachekey)){
$this->log("任务:{$jobStdata['id']}正在发送,跳过");
SendJob::dispatch($this->job_id)->delay(30);
return true;
}
// 占有2分钟
Cache::set($cachekey,$jobStdata['id'],120);
// 是否等待状态
if($jobData->status !== EmailSendJob::STATUS_RUNING){
$this->log('开始运行脚本了');
$jobData->status = EmailSendJob::STATUS_RUNING;;
$jobData->save();
}
// 当前管理账号下所有绑定的邮件
$emailinfos = Email::_get($jobData->user_id,Email::STATUS_ACTIVE,['e.id','e.email','e.email_name','e.smtp','e.password','e.pwd_error']);
$smtpErrorNum = 0;// 错误次数
// 标签
$tags = $jobData->tags;
$tags = is_array($tags) ? $tags : explode(',',$tags);
// 模板列表
$tempLists = EmailSendTemplate::getAdminTagsList($tags);
if(!$tempLists){
// 是否完成
$jobData->status = EmailSendJob::STATUS_SUCCESS;
$jobData->save();
$this->log('没有可用的模板');
return false;
}
// 随机一个模板
$template = $tempLists[array_rand($tempLists->toArray())];
// 节点
EMAILRESETRANG:
if(!$emailinfos){
$this->log('暂时没有可分配的账号');
// 再次发布任务,延时10分钟
SendJob::dispatch($this->job_id)->delay(120);
return true;
}
// 随机一个邮件来当发送
$key = array_rand($emailinfos,1);
$emailinfo = $emailinfos ? $emailinfos[$key] : [];
if(!$emailinfo){
$this->log('没有分配到账号');
// 再次发布任务,延时10分钟
SendJob::dispatch($this->job_id)->delay(600);
return true;
}
unset($emailinfos[$key]);
$emailinfos = array_values($emailinfos);
// 密码是否需要验证
if($emailinfo['pwd_error']){
$this->log('账号密码验证失败,需要修改密码 '.$emailinfo['email']);
// 重新随机一个,
goto EMAILRESETRANG;
}
// 每个小时不能超过20
$cacheEmailSuccesshkey = 'email_job_email:'.$emailinfo['email'].":success_h";
if(Cache::get($cacheEmailSuccesshkey,0) >= 8){
$this->log('账号超过每小时8封了'.$emailinfo['email']);
// 重新随机一个,
goto EMAILRESETRANG;
}
// 一天中是否超过100,
// $cacheEmailSuccessdkey = 'email_job_email:'.$emailinfo['email'].":success_d";
// if(Cache::get($cacheEmailSuccessdkey,0) >= 100){
// $this->log('账号超过每天100封了'.$emailinfo['email']);
// // 重新随机一个,
// goto EMAILRESETRANG;
// }
$cachemailikey = 'email_job_email:'.$emailinfo['email'].":success_i";
if (Cache::has($cachemailikey)){
$this->log('账号没超过10分钟间隔 '.$emailinfo['email']);
// 重新随机一个,
goto EMAILRESETRANG;
}
// 是否错误了
$cachemailierrordkey = 'email_job_email:'.$emailinfo['email'].":error_d";
if(Cache::has($cachemailierrordkey)){
$this->log('账号记录错误了,跳过 1小时'.$emailinfo['email']);
goto EMAILRESETRANG;
}
Cache::set($cachemailikey,$jobStdata['id'],550);
$this->log('找到账号:'.$emailinfo['email']);
// 数据发送的email
$data = json_decode($jobData->maildata,true);
$data['body'] = $template['body'];
$data['subject'] = $template['subject'];
try {
// 替换邮件规则,
$data['body'] = str_replace('[read][/read]','<div style="opacity: 0;width: 1px;height: 1px;overflow: hidden;"><img src="https://king.shopk.com/_shopk_?mail='.base64_encode('logo|'.$jobStdata['id']).'" /></div>',$data['body']);
// 匹配链接规则
if (preg_match_all("/\[link:(.*)\](.*)\[\/link\]/U",$data['body'],$m)){
foreach ($m[0] as $mk=>$item){
$data['body'] = str_replace($item,'<a target="_blank" href="https://king.shopk.com/_shopk_?mail='.base64_encode('link|'.$jobStdata['id'].'|'.urlencode($m[1][$mk])).'">'.$m[2][$mk].'</a>',$data['body']);
}
}
// 发送邮件
MailFun::sendEmail(
$emailinfo['smtp'],$emailinfo['email'],decrypt($emailinfo['password'])
,$emailinfo['email_name'],$jobStdata['to_email'],$data['subject'],
$data['body'],$data['file']??[],false
,($data['priority']??0) ? 1 : 3
);
// 记录
$jobStdata->status = EmailSendJobStatu::STATUS_SUCCESS;
$jobStdata->send_email = $emailinfo['email'];
$jobStdata->time = date('Y-m-d H:i:s');
$this->log('成功了');
// 成功
$jobData->success = $jobData->success+1;
// 24小时内不能超过100
// if(Cache::has($cacheEmailSuccessdkey)){
// // 加1
// Cache::increment($cacheEmailSuccessdkey);
// }else{
// Cache::set($cacheEmailSuccessdkey,1,86400);
// }
// 每小时内不能超过20
if(Cache::has($cacheEmailSuccesshkey)){
// 加1
Cache::increment($cacheEmailSuccesshkey);
}else{
Cache::set($cacheEmailSuccesshkey,1,3600);
}
} catch (\Exception $e) {
if($e->getMessage()=='SMTP Error: data not accepted.'){
// 下次跳过账号
if(!Cache::has($cachemailierrordkey)) {
Cache::set($cachemailierrordkey, $e->getMessage(), 3600);
}
}
// 无法验证,密码错误了,处理密码
if($e->getMessage()=='SMTP Error: Could not authenticate.'){
Email::_update(['id'=>$emailinfo['id']],['pwd_error'=>1]);
}
$smtpErrorNum++;
// 超过失败3次的
$error = $jobStdata->error;
$error[] = [
'email' => $emailinfo['email'],
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine()
];
$jobStdata->time = date('Y-m-d H:i:s');
$jobStdata->error = json_encode($error);
// 如果失败了,重试3次
if($smtpErrorNum <= 20){
// 记录
$jobStdata->save();
$this->log('错误次数'.$smtpErrorNum);
goto EMAILRESETRANG;
}
$this->log('错误超过次数'.$smtpErrorNum);
// 记录
$jobStdata->status = EmailSendJobStatu::STATUS_ERROR;
$jobStdata->send_email = $emailinfo['email'];
// 错误
$jobData->error = $jobData->error+1;
}
// 保存
$jobStdata->save();
$jobData->save();
$this->log('完成');
// 再次发布任务
SendJob::dispatch($this->job_id);
}
protected function log($content){
@file_put_contents(storage_path('logs/send_email_job_'.$this->job_id.'_.log'),date('Y-m-d H:i:s ').$content.PHP_EOL,FILE_APPEND);
}
}