You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
<?php
|
|
|
|
namespace App\Console\Commands; // 定义命名空间
|
|
|
|
use App\Mail\SubscribesNotice; // 引入订阅通知邮件类
|
|
use App\Models\Subscribe; // 引入订阅模型
|
|
use Illuminate\Console\Command; // 引入 Laravel 的 Command 基类
|
|
use Mail; // 引入 Mail 门面
|
|
|
|
/**
|
|
* 发送订阅邮件命令
|
|
*
|
|
* 该命令用于向所有已订阅用户发送通知邮件。
|
|
*/
|
|
class SendSubscribeEmail extends Command
|
|
{
|
|
/**
|
|
* 控制台命令的名称和签名
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'moon:send-subscribes'; // 定义命令的名称和签名
|
|
|
|
/**
|
|
* 控制台命令的描述
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '发送订阅通知邮件'; // 描述该命令的用途
|
|
|
|
/**
|
|
* 创建一个新的命令实例.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct(); // 调用父类构造函数
|
|
}
|
|
|
|
/**
|
|
* 执行控制台命令
|
|
*
|
|
* 该方法执行发送订阅邮件的逻辑,包括查询订阅用户和发送邮件。
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function handle()
|
|
{
|
|
// 查询所有已订阅的用户邮箱
|
|
$mails = Subscribe::query()
|
|
->where('is_subscribe', 1) // 只选择已订阅的用户
|
|
->pluck('email'); // 获取邮箱地址列表
|
|
|
|
// 遍历每个邮箱并发送通知邮件
|
|
$mails->map(function ($realMail) {
|
|
// 对邮箱进行加密处理
|
|
$email = encrypt($realMail);
|
|
// 生成包含加密邮箱的邮件链接
|
|
$url = route('site.email', compact('email'));
|
|
|
|
// 发送邮件给当前用户
|
|
// 不要一次发送给多个用户,以免暴露其他人的邮箱
|
|
Mail::to($realMail)->send(new SubscribesNotice($url));
|
|
});
|
|
|
|
// 记录系统日志,记录发送的用户信息
|
|
createSystemLog('系统发送订阅消息, 发送的用户:' . $mails->implode(', '), $mails->toArray());
|
|
}
|
|
}
|