final class MailChannel implements Channel

Notification channel that sends email via the framework MailerService and/or AbstractMailer-based custom mailers.

Supported payload shapes (returned by Notification::toMail()):

1) Template mode (MailerService::sendTemplate) [ // 'to' => 'user@example.com', // optional; auto-routes from notifiable if omitted 'subject' => 'Subject', 'template' => 'welcome', 'data' => ['user' => $user], 'layout' => 'default', // optional 'attachments' => [...], // optional (use Attachments::content/path helpers) 'layoutPath' => null, // optional 'templatePath'=> null, // optional 'styles' => 'default', // optional 'stylesPath' => null, // optional ]

2) Raw HTML (MailerService::send / sendWithText) [ // 'to' => 'user@example.com', // optional; auto-route 'subject' => 'Subject', 'html' => '

Hello

', 'text' => 'Hello', // optional -> triggers sendWithText() 'template' => 'welcome', // optional: used only for logging 'attachments' => [...], // optional ]

3) Custom mailer (AbstractMailer subclass) [ 'mailer' => \Core\Lib\Mail\WelcomeMailer::class, // The mailer can implement a static sendTo(\App\Models\Users $user): bool // If not, we construct it and call buildAndSend(...) with optional overrides: 'layout' => null, 'attachments' => [], 'layoutPath' => null, 'templatePath'=> null, 'styles' => null, 'stylesPath' => null, ]

Methods

__construct(MailerService|null $service = null)

Constructor for MailChannel.

bool
isTemplate(array $payload)

Determines if mail will be using a template.

static string
name()

Short channel name used in Notification::via().

bool
notifyWithBuildAndSend(AbstractMailer $mailer, array $payload)

Setups notification using buildAndSend

void
notifyWithHTML(mixed $notification, array $payload, string $subject, string $to)

Setups notification with html and with text optional.

void
notifyWithTemplate(mixed $notification, array $payload, string $subject, string $to)

Setups notification with mail template.

static object
requireUser(object $notifiable)

Ensure the notifiable is the expected user type for AbstractMailer.

string
route(object $notifiable)

Resolve recipient email from the notifiable.

void
send(object $notifiable, Notification $notification, mixed $payload)

Deliver the notification for this channel.

bool
sendWithHTML(string $to, string $subject, string $html, string|null $text = null, string|null $template = null, array $attachments = [])

Sends notification through send function of MailerService or if text body exists it uses sendWithText.

void
sendWithCustomMailer(object $notifiable, array $payload)

Handle AbstractMailer subclasses.

Details

at line 64
__construct(MailerService|null $service = null)

Constructor for MailChannel.

Parameters

MailerService|null $service

The mailer service object.

at line 74
private bool isTemplate(array $payload)

Determines if mail will be using a template.

Parameters

array $payload

The payload for the notification.

Return Value

bool

True if template. Otherwise, we return false.

at line 83
static string name()

Short channel name used in Notification::via().

Return Value

string

The name of the channel.

at line 94
private bool notifyWithBuildAndSend(AbstractMailer $mailer, array $payload)

Setups notification using buildAndSend

Parameters

AbstractMailer $mailer

The mailer class object.

array $payload

The payload for the notification.

Return Value

bool

True if successful. Otherwise, we return false.

at line 113
private void notifyWithHTML(mixed $notification, array $payload, string $subject, string $to)

Setups notification with html and with text optional.

Parameters

mixed $notification
array $payload

The payload for the notification.

string $subject

The subject for the E-mail.

string $to

The recipient for the E-mail.

Return Value

void

at line 139
private void notifyWithTemplate(mixed $notification, array $payload, string $subject, string $to)

Setups notification with mail template.

Parameters

mixed $notification
array $payload

The payload for the notification.

string $subject

The subject for the E-mail.

string $to

The recipient for the E-mail.

Return Value

void

at line 169
static private object requireUser(object $notifiable)

Ensure the notifiable is the expected user type for AbstractMailer.

Parameters

object $notifiable

The user/entity receiving the notification.

Return Value

object

at line 184
private string route(object $notifiable)

Resolve recipient email from the notifiable.

Parameters

object $notifiable

The user/entity receiving the notification.

Return Value

string

The recipient of the E-mail

at line 198
void send(object $notifiable, Notification $notification, mixed $payload)

Deliver the notification for this channel.

Implementations SHOULD be idempotent when possible, or document non-idempotent behavior. If delivery fails, throw an exception to allow the caller (or a queue worker) to handle retries.

Parameters

object $notifiable

The user/entity receiving the notification.

Notification $notification

The notification instance.

mixed $payload

Usually the result of toX() (array/DTO)

Return Value

void

at line 257
private bool sendWithHTML(string $to, string $subject, string $html, string|null $text = null, string|null $template = null, array $attachments = [])

Sends notification through send function of MailerService or if text body exists it uses sendWithText.

Parameters

string $to

The recipient for the E-mail.

string $subject

The subject for the E-mail.

string $html

The E-mail's content.

string|null $text

The E-mail's text content.

string|null $template

The template if it exists.

array $attachments

An array containing information about attachments.

Return Value

bool

at line 276
private void sendWithCustomMailer(object $notifiable, array $payload)

Handle AbstractMailer subclasses.

Parameters

object $notifiable

Must be compatible with the mailer constructor (expects \App\Models\Users)

array $payload

Usually the result of toX() (array/DTO)

Return Value

void