SendAutoEmail.php
2.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
<?php
/**
* @remark :
* @name :SendAutoEmail.php
* @author :lyh
* @method :post
* @time :2024/12/30 15:39
*/
namespace App\Console\Commands\Task;
use App\Mail\TextMail;
use App\Models\Project\AutoEmailLog;
use App\Models\Subscribe\Smtp;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
class SendAutoEmail extends Command
{
protected $signature = 'send_auto_email';
protected $description = '自动发送回复邮件';
/**
* @remark :脚本
* @name :handle
* @author :lyh
* @method :post
* @time :2024/12/30 15:43
*/
public function handle()
{
$autoEmailLogModel = new AutoEmailLog();
$list = $autoEmailLogModel->list(['status'=>AutoEmailLog::STATUS_PENDING],'id',['*'],'asc',100);
if(!empty($list)){
$ids = array_column($list,'id');
$autoEmailLogModel->edit(['status'=>AutoEmailLog::STATUS_ON],['id'=>['in',$ids]]);
foreach ($list as $value) {
$this->output('开始执行任务:' . $value['id']);
$smtpModel = new Smtp();
$smtpInfo = $smtpModel->read(['project_id'=>$value['project_id']]);
if($smtpInfo === false){
$status = AutoEmailLog::STATUS_ERROR;
$this->output('任务:' . $smtpInfo['id'] . '失败,未配置SMTP');
}else{
$status = $this->toQueue($value,$smtpInfo);
}
$autoEmailLogModel = new AutoEmailLog();
$rs = $autoEmailLogModel->edit(['status'=>$status],['id'=>$value['id']]);
$this->output('保存状态:' . $rs . 'status=>'.$status);
}
}
return true;
}
/**
* @remark :发送邮件
* @name :toQueue
* @author :lyh
* @method :post
* @time :2024/12/30 15:43
*/
public function toQueue($info,$smtpInfo){
Config::set('mail.mailers.smtp.host', $smtpInfo['host']);
Config::set('mail.mailers.smtp.username', $smtpInfo['email']);
Config::set('mail.mailers.smtp.password', $smtpInfo['password']);
Config::set('mail.from.address', $smtpInfo['email']);
Config::set('mail.from.name', $smtpInfo['from_name']);
try {
$status = AutoEmailLog::STATUS_SUCCESS;
Mail::to([$info['email']])->send(new TextMail(['subject' => $info['title'], 'text' => $info['content']]));
} catch (\Exception $e) {
$status = AutoEmailLog::STATUS_ERROR;
$this->output('任务:' . $info['id'] . ' 邮箱' . $info['email'] . '发送失败,' . $e->getMessage());
}
return $status;
}
/**
* 输出处理日志
*/
public function output($message): bool
{
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
return true;
}
}