How To Send Email In Laravel 9 Using Mailgun

WebSolutionStuff | Jul-29-2022 | Categories : Laravel

In this article, how to send email in laravel 9 using mailgun. we will learn laravel 9 to send emails using mailgun. Mailgun is an email delivery service that provides developers an easy API to send out transactional emails from their website. It's powerful APIs that enable you to send, receive, and track email effortlessly.

So, let's see, send mail using mailgun in laravel 9 and how to send email using mailgun in laravel 9.

Step 1: Create a Mailgun Account

Step 2: Setup .env file Configuration

Step 3: Create Mailable Class

Step 4: Create Blade File

Step 5: Create Route


Step 1: Create a Mailgun Account

In this step, we will create a mailgun account. So, open mailgun and create an account.

After successful registration, you will get redirected to Mailgun's dashboard page. Then, click on the "Overview" link from the left sidebar under the section labeled "Sending." On this page, you will see two ways to send emails, i.e., API and SMTP.

Click on the SMTP box and get the SMTP details.


If you want to test the emailing feature, you can add a recipient email to the "Authorized Recipients" section available on the "Overview" page in the right sidebar.

Note: The email feature is restricted to authorized recipients for free accounts.



Step 2: Setup .env file Configuration

Now, we will set up mail configuration in the .env file.

[email protected]g
MAIL_FROM_ADDRESS="[email protected]"
MAILGUN_DOMAIN=Your mailgun domain name


Step 3: Create Mailable Class

In this step, we will create a mailable class using the laravel artisan command.

php artisan make:mail TestMail

This command will create a new file app/Mail/TestEmail.php.

<? php

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class TestMail extends Mailable{

    use Queueable, SerializesModels;

    public $user;

    public function __construct($user){

        $this->user = $user;


    public function build(){

        return $this->subject('This Is Mailgun Testing Mail')



Step 4: Create Blade File

Let's create a file under app/resources/views/emails/test.blade.php and add this code.

<!DOCTYPE html>
    <title>How To Send Email In Laravel 9 Using Mailgun - Websolutionstuff</title>
    <h5>{{ $user['name'] }}</h5>
    <p>{{ $user['info'] }}</p>
    <p>Thank you</p>


Step 5: Create Route

In this step, we will create a route for sending emails.


Route::get('send-mail', function () {

    $user = [
        'name' => 'Websolutionstuff',
        'info' => 'This is mailgun example in laravel 9'

    \Mail::to('[email protected]')->send(new \App\Mail\TestMail($user));

    dd("Successfully send mail..!!");



You might also like:

Recommended Post

Featured Post

Follow us
facebooklogo github instagram twitter