添加第二个操作或 link 到 Laravel 通知
Add a second action or a link to a Laravel Notification
我正在使用 Laravel 发送通知,但无法确定如何发送第二个操作。这是我的 toMail
方法代码:
public function toMail($notifiable)
{
return (new MailMessage)
->subject($this->options['subject'])
->greeting($this->options['greeting'])
->line($this->options['contentParagraph1'])
->line($this->options['contentParagraph2'])
->action('Facebook', 'https://www.facebook.com/')
->line($this->options['contentParagraph5'])
->action('LinkedIn', 'https://www.linkedin.com/')
->line($this->options['contentParagraph3'])
->line($this->options['contentParagraph4'])
->salutation($this->options['salutation']);
}
我得到的只是第二个动作。看起来第二个动作覆盖了第一个动作。有没有办法同时保留这两个操作或使用 link 而不是按钮?
SimpleMessage
class 旨在创建具有一个号召性用语按钮的简单消息,您可以在 Illuminate/Notifications/Messages/SimpleMessage.php and the template for the SimpleMessage emails can be found in Illuminate/Notifications/resources/views/email.blade.php 中找到支持该功能的代码 — 请注意单个按钮.
您可以使用 Markdown Mail Notifications 功能创建更复杂的消息,这将允许您包含任意数量的按钮。您可以这样实现:
- 运行生成新通知的命令,传入
markdown
选项,例如:php artisan make:notification InvoicePaid --markdown=mail.invoice.paid
- 打开新建的模板,例如:
views/mail/invoice/paid.blade.php
添加任意数量的按钮,例如:
@component('mail::message')
# Introduction
@component('mail::button', ['url' => $url1])
Button 1 Text
@endcomponent
@component('mail::button', ['url' => $url2])
Button 2 Text
@endcomponent
@component('mail::button', ['url' => $url3])
Button 3 Text
@endcomponent
Thanks,<br>
{{ config('app.name') }}
@endcomponent
在构建电子邮件时,将对 SimpleMessage
方法的调用替换为对 Markdown 模板的引用,例如:
return (new MailMessage)
->subject($this->options['subject'])
->markdown('mail.invoice.paid', $this->options);
markdown
方法中的第二个参数是一个传递给您的视图的数组,通过它您可以包含您想要包含在电子邮件中的各种值,例如 contentParagraph1
, greeting
和 salutation
.
我实施了这个变通方法来向标准 SimpleMail
对象添加按钮。因为 MailMessage::line()
可以接受任何实现 Htmlable
的对象,所以你可以在里面放任何你想要的东西。
您仍然需要覆盖通知 class 以创建 MyMultiButtonVerifyEmail
的实例(而不是 VerifyEmail
),但这很容易完成(您可能已经必须这样做)提供更新的电子邮件副本后执行此操作)。
糟糕,这些默认 Laravel 通知 class 并不是真正可重用的,是吗?
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Notifications\Action;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
class MyMultiButtonVerifyEmail extends VerifyEmail {
public function toMail($notifiable) {
$verificationUrl = ...;
$resendUrl = ...;
return (new MailMessage)
->line('some line')
->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
->line('another line')
->line($this->makeActionIntoLine(new Action(Lang::getFromJson('Request New Activation'), $resendUrl)))
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
} // end toMail()
private function makeActionIntoLine(Action $action): Htmlable {
return new class($action) implements Htmlable {
private $action;
public function __construct(Action $action) {
$this->action = $action;
} // end __construct()
public function toHtml() {
return $this->strip($this->table());
} // end toHtml()
private function table() {
return sprintf(
'<table class="action">
<tr>
<td align="center">%s</td>
</tr></table>
', $this->btn());
} // end table()
private function btn() {
return sprintf(
'<a
href="%s"
class="button button-primary"
target="_blank"
style="font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Helvetica, Arial, sans-serif, \'Apple Color Emoji\', \'Segoe UI Emoji\', \'Segoe UI Symbol\'; box-sizing: border-box; border-radius: 3px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); color: #fff; display: inline-block; text-decoration: none; -webkit-text-size-adjust: none; background-color: #3490dc; border-top: 10px solid #3490dc; border-right: 18px solid #3490dc; border-bottom: 10px solid #3490dc; border-left: 18px solid #3490dc;"
>%s</a>',
htmlspecialchars($this->action->url),
htmlspecialchars($this->action->text)
);
} // end btn()
private function strip($text) {
return str_replace("\n", ' ', $text);
} // end strip()
};
} // end makeActionIntoLine()
}
我正在使用 Laravel 发送通知,但无法确定如何发送第二个操作。这是我的 toMail
方法代码:
public function toMail($notifiable)
{
return (new MailMessage)
->subject($this->options['subject'])
->greeting($this->options['greeting'])
->line($this->options['contentParagraph1'])
->line($this->options['contentParagraph2'])
->action('Facebook', 'https://www.facebook.com/')
->line($this->options['contentParagraph5'])
->action('LinkedIn', 'https://www.linkedin.com/')
->line($this->options['contentParagraph3'])
->line($this->options['contentParagraph4'])
->salutation($this->options['salutation']);
}
我得到的只是第二个动作。看起来第二个动作覆盖了第一个动作。有没有办法同时保留这两个操作或使用 link 而不是按钮?
SimpleMessage
class 旨在创建具有一个号召性用语按钮的简单消息,您可以在 Illuminate/Notifications/Messages/SimpleMessage.php and the template for the SimpleMessage emails can be found in Illuminate/Notifications/resources/views/email.blade.php 中找到支持该功能的代码 — 请注意单个按钮.
您可以使用 Markdown Mail Notifications 功能创建更复杂的消息,这将允许您包含任意数量的按钮。您可以这样实现:
- 运行生成新通知的命令,传入
markdown
选项,例如:php artisan make:notification InvoicePaid --markdown=mail.invoice.paid
- 打开新建的模板,例如:
views/mail/invoice/paid.blade.php
添加任意数量的按钮,例如:
@component('mail::message') # Introduction @component('mail::button', ['url' => $url1]) Button 1 Text @endcomponent @component('mail::button', ['url' => $url2]) Button 2 Text @endcomponent @component('mail::button', ['url' => $url3]) Button 3 Text @endcomponent Thanks,<br> {{ config('app.name') }} @endcomponent
在构建电子邮件时,将对
SimpleMessage
方法的调用替换为对 Markdown 模板的引用,例如:return (new MailMessage) ->subject($this->options['subject']) ->markdown('mail.invoice.paid', $this->options);
markdown
方法中的第二个参数是一个传递给您的视图的数组,通过它您可以包含您想要包含在电子邮件中的各种值,例如 contentParagraph1
, greeting
和 salutation
.
我实施了这个变通方法来向标准 SimpleMail
对象添加按钮。因为 MailMessage::line()
可以接受任何实现 Htmlable
的对象,所以你可以在里面放任何你想要的东西。
您仍然需要覆盖通知 class 以创建 MyMultiButtonVerifyEmail
的实例(而不是 VerifyEmail
),但这很容易完成(您可能已经必须这样做)提供更新的电子邮件副本后执行此操作)。
糟糕,这些默认 Laravel 通知 class 并不是真正可重用的,是吗?
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Notifications\Action;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Lang;
class MyMultiButtonVerifyEmail extends VerifyEmail {
public function toMail($notifiable) {
$verificationUrl = ...;
$resendUrl = ...;
return (new MailMessage)
->line('some line')
->action(Lang::getFromJson('Verify Email Address'), $verificationUrl)
->line('another line')
->line($this->makeActionIntoLine(new Action(Lang::getFromJson('Request New Activation'), $resendUrl)))
->line(Lang::getFromJson('If you did not create an account, no further action is required.'));
} // end toMail()
private function makeActionIntoLine(Action $action): Htmlable {
return new class($action) implements Htmlable {
private $action;
public function __construct(Action $action) {
$this->action = $action;
} // end __construct()
public function toHtml() {
return $this->strip($this->table());
} // end toHtml()
private function table() {
return sprintf(
'<table class="action">
<tr>
<td align="center">%s</td>
</tr></table>
', $this->btn());
} // end table()
private function btn() {
return sprintf(
'<a
href="%s"
class="button button-primary"
target="_blank"
style="font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Helvetica, Arial, sans-serif, \'Apple Color Emoji\', \'Segoe UI Emoji\', \'Segoe UI Symbol\'; box-sizing: border-box; border-radius: 3px; box-shadow: 0 2px 3px rgba(0, 0, 0, 0.16); color: #fff; display: inline-block; text-decoration: none; -webkit-text-size-adjust: none; background-color: #3490dc; border-top: 10px solid #3490dc; border-right: 18px solid #3490dc; border-bottom: 10px solid #3490dc; border-left: 18px solid #3490dc;"
>%s</a>',
htmlspecialchars($this->action->url),
htmlspecialchars($this->action->text)
);
} // end btn()
private function strip($text) {
return str_replace("\n", ' ', $text);
} // end strip()
};
} // end makeActionIntoLine()
}