Laravel Mailgun Integration Example

In this example, I explain how to Integration mailgun in laravel. In this post, I explain simple  example of laravel send mail using mailgun. You can multiple way to send mail in laravel.

Laravel provide mail class to send email. Also you can use the many drivers for sending email in laravel application. You can use mailtrap, Mailgun, smtp, Postmark, Amazon SES, and sendmail. You have to configure on .env file what driver you want to use.

In this tutorial, I will give you step by step to send email in laravel application using mailgun. You can make blade file and also with dynamic information for mail layout. So let’s see step by step guide and send email to your need.

Step 1: Mailgun Configuration

In this step we configuration of mailgun for send mail in your laravel application. So first create mailgun account from Mailgun Site.

Read Also :  Laravel Arr only() function Example

After creating account you will get mail configuration as mail host, mail port, mail username, mail password you can see bellow screen shot:

Domain Page: Sending->Domain

In Domain Detail page -> Click to SMTP

Get SMTP Details

Add Receiver Email for Testing: In this step you can verify your receiver email id. If receiver email id is not verified then your mail in not send. Mailgun only send email which email is verified.

add details from there bellow:

Read Also :  How to Get Config Directory Path in Laravel

.env

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}"

config/mail.php

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

Step 2: 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 3: Create blade file

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

This is web-tuts.com testing mail....

Step 4: Create route

<?php

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


Route::get('/send-mail-using-mailgun', [MailGunController::class, 'index'])->name('send.mail.using.mailgun.index');

I hope you understand laravel mailgun integration and it can help you…

Read Also :  Laravel 8 Datatables Filter with Dropdown