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.
66 lines
1.8 KiB
66 lines
1.8 KiB
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TestEmailCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'test:email';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '测试邮件发送功能是否正常';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
// 写死的收件人地址
|
|
$to = 'tanyuan@langye.net';
|
|
$subject = '系统测试邮件';
|
|
$content = '这是一封测试邮件,用于验证邮件发送功能是否正常工作。';
|
|
|
|
$this->info("开始测试邮件发送功能...");
|
|
$this->info("收件人: {$to}");
|
|
$this->info("主题: {$subject}");
|
|
|
|
try {
|
|
// 临时设置为 log 驱动进行测试
|
|
$originalMailer = config('mail.default');
|
|
config(['mail.default' => 'log']);
|
|
|
|
// 使用 Laravel 的 Mail 门面发送邮件
|
|
Mail::raw($content, function ($message) use ($to, $subject) {
|
|
$message->to($to)->subject($subject);
|
|
});
|
|
|
|
// 恢复原始配置
|
|
config(['mail.default' => $originalMailer]);
|
|
|
|
$this->info("✅ 邮件发送成功!");
|
|
$this->info("邮件已记录到日志文件中,请检查 storage/logs/laravel.log");
|
|
|
|
return Command::SUCCESS;
|
|
|
|
} catch (\Exception $e) {
|
|
$this->error("❌ 邮件发送失败!");
|
|
$this->error("错误信息: " . $e->getMessage());
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|