3
votes

In notification email, we can use some API to write the mail rapidly using methods like greeting(), line() etc..

https://laravel.com/docs/5.4/notifications#mail-notifications

Can we use the same API in Mailable class?

Thanks

1
It going to be a pull request.alexeydemin

1 Answers

2
votes

No. The Mailable class does not implement the same methods as the MailMessage class.

You can use the MailMessage class outside of notifications, if you need to, but you'll have to send the mail object yourself.

$message = (new \Illuminate\Notifications\Messages\MailMessage())
    ->to(/* */)
    ->subject(/* */)
    ->line(/* */)
    ->action(/* */)
    ->line(/* */);

// most of this code is copied from \Illuminate\Notifications\Channels\MailChannel
Mail::send($message->view, $message->data(), function ($m) use ($message) {
    if (!empty($message->from)) {
        $m->from($message->from[0], isset($message->from[1]) ? $message->from[1] : null);
    }

    $m->to($message->to);

    if ($message->cc) {
        $m->cc($message->cc);
    }

    if (!empty($message->replyTo)) {
        $m->replyTo($message->replyTo[0], isset($message->replyTo[1]) ? $message->replyTo[1] : null);
    }

    $m->subject($message->subject ?: 'Default Subject');

    foreach ($message->attachments as $attachment) {
        $m->attach($attachment['file'], $attachment['options']);
    }

    foreach ($message->rawAttachments as $attachment) {
        $m->attachData($attachment['data'], $attachment['name'], $attachment['options']);
    }

    if (!is_null($message->priority)) {
        $m->setPriority($message->priority);
    }
});

NB: this is untested, but I think it should work.