Метод salutation
Метод salutation класса MailMessage устанавливает прощальную фразу в конце письма. Обычно используется в методах toMail уведомлений для добавления заключительной части сообщения. Метод принимает один параметр - строку с текстом прощания, и возвращает экземпляр класса MailMessage для цепочки вызовов.
Синтаксис
<?php
use Illuminate\Notifications\Messages\MailMessage;
$message = (new MailMessage)
->salutation($text);
?>
Пример
Добавим прощальную фразу в уведомление о завершении регистрации:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class WelcomeNotification extends Notification
{
use Queueable;
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello!')
->line('Welcome to our platform.')
->action('Get Started', url('/dashboard'))
->salutation('Best regards, The Team');
}
}
?>
Результат выполнения кода:
Hello!
Welcome to our platform.
[Get Started]
Best regards, The Team
Пример
Создадим уведомление о сбросе пароля с персонализированным прощанием:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class ResetPasswordNotification extends Notification
{
use Queueable;
protected $token;
public function __construct($token)
{
$this->token = $token;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
$url = url(route('password.reset', [
'token' => $this->token,
'email' => $notifiable->getEmailForPasswordReset(),
], false));
return (new MailMessage)
->subject('Reset 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.')
->salutation('Thank you, ' . config('app.name'));
}
}
?>
Результат выполнения кода:
You are receiving this email because we received a password reset request for your account.
[Reset Password]
If you did not request a password reset, no further action is required.
Thank you, Laravel
Пример
Используем метод salutation для создания уведомления об успешной оплате:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class PaymentConfirmationNotification extends Notification
{
use Queueable;
protected $amount;
public function __construct($amount)
{
$this->amount = $amount;
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Payment Confirmation')
->line('Your payment of $' . $this->amount . ' has been processed successfully.')
->line('Thank you for your purchase.')
->salutation('Have a great day!');
}
}
?>
Результат выполнения кода:
Payment Confirmation
Your payment of $100 has been processed successfully.
Thank you for your purchase.
Have a great day!
Смотрите также
-
класс
MailMessage,
предоставляющий API для построения почтовых сообщений -
метод
greeting,
который устанавливает приветствие в письме -
метод
line,
который добавляет строку текста в письмо -
метод
action,
который добавляет кнопку-ссылку в письмо