Laravl 在 Mailer 类里获取渲染后的模版内容 作者: Chuwen 时间: 2021-10-21 分类: Laravel,PHP 评论 ## 序言 因为在项目中需要保存下发送的邮件内容,以便后续查看 ## 代码 通过以下代码就会获取到: ```php return Container::getInstance()->make('mailer')->render( $this->buildView(), $this->buildViewData() ); ``` 完整代码实例: ```php queue = 'account-send-email'; } /** * Build the message. * * @return $this */ public function build(): static { // 存放你的逻辑代码 return $this->view('emails.account.insufficient_quota_reminder'); } /** * 处理任务成功 * * @param \Illuminate\Contracts\Mail\Factory|\Illuminate\Contracts\Mail\Mailer $mailer */ public function send($mailer) { parent::send($mailer); $model = MailSendLog::whereId($this->mailSendLogId)->first(); if (is_null($model)) return; try { $model->content = $this->getRender(); } catch (BindingResolutionException | ReflectionException) { } $model->status = 1;//发送成功 $model->save(); } /** * 处理任务失败。 * * @param \Throwable $exception * * @return void */ public function failed(Throwable $exception) { $model = MailSendLog::whereId($this->mailSendLogId)->first(); if (is_null($model)) return; try { $model->content = $this->render(); } catch (ReflectionException) { } $model->exception = $exception->getMessage(); $model->status = 2;//发送失败 $model->save(); } /** * 获取渲染后的邮件模版 * * @return string * @throws \Illuminate\Contracts\Container\BindingResolutionException * @throws \ReflectionException */ private function getRender(): string { return Container::getInstance()->make('mailer')->render( $this->buildView(), $this->buildViewData() ); } } ```