我针对向标准的SimpleMail
对象添加按钮的问题实现了这个解决方法。由于MailMessage::line()
可以接受任何实现Htmlable
的对象,因此您可以在其中放入任何想要的内容。
您仍然需要重写通知类,以创建MyMultiButtonVerifyEmail
的实例(而不是VerifyEmail
),但这很容易完成(并且您可能已经不止一次地更新了邮件副本)。
这些默认的Laravel通知类真的不太可重用,是吗?
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.'));
}
private function makeActionIntoLine(Action $action): Htmlable {
return new class($action) implements Htmlable {
private $action;
public function __construct(Action $action) {
$this->action = $action;
}
public function toHtml() {
return $this->strip($this->table());
}
private function table() {
return sprintf(
'<table class="action">
<tr>
<td align="center">%s</td>
</tr></table>
', $this->btn());
}
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)
);
}
private function strip($text) {
return str_replace("\n", ' ', $text);
}
};
}
}
为了后代,这是针对 Laravel 5.8.32 编写的。在框架的以后版本中可能有更好的实现方式。你的情况可能会有所不同。