正在显示
3 个修改的文件
包含
132 行增加
和
0 行删除
app/Console/Commands/Task/SendAutoEmail.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @remark : | ||
| 4 | + * @name :SendAutoEmail.php | ||
| 5 | + * @author :lyh | ||
| 6 | + * @method :post | ||
| 7 | + * @time :2024/12/30 15:39 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +namespace App\Console\Commands\Task; | ||
| 11 | + | ||
| 12 | +use App\Mail\TextMail; | ||
| 13 | +use App\Models\Project\AutoEmailLog; | ||
| 14 | +use App\Models\Subscribe\GroupSendTask; | ||
| 15 | +use App\Models\Subscribe\GroupSendTaskLog; | ||
| 16 | +use App\Models\Subscribe\Smtp; | ||
| 17 | +use Illuminate\Console\Command; | ||
| 18 | +use Illuminate\Support\Facades\Config; | ||
| 19 | +use Illuminate\Support\Facades\Mail; | ||
| 20 | +class SendAutoEmail extends Command | ||
| 21 | +{ | ||
| 22 | + protected $signature = 'send_auto_email'; | ||
| 23 | + protected $description = '自动发送回复邮件'; | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * @remark :脚本 | ||
| 27 | + * @name :handle | ||
| 28 | + * @author :lyh | ||
| 29 | + * @method :post | ||
| 30 | + * @time :2024/12/30 15:43 | ||
| 31 | + */ | ||
| 32 | + public function handle() | ||
| 33 | + { | ||
| 34 | + $autoEmailLogModel = new AutoEmailLog(); | ||
| 35 | + $lists = $autoEmailLogModel->lists(['status'=>AutoEmailLog::STATUS_PENDING],1,100,'id',['*'],'asc'); | ||
| 36 | + if(empty($lists) || empty($lists['list'])){ | ||
| 37 | + sleep(10); | ||
| 38 | + } | ||
| 39 | + foreach ($lists['list'] as $values) { | ||
| 40 | + $this->toQueue($values); | ||
| 41 | + } | ||
| 42 | + return true; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * @remark :执行方法 | ||
| 47 | + * @name :toQueue | ||
| 48 | + * @author :lyh | ||
| 49 | + * @method :post | ||
| 50 | + * @time :2024/12/30 15:43 | ||
| 51 | + */ | ||
| 52 | + public function toQueue($info){ | ||
| 53 | + $this->output('开始执行任务:' . $info['id']); | ||
| 54 | + $smtpModel = new Smtp(); | ||
| 55 | + $smtpInfo = $smtpModel->read(['project_id'=>$info['project_id']]); | ||
| 56 | + if($smtpInfo === false){ | ||
| 57 | + $this->output('任务:' . $smtpInfo['id'] . '失败,未配置SMTP'); | ||
| 58 | + return false; | ||
| 59 | + } | ||
| 60 | + Config::set('mail.mailers.smtp.host', $smtpInfo['host']); | ||
| 61 | + Config::set('mail.mailers.smtp.username', $smtpInfo['email']); | ||
| 62 | + Config::set('mail.mailers.smtp.password', $smtpInfo['password']); | ||
| 63 | + Config::set('mail.from.address', $smtpInfo['email']); | ||
| 64 | + Config::set('mail.from.name', $smtpInfo['from_name']); | ||
| 65 | + try { | ||
| 66 | + $status = AutoEmailLog::STATUS_SUCCESS; | ||
| 67 | + Mail::to([$info['email']])->send(new TextMail(['subject' => $info['title'], 'text' => $info['content']])); | ||
| 68 | + } catch (\Exception $e) { | ||
| 69 | + $status = AutoEmailLog::STATUS_ERROR; | ||
| 70 | + $this->output('任务:' . $info['id'] . ' 邮箱' . $info['email'] . '发送失败,' . $e->getMessage()); | ||
| 71 | + } | ||
| 72 | + $autoEmailLogModel = new AutoEmailLog(); | ||
| 73 | + return $autoEmailLogModel->edit(['status'=>$status],['id'=>$info['id']]); | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * 输出处理日志 | ||
| 78 | + */ | ||
| 79 | + public function output($message): bool | ||
| 80 | + { | ||
| 81 | + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL; | ||
| 82 | + return true; | ||
| 83 | + } | ||
| 84 | +} |
app/Models/Project/AutoEmailLog.php
0 → 100644
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * @remark : | ||
| 4 | + * @name :AutoEmailLog.php | ||
| 5 | + * @author :lyh | ||
| 6 | + * @method :post | ||
| 7 | + * @time :2024/12/30 15:31 | ||
| 8 | + */ | ||
| 9 | + | ||
| 10 | +namespace App\Models\Project; | ||
| 11 | + | ||
| 12 | +use App\Models\Base; | ||
| 13 | + | ||
| 14 | +class AutoEmailLog extends Base | ||
| 15 | +{ | ||
| 16 | + protected $table = 'gl_auto_email_content_log'; | ||
| 17 | + | ||
| 18 | + const STATUS_PENDING = 0;//待发送 | ||
| 19 | + const STATUS_SUCCESS = 1;//OK | ||
| 20 | + const STATUS_ERROR = 2;//error | ||
| 21 | +} |
| @@ -6,6 +6,8 @@ namespace App\Services; | @@ -6,6 +6,8 @@ namespace App\Services; | ||
| 6 | use App\Exceptions\InquiryFilterException; | 6 | use App\Exceptions\InquiryFilterException; |
| 7 | use App\Models\Inquiry\InquiryForm; | 7 | use App\Models\Inquiry\InquiryForm; |
| 8 | use App\Models\Inquiry\InquiryFormData; | 8 | use App\Models\Inquiry\InquiryFormData; |
| 9 | +use App\Models\Project\AutoEmail; | ||
| 10 | +use App\Models\Project\AutoEmailLog; | ||
| 9 | use App\Models\Project\InquiryFilterConfig; | 11 | use App\Models\Project\InquiryFilterConfig; |
| 10 | use App\Models\Project\Project; | 12 | use App\Models\Project\Project; |
| 11 | use App\Models\Subscribe\Email; | 13 | use App\Models\Subscribe\Email; |
| @@ -129,12 +131,37 @@ class SyncSubmitTaskService | @@ -129,12 +131,37 @@ class SyncSubmitTaskService | ||
| 129 | } | 131 | } |
| 130 | $model->email = $email; | 132 | $model->email = $email; |
| 131 | $model->save(); | 133 | $model->save(); |
| 134 | + //查询当前项目是否开启自动回复邮件 | ||
| 135 | + $this->saveEmailLog($data['project_id'],$email); | ||
| 132 | }else{ | 136 | }else{ |
| 133 | throw new InquiryFilterException( '邮箱格式异常:' . $email); | 137 | throw new InquiryFilterException( '邮箱格式异常:' . $email); |
| 134 | } | 138 | } |
| 135 | } | 139 | } |
| 136 | 140 | ||
| 137 | /** | 141 | /** |
| 142 | + * @remark :保存自动发送邮件 | ||
| 143 | + * @name :saveEmailLog | ||
| 144 | + * @author :lyh | ||
| 145 | + * @method :post | ||
| 146 | + * @time :2024/12/30 15:36 | ||
| 147 | + */ | ||
| 148 | + public function saveEmailLog($project_id,$email){ | ||
| 149 | + $autoEmailModel = new AutoEmail(); | ||
| 150 | + $info = $autoEmailModel->read(['project_id'=>$project_id]); | ||
| 151 | + if($info !== false){ | ||
| 152 | + $paramData = [ | ||
| 153 | + 'project_id'=>$project_id, | ||
| 154 | + 'title'=>$info['title'], | ||
| 155 | + 'content'=>$info['content'], | ||
| 156 | + 'email'=>$email, | ||
| 157 | + ]; | ||
| 158 | + $autoEmailLogModel = new AutoEmailLog(); | ||
| 159 | + $autoEmailLogModel->addReturnId($paramData); | ||
| 160 | + } | ||
| 161 | + return true; | ||
| 162 | + } | ||
| 163 | + | ||
| 164 | + /** | ||
| 138 | * 询盘 | 165 | * 询盘 |
| 139 | * @param $data | 166 | * @param $data |
| 140 | * @return bool | 167 | * @return bool |
-
请 注册 或 登录 后发表评论