How to Send Email in Laravel: Step-by-Step Guide

A step-by-step guide to sending an email in Laravel: configure your mailer, create a mailable and view, send it, and fix the most common problems.

Mahzaib MirzaMahzaib MirzaJune 15, 20224 min read3 comments
How to Send Email in Laravel: Step-by-Step Guide

To send an email in Laravel, set your mail settings in .env, generate a mailable with php artisan make:mail, give it a Blade view, and send it with Mail::to($address)->send(new YourMailable). This guide walks through each step with a working example you can run locally.

If you need to email several people at once, see how to send email to multiple recipients in Laravel. If you want to send through a Gmail account, see how to send email with Gmail SMTP in Laravel.

Step 1: Configure your mail settings

Laravel sends mail through a mailer you choose in .env. It supports SMTP and several email services, such as Amazon SES, Postmark and Mailgun. The Laravel mail documentation lists the current options. For SMTP, your provider gives you the host, port, username and password:

MAIL_MAILER=smtp
MAIL_HOST=your-smtp-host
MAIL_PORT=587
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
[email protected]
MAIL_FROM_NAME="Your App"

While you are learning, set MAIL_MAILER=log instead. Laravel then writes each email to storage/logs/laravel.log and sends nothing, which is the quickest way to check that your code works.

Step 2: Create a mailable class

A mailable is a class that describes one email. Generate it with Artisan:

php artisan make:mail ContactMail

This creates app/Mail/ContactMail.php. Update it to take some data and render a view:

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    public function __construct(
        public array $details,
    ) {}

    public function envelope(): Envelope
    {
        return new Envelope(
            subject: 'Mail from Coders Vibe',
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'mail.contact',
        );
    }
}

The envelope() method sets the subject, and content() says which view to render. Public properties, like $details here, are available in the view automatically. Older tutorials, including an earlier version of this one, use a build() method for this instead.

Step 3: Create the email view

Create resources/views/mail/contact.blade.php:

<h1>{{ $details['title'] }}</h1>

<p>{{ $details['body'] }}</p>

<p>Thank you.</p>

Step 4: Send the email

The simplest way to try it is a temporary route in routes/web.php:

use App\Mail\ContactMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Route;

Route::get('/send-mail', function () {
    Mail::to('[email protected]')->send(new ContactMail([
        'title' => 'Mail from Coders Vibe',
        'body' => 'This is a test email sent from Laravel.',
    ]));

    return 'Email sent.';
});

In a real application you would send mail from a controller, a form request handler or a queued job, not from a public route. Here is the same thing in a controller:

<?php

namespace App\Http\Controllers;

use App\Mail\ContactMail;
use Illuminate\Support\Facades\Mail;

class MailController extends Controller
{
    public function send()
    {
        Mail::to('[email protected]')->send(new ContactMail([
            'title' => 'Mail from Coders Vibe',
            'body' => 'This is a test email sent from Laravel.',
        ]));

        return 'Email sent.';
    }
}
Route::get('/send-mail', [MailController::class, 'send']);

Delete the test route once you have confirmed it works. A public GET route that sends mail can be triggered by anyone who finds the URL.

Step 5: Run it and check the result

php artisan serve

Open http://127.0.0.1:8000/send-mail in your browser. If you used the log mailer, open storage/logs/laravel.log and you will see the full email. If you used a real mailer, check the receiver's inbox, and the spam folder if it does not arrive.

Fixing common problems

The email is not sent or the connection fails

Check the host, port, username and password against your email provider's documentation. After you edit .env, run php artisan config:clear so Laravel picks up the new values.

The email arrives in spam

Send from an address on a domain you have verified with your email provider, and make sure MAIL_FROM_ADDRESS uses it. Providers usually explain the DNS records they need.

The view is not found

The view name in content() has to match the file path. view: 'mail.contact' points to resources/views/mail/contact.blade.php.

Where to go next

Once a single email works, the next steps are sending to multiple recipients, which also covers queueing, and sending through Gmail SMTP.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

0 Comments

Leave a comment