Going DeeperPro· 40 min read

Mail & Notifications

Send real email with Mailables, and reach users across email, the database and Slack with one Notification.

What you will learn

  • Send an email with a Mailable class
  • Send a multi-channel Notification
  • Understand when to use each

Two ways to reach a user

Almost every app needs to contact users — a welcome email, a "your order shipped" alert, a password reset. Laravel gives you two related tools:

  • A Mailable — a class representing one email: its subject, who it is from, and which Blade view is its body.
  • A Notification — a class representing one message that can be delivered over several channels at once (email, a database row for an in-app bell icon, Slack, SMS).

Rule of thumb: if you only need email, a Mailable is simplest. If the same message should reach the user in multiple places, use a Notification.

Sending an email with a Mailable

Generate the Mailable, with a Blade view for its body:

Generate a Mailable and its email template
php artisan make:mail WelcomeMail --view=emails.welcome

This creates app/Mail/WelcomeMail.php plus a Blade view at resources/views/emails/welcome.blade.php — the email’s body is just a normal Blade template, so you write it like any page. Inside the Mailable you set the subject and the view:

The subject and the Blade view that becomes the email body
// app/Mail/WelcomeMail.php
public function __construct(public User $user) {}

public function envelope(): Envelope
{
    return new Envelope(subject: 'Welcome to CodingClave!');
}

public function content(): Content
{
    return new Content(view: 'emails.welcome');
}

The constructor carries the $user so the template can greet them. envelope() sets the subject line. content() names the Blade view used for the body. Now send it from anywhere with one expressive line:

Send the email to a user
use Illuminate\Support\Facades\Mail;

Mail::to($user->email)->send(new WelcomeMail($user));

Mail::to($user->email) chooses the recipient, and ->send(new WelcomeMail($user)) sends that email to them. While learning, set MAIL_MAILER=log in .env so emails are written to a log file instead of really sent — perfect for testing.

Note: Output (with MAIL_MAILER=log): The rendered email is written to storage/logs/laravel.log with its subject "Welcome to CodingClave!" and the HTML from your emails.welcome view — so you can confirm it works without a real mail server.

Reaching multiple channels with a Notification

A Notification can deliver the same message over several channels. Generate one, then list the channels in via() and describe each channel’s content:

One notification delivered by email and stored in the database
// app/Notifications/OrderShipped.php
public function via($notifiable): array
{
    return ['mail', 'database'];   // send by email AND store in the DB
}

public function toMail($notifiable): MailMessage
{
    return (new MailMessage)
        ->subject('Your order has shipped')
        ->line('Good news — your order is on the way!');
}

public function toArray($notifiable): array
{
    return ['message' => 'Your order has shipped'];  // for the database
}

Reading it: via() returns the list of channels — here mail and database — so this notification both emails the user and saves a row they can see in an in-app notifications panel. toMail() builds the email body with simple ->line() calls (no Blade needed for basic emails), and toArray() is the data stored for the database channel. You send it on the user model itself:

Notify the user across all listed channels at once
$user->notify(new OrderShipped());

$user->notify(new OrderShipped()) fires the notification through every channel in via() — one call, delivered to email and the database together. (For the database channel, run php artisan make:notifications-table && php artisan migrate once to create the notifications table.)

Note: Output: The user receives the "Your order has shipped" email, and a new row appears in the notifications table — which you can show as an unread badge in your app’s header. The same message reached them in two places from one notify() call.

Tip: Notifications pair beautifully with queues: implement ShouldQueue on a notification (or Mailable) and Laravel sends it in the background automatically, so a slow mail server never holds up the user’s request.

Q. What is the main advantage of a Notification over a plain Mailable?

Answer: A Mailable is one email. A Notification lists channels in via() and delivers the same message across all of them — e.g. email plus an in-app database notification — from a single notify() call.

✍️ Practice

  1. Generate a Mailable with a Blade view and send it to yourself with MAIL_MAILER=log.
  2. Create a Notification that uses both the mail and database channels and send it with $user->notify(...).

🏠 Homework

  1. Send a welcome email when a user registers, then add a database notification the user can see in an in-app bell menu.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →