php - 如何使用 laravel 中的电子邮件通知使发件人的地址动态化
问题描述
我有一个关于 laravel 5.5 的项目,我的电子邮件通知工作正常,但我希望从数据库中选择发件人地址,而不是从通知中手动编码。
public function toMail($notifiable)
{
return (new MailMessage)
->from('hr@example.com', 'Example')
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
我的邮件陷阱邮件
解决方案
当您在 $user 对象上或通过外观调用 notify() 函数时,您可以通过(动态)地址
use App\Notifications\DemoNotification;
use Illuminate\Support\Facades\Notification;
class SomeController extends Controller
{
public function demo()
{
//$data = Some model object or anything
//$fromAddress = Pick from database
Notification::send($users, new DemoNotification($data,$fromAddress));
//Or $user = auth()->user();
$user->notify(new DemoNotification($data,$fromAddress));
}
}
接受 DemoNotification 类的构造函数中的 fromAddress
class DemoNotification extends Notification
{
use Queable;
public $fromAddress;
public $data;
public function __construct($data, $fromAddress)
{
$this->data = $data;
$this->fromAddress = $fromAddress;
}
public function toMail($notifiable)
{
return (new MailMessage)
->from($this->fromAddress, 'Example')
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}
}
推荐阅读
- wpf - 带有动画内容的 WPF 切换按钮
- javascript - 为从 javascript 数组创建的某些表格单元格着色
- javascript - 如何以编程方式关闭在 Kiosk 模式下运行的 Chrome?
- sqlite - 颤动如何获取代码行号'警告数据库已锁定0:00:10.000000
- django - 如何使用 uuid 而不是 id 进行嵌套路由 drf-extensions django
- google-cloud-platform - 使用 Google Docs 在 GCP 存储桶上编辑 Word 文档
- python - Python numpy 在大计算后不释放交换
- excel - 在文本框 vba 中输入 1 或 2 位小数后如何自动制表?
- javascript - 在空数组更改中触发 React UseEffect 函数
- html - 在 R 中的 DT::datatable 中包含表容器