Метод action
Метод action класса MailMessage используется для добавления в письмо кнопки-ссылки. Первым параметром передаётся текст кнопки, вторым - URL-адрес, на который она будет вести. Метод возвращает текущий экземпляр MailMessage для построения цепочки вызовов.
Синтаксис
<?php
$message->action($text, $url);
?>
Пример
Создадим уведомление для пользователя, которое содержит призыв подтвердить email:
<?php
namespace AppNotifications;
use IlluminateNotificationsNotification;
use IlluminateNotificationsMessagesMailMessage;
class VerifyEmailNotification extends Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello!')
->line('Please verify your email address.')
->action('Verify Email', url('/verify-email'))
->line('Thank you for using our application!');
}
}
?>
Результат выполнения кода (письмо будет содержать кнопку):
<div style="text-align: center;">
<a href="http://example.com/verify-email" style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px;">Verify Email</a>
</div>
Пример
Отправим уведомление о сбросе пароля с действием для перехода на страницу сброса:
<?php
namespace AppNotifications;
use IlluminateNotificationsNotification;
use IlluminateNotificationsMessagesMailMessage;
class ResetPasswordNotification extends Notification
{
protected $token;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$url = url('/password/reset', $this->token);
return (new MailMessage)
->subject('Reset Your Password')
->line('You are receiving this email because we received a password reset request for your account.')
->action('Reset Password', $url)
->line('If you did not request a password reset, no further action is required.');
}
}
?>
Результат выполнения кода:
"MailMessage object with action button: Reset Password -> http://example.com/password/reset/token_value"
Пример
Используем метод action в сочетании с другими методами для создания полноценного письма:
<?php
namespace AppMail;
use IlluminateBusQueueable;
use IlluminateMailMailable;
use IlluminateMailMailablesContent;
use IlluminateMailMailablesEnvelope;
use IlluminateQueueSerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public function envelope(): Envelope
{
return new Envelope(
subject: 'Welcome to our platform',
);
}
public function content(): Content
{
return new Content(
view: 'emails.welcome',
with: [
'message' => (new \IlluminateNotificationsMessagesMailMessage)
->greeting('Welcome!')
->line('We are glad to have you on board.')
->action('Get Started', url('/dashboard'))
->salutation('Best regards, Team'),
],
);
}
}
?>
Результат выполнения кода:
"Email with header 'Welcome!', text 'We are glad to have you on board.', button 'Get Started' linking to '/dashboard'"
Смотрите также
-
метод
line,
который добавляет строку текста в письмо -
метод
greeting,
который устанавливает приветствие в письме -
метод
subject,
который устанавливает тему письма -
метод
salutation,
который устанавливает прощальную фразу в письме