我实现了这个解决方法来向标准SimpleMail
对象添加按钮。由于MailMessage::line()
可以接受任何实现的对象Htmlable
,因此您可以在其中放入任何您想要的东西。
您仍然必须重写通知类来创建MyMultiButtonVerifyEmail
( 而不是VerifyEmail
) 的实例,但这很容易完成(一旦提供了更新的电子邮件副本,您可能已经必须做的事情)。
Grrr,这些默认的 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.'));
} // 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()
}