Laravel Mailgun Integration for Sending Email Example

Laravel Mailgun Integration Example; In this tutorial you will learn how to send Emails with Mailgun in Laravel application. Using this example you will understand laravel mailgun setup and integration to send email using mailgun servecies.

Laravel provide us mail class to send email wheres you can use several drivers for sending email in laravel. You can use mailtrap, smtp, Mailgun, Postmark, Amazon SES, and sendmail. You just need to configure on .env file what driver you want to use.

This tutorial gives you step by step instruction to sending email in laravel using mailgun. You can use this example in your laravel 5, laravel 6, laravel 7, laravel 8 or laravel 9 apps.

What is Mailgun?

Mailgun is an email delivery service that provides developers an easy API to send out transactional emails from their website. And the Mailgun service is already built into the core of Laravel!

Sending Emails with Laravel and Mailgun

Follow the following steps to sending email in laravel using the mailgun service;

  • Step 1: Install Laravel App
  • Step 2: Connect App to Database
  • Step 3: Mailgun Configuration
  • Step 4: Make Routes
  • Step 5: Send Mail From Controller
  • Step 6: Create Blade File
  • Step 7: Run Development Server

Install Laravel App

First run the following command in terminal to install laravel fresh app for laravel sending email example with mailgun.

composer create-project --prefer-dist laravel/laravel laravel-mailgun

Go into the app:

cd laravel-mailgun

Connect APP to Database

Open the .env file and add your database credentials such as database name, username equally important password:

DB_USERNAME=db user name
DB_PASSWORD=db password

Mailgun Configuration

Next, you need to create account on mailgun site. So click bellow link to create account in mailgun account so we can use to sending email using mailgun service:

Mailgun Site

After creating account you will get mail configuration as mail host, mail port, mail username, mail password.

Now get all your mailgun details and put in your envoirement file (.env) just like below.



Make Routes

Open routes/web.php file and define the route to impement the laravel mailgun email sending example;


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\EmailController;

Route::get('send-email', [EmailController::class, 'sendEmail']);

Send Mail From Controller

Now create a new controller named as EmailController using the following command.

php artisan make:controller EmailController

After running this command you will find new file in this path “app/Http/Controllers/PostController.php”. Open this file and put the exiting below code on it;



namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Mail;

class EmailController extends Controller
    public function sendEmail()
        $data = [
            'subject' => 'Test subject',
            'title' => 'Test mail send from',
            'body' => 'This is for testing email using mailgun'

        Mail::send('email-template', $data, function($message) use ($data) {
        dd("Email is Sent.");

Create Blade File

Now we need to create a email blade file which will saw in your mail service provider inside view. Lets create and put the below code on it.


        <div class="container">
             <div class="row justify-content-center">
                 <div class="col-md-8">
                     <div class="card">
                         <div class="card-header">Welcome!</div>
                           <div class="card-body">
                               <h1>{{ $title }}</h1>
                               <p>{{ $body }}</p>

Run Development Server

Finnaly, the laravel mailgun email sending example is completed, now we need to test, so we need to run the project using the serve command:

Run App:

php artisan serve

Open Link: Now open the link and send email.


Test your requirement and get email in your mailgun account. I hope this example help you…

Leave a Comment