php - Laravel 自定义邮件通知表
问题描述
我正在使用 laravel 通知发送电子邮件,但我想知道如何在文本行之间的电子邮件视图上显示表格。
我正在使用 laravel 的默认视图,但我不知道如何传递表格'
这是我的 toMail 方法:
public function toMail($notifiable)
{
$message = new MailMessage();
$message->subject('Command confirmation n°'.$this->order->id)
->replyTo('noreply@example.com')
->line('Thanks to choose us!')
->line('Here the details of your order n°'.$this->order->id);
foreach($this->order->cart as $item){
// here the table
}
$message->line('To see your order click here');
$message->action('My orders', url('http://www.example.com/espace-perso/mes-commandes'))
return $message;
}
在 email.blade.php 视图(默认 laravel 视图)中,我有:
{{-- Action Button --}}
@isset($actionText)
<?php
switch ($level) {
case 'success':
$color = 'green';
break;
case 'error':
$color = 'red';
break;
default:
$color = 'blue';
}
?>
@component('mail::button', ['url' => $actionUrl, 'color' => $color])
{{ $actionText }}
@endcomponent
@endisset
{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}
@endforeach
如何放置降价表,以及如何在行之间放置它,例如操作按钮?
解决方案
如果$this->order
是公共属性,它将在视图文件中可用。
通常,您需要将一些数据传递给您的视图,以便在呈现电子邮件的 HTML 时使用这些数据。有两种方法可以使数据对您的视图可用。首先,在您的可邮寄类上定义的任何公共属性都将自动对视图可用。因此,例如,您可以将数据传递到可邮寄类的构造函数中,并将该数据设置为类上定义的公共属性:
否则使用该with
方法将数据传递给视图。
如果您想在发送到模板之前自定义电子邮件数据的格式,您可以通过 with 方法手动将数据传递到视图。通常,您仍将通过可邮寄类的构造函数传递数据;但是,您应该将此数据设置为受保护或私有属性,这样数据就不会自动提供给模板。然后,在调用 with 方法时,传递一个您希望提供给模板的数据数组
接下来添加表格组件并循环创建行的订单项:
@component('mail::table')
| id | name | price | qty | subtotal |
| -- |:----:| -----:| ---:| --------:|
@foreach($order->cart as $item)
// create table rows
@endforeach
@endcomponent
推荐阅读
- r - 如何按频率聚合一列字符串
- javascript - 提交表单时,加载到 tinymce 中的 HTML 标记未保存
- javascript - 为什么这个 JavaScript 实现查找最大单词返回 TypeError: Cannot read properties of undefined (reading 'length')
- python - 从 Python 中的并发线程修改唯一的字典条目是否安全?
- python-3.x - 无法在 TKinter 中调整类型 StringVar
- type-hinting - 如何正确键入提示类装饰器?
- c++ - 需要在c ++中将连续空格读取为单词
- php - Redis 消息:连接到 myredis-test.redis.cache.windows.net:6380 时出现读取错误 - Codeigniter
- python - 如何消除阻止网页内容的“接受 cookie”弹出窗口?
- python - 为股票价格建立“月份中的日期”列