Laravel调度器与Notification发送邮件时模板不更新问题求助
Laravel调度器发送邮件布局不更新问题
我使用Laravel调度器在用户创建账户时批量发送邮件,修改邮件布局后,通过调度器发送的邮件布局内容始终保持不变。已执行php optimize:clear和php view:clear命令,但布局仍未更新。相关代码如下:
MailBatch 命令类代码
class MailBatch extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'app:mail-batch'; /** * The console command description. * * @var string */ protected $description = 'send mail in pending mode'; /** * Execute the console command. */ public function handle() { $mailToSend = Mail::where('status', 'pending')->take('50')->orderBy('created_at')->get(); $emailLists = []; foreach ($mailToSend as $mail) { $emailBody = json_decode($mail->content); // $sendEmail = new GraphApiController(); $statusSend = 'send'; $recipients = $emailBody->message->toRecipients; $recipientsArray = []; $ccArray = []; $bccArray = []; foreach ($recipients as $recipient) { $recipientsArray[] = $recipient->emailAddress->address; if (isset($recipient->emailAddress->cc)) { $ccArray[] = $recipient->emailAddress->cc; } if(isset($recipient->emailAddress->bcc)) { $bccArray[] = $recipient->emailAddress->bcc; } } $replyTo = null; if(isset($emailBody->message->replyTo)) { $replyTo = $emailBody->message->replyTo; } try { $emailLists[] = Mailer::send(new NotifyUsers( $recipientsArray, $emailBody->message->subject, $emailBody->message->body->content, $ccArray, $bccArray, $replyTo )); } catch (\Exception $e) { $statusSend = 'error'; $emailLists[] = $e->getMessage(); } $mail->status = $statusSend; $mail->send_date = date("Y-m-d H:i:s", time()); $mail->save(); } } }
NotifyUsers Mailable类代码
#[AllowDynamicProperties] class NotifyUsers extends Mailable { use Queueable, SerializesModels; public array $receivers; public array $ccs; public array $bccs; public string $messageSubject; public string $emailBody; /** * Create a new message instance. */ public function __construct($receivers, $messageSubject, $emailBody, $ccs = [], $bccs = [], $replyTo = null) { $this->receivers = $receivers; $this->ccs = $ccs; $this->bccs = $bccs; $this->messageSubject = $messageSubject; $this->emailBody = $emailBody; $this->replyToUser = $replyTo; } /** * Get the message envelope. */ public function envelope(): Envelope { return new Envelope( from: new Address(Config::get('constants.support_email'), Config::get('constants.support_name')), to: $this->receivers, cc: $this->ccs, bcc: $this->bccs, replyTo: $this->replyToUser, subject: $this->messageSubject, ); } /** * Get the message content definition. */ public function content(): Content { return new Content( view: 'email.notifyUsers', ); } /** * Get the attachments for the message. * * @return array<int, \Illuminate\Mail\Mailables\Attachment> */ public function attachments(): array { $files= File::allFiles(public_path('logo')); $latestFile = collect($files)->sortByDesc(function ($file) { return $file->getMTime(); })->first(); $totalPath = public_path('logo').'/'.$latestFile->getFileName(); return [ Attachment::fromPath($totalPath) ]; } }
notifyUsers视图模板代码
@extends('email.layout') @section("content") {!! $emailBody !!} @endsection
解决方案
- 重启调度器进程:如果用Supervisor等工具管理调度器常驻进程,修改视图后旧缓存会被进程保留,需重启Supervisor服务(如
supervisorctl restart your-scheduler-process)或直接重启调度器进程。 - 彻底清理视图缓存:执行
php artisan view:clear后,手动检查storage/framework/views目录,删除所有残留的缓存文件。 - 禁用开发环境视图缓存:在
.env文件中设置VIEW_CACHE=false,确保开发期间视图不会被缓存。生产环境更新布局后,需重新执行php artisan view:cache生成新缓存。 - 验证布局文件路径:确认修改的
email.layout是正确的视图文件(路径应为resources/views/email/layout.blade.php),避免修改了其他环境或目录下的文件。 - 手动测试命令:直接执行
php artisan app:mail-batch发送邮件,若布局正常更新,说明问题出在调度进程未刷新,需重启调度器。 - 重启队列进程:如果邮件发送依赖队列,队列工作进程可能缓存了视图,执行
php artisan queue:restart重启队列进程。
内容的提问来源于stack exchange,提问作者Karan
相关产品推荐
相关产品推荐

