0
0
LaravelHow-ToBeginner · 4 min read

How to Create Mailable in Laravel: Simple Guide

To create a mailable in Laravel, run php artisan make:mail YourMailableName to generate a Mailable class. Then, define the email content in the class's build() method and send it using Mail::to()->send().
📐

Syntax

Use the artisan command to create a mailable class. Then customize the build() method to set the email view and data.

  • php artisan make:mail YourMailableName: Creates the mailable class.
  • build(): Method to define email content and settings.
  • Mail::to($recipient)->send(new YourMailableName()): Sends the email.
bash
php artisan make:mail YourMailableName

// In app/Mail/YourMailableName.php
public function build()
{
    return $this->view('emails.example');
}

// Sending email
use Illuminate\Support\Facades\Mail;
Mail::to('user@example.com')->send(new YourMailableName());
💻

Example

This example shows creating a mailable that sends a welcome email using a Blade view.

php
<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public $username;

    public function __construct($username)
    {
        $this->username = $username;
    }

    public function build()
    {
        return $this->subject('Welcome to Our Site')
                    ->view('emails.welcome');
    }
}

// In a controller or route
use App\Mail\WelcomeMail;
use Illuminate\Support\Facades\Mail;

Route::get('/send-welcome', function () {
    Mail::to('user@example.com')->send(new WelcomeMail('John'));
    return 'Welcome email sent!';
});

// Blade view (resources/views/emails/welcome.blade.php):
// <p>Hello, {{ $username }}! Welcome to our website.</p>
Output
Welcome email sent!
⚠️

Common Pitfalls

Common mistakes when creating mailables include:

  • Not importing the Mail facade before sending emails.
  • Forgetting to create or correctly reference the Blade view in build().
  • Not passing required data to the mailable constructor.
  • Trying to send mail without configuring mail settings in .env.
php
<?php
// Wrong: Missing import
Mail::to('user@example.com')->send(new WelcomeMail('John'));

// Right: Import Mail facade
use Illuminate\Support\Facades\Mail;
Mail::to('user@example.com')->send(new WelcomeMail('John'));
📊

Quick Reference

StepCommand/CodeDescription
1php artisan make:mail YourMailableNameCreate a new mailable class
2Define build() methodSet email view and subject inside the class
3Mail::to($email)->send(new YourMailableName())Send the email to recipient
4Create Blade viewDesign the email content in resources/views
5Configure mail settingsSet SMTP or mail driver in .env file

Key Takeaways

Use php artisan make:mail to create a mailable class quickly.
Customize the build() method to set the email view and subject.
Send emails with Mail::to()->send() after importing the Mail facade.
Always create and reference a Blade view for your email content.
Ensure mail settings are configured in your .env file before sending.