DEV Community

Cover image for Sending Emails in Laravel with Mailtrap: A Beginner's Guide
Rohit Dhiman
Rohit Dhiman

Posted on

Sending Emails in Laravel with Mailtrap: A Beginner's Guide

Ever tried sending a test email in your Laravel app and ended up spamming your real inbox?
If yes, then you’ll love what Mailtrap can do for you.

In this tutorial, we’ll walk through how to send and test emails safely in Laravel using Mailtrap, step by step.


What Is Mailtrap?

Mailtrap is a fake SMTP server designed for developers.
Instead of sending real emails, it captures them in a virtual inbox where you can preview, debug, and validate everything safely.

Why use Mailtrap?

  • ✅ No accidental spam to real users
  • ✅ See email headers, body, and attachments
  • ✅ Test HTML and plain text versions
  • ✅ Free tier for small projects

It’s like a playground for testing emails, no risk, no mess!


Step 1: Set Up a New Laravel Project

If you don’t already have a project, open your terminal and run:

composer create-project laravel/laravel email-demo
cd email-demo
Enter fullscreen mode Exit fullscreen mode

Once installed, you can serve your app using:

php artisan serve
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Mailtrap Account

Head over to Mailtrap.io and sign up for a free account.

  • Create a new Inbox
  • Go to SMTP Settings
  • Copy your host, port, username, and password

You’ll use these credentials in your Laravel .env file.


Step 3: Configure Your .env File

Open .env and update the mail configuration with your Mailtrap details:

MAIL_MAILER=smtp
MAIL_HOST=sandbox.smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=hello@example.com
MAIL_FROM_NAME="${APP_NAME}"
Enter fullscreen mode Exit fullscreen mode

Save the file.


Step 4: Create a Mailable Class

Laravel’s Mailable class makes sending emails super simple.

Run this command:

php artisan make:mail WelcomeMail
Enter fullscreen mode Exit fullscreen mode

Then open app/Mail/WelcomeMail.php and edit it:

<?php

namespace App\Mail;

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

class WelcomeMail extends Mailable
{
    use Queueable, SerializesModels;

    public function build()
    {
        return $this->subject('Welcome to Our App!')
                    ->view('emails.welcome');
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Create the Email View

Now let’s make a simple Blade view.
Create a file at resources/views/emails/welcome.blade.php:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Email</title>
</head>
<body>
    <h1>Hey there!</h1>
    <p>Thanks for joining our app. We’re thrilled to have you on board 🚀</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Step 6: Send a Test Email

To test your setup, add a route in routes/web.php:

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

Route::get('/send-mail', function () {
    Mail::to('test@example.com')->send(new WelcomeMail());
    return 'Email sent!';
});
Enter fullscreen mode Exit fullscreen mode

Now open your browser and visit:
http://127.0.0.1:8000/send-mail

If everything’s correct, you’ll see your test email in your Mailtrap inbox!


Step 7: Preview & Debug

Go to your Mailtrap Inbox.
You’ll see your test email with all details: subject, headers, and rendered HTML.

This is super helpful for debugging layouts, checking links, or testing dynamic content before going live.


Common Mistakes to Avoid

Using Mailtrap in production, it’s only for testing!
Wrong credentials in .env file
✅ Always clear cache after updating .env:

php artisan config:clear
Enter fullscreen mode Exit fullscreen mode

Quick Recap

Here’s what we learned today:
✅ What Mailtrap is and why it’s useful
✅ How to configure Laravel’s mail settings
✅ How to create and send a test email
✅ How to safely preview your emails

With this setup, you’ll never worry about spamming real users again.


Final Thoughts

Sending emails is a fundamental part of almost every web app whether it’s for password resets, confirmations, or newsletters.

Mailtrap makes testing this process safe, clean, and efficient.

If you found this helpful, try adding your own email templates next or integrate dynamic data like user names, order IDs, etc.

Keep coding, keep learning. 💻


#Laravel #Mailtrap #WebDevelopment #BackendDevelopment #PHPDevelopers #LaravelBeginners #CodingJourney #OOPsJunction #LearnLaravel #SoftwareEngineering #WebDevCommunity

Top comments (0)