|
|
|
<?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\GroupSendTask;
|
|
|
|
use App\Models\Subscribe\GroupSendTaskLog;
|
|
|
|
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();
|
|
|
|
$lists = $autoEmailLogModel->lists(['status'=>AutoEmailLog::STATUS_PENDING],1,100,'id',['*'],'asc');
|
|
|
|
if(empty($lists) || empty($lists['list'])){
|
|
|
|
sleep(10);
|
|
|
|
}
|
|
|
|
foreach ($lists['list'] as $values) {
|
|
|
|
$this->toQueue($values);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @remark :执行方法
|
|
|
|
* @name :toQueue
|
|
|
|
* @author :lyh
|
|
|
|
* @method :post
|
|
|
|
* @time :2024/12/30 15:43
|
|
|
|
*/
|
|
|
|
public function toQueue($info){
|
|
|
|
$this->output('开始执行任务:' . $info['id']);
|
|
|
|
$smtpModel = new Smtp();
|
|
|
|
$smtpInfo = $smtpModel->read(['project_id'=>$info['project_id']]);
|
|
|
|
if($smtpInfo === false){
|
|
|
|
$this->output('任务:' . $smtpInfo['id'] . '失败,未配置SMTP');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
$autoEmailLogModel = new AutoEmailLog();
|
|
|
|
return $autoEmailLogModel->edit(['status'=>$status],['id'=>$info['id']]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 输出处理日志
|
|
|
|
*/
|
|
|
|
public function output($message): bool
|
|
|
|
{
|
|
|
|
echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|