Laravel 8 Send Mail using Mailgun Example

In this example, I explain how to send mail using mailgun in laravel. We will explain step by step send mail using mailgun example in laravel 8. Also laravel provide mail class to send mail.

Mailgun is most popular API for send mail in laravel application. In this post, I will show you how to setting of mailgun in our laravel 8 web application. If you wish send mail in laravel using mailgun, So first create mailgun account from Mailgun Site.

Example : Send mail using mailgun in laravel

Step 1: Mailgun Configuration

After create mailgun account you can setup mailgun configuration. So let’s following screenshot:

Read Also :  How to Send Email with Attachment in Laravel 8

Laravel mailgun integration First click on dashboard menu and after click on sending domains.

how to send mail using mailgun in laravelIn this step you can verify your receiver email id and after click on STMP.

Laravel 8 mailgun integration example

In this step we will copy our username and password.

Step 2:  Edit .env file

In this step we will edit mailgun configuration. So following code.

MAIL_MAILER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandbo...
MAIL_PASSWORD=4f2....
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your_email_id
MAIL_FROM_NAME="${APP_NAME}"

Step 3: config/mail.php

'from' => [
    'address' => env('MAIL_FROM_ADDRESS', 'your_email_id'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

Step 4: Create Controller

In this step you create MailGunController.php controller in your laravel application. So let’s following command:
php artisan make:controller MailGunController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Mail;
class MailGunController extends Controller
{
    public function index()
    {
        $user = User::find(1)->toArray();
        Mail::send('mailEvent', $user, function($message) use ($user) {
            $message->to($user['email']);
            $message->subject('Hello');
        });
        dd('Mail Send Successfully');
    }
}

Step 5: Create blade file

In this step we will create mailEvent.bladde.php blade file. And add the following code.