In this tutorial, we will see how to send mail using mailgun in laravel 8. We will see step by step send process of sending mail using mailgun in laravel 8.



Step 1: .env


Firstly, we will add configration on mail.

MAIL_DRIVER=mailgun

MAIL_HOST=smtp.mailgun.org

MAIL_PORT=587

MAIL_USERNAME=yourUserName

MAIL_PASSWORD=yourPassword

MAIL_ENCRYPTION=tls




Step 2: Get Domain and Secret


First of all create a new account on mailgun.com.

After registeration is complete active your mailgun account and click on Domails and click on Add New Domail button.

Then add your name and copy domain name and API Key




Step 3: Services


Open services.php and add mailgun configration this way :

config/services.php

'mailgun' => array(
'domain' => 'your_domain',
'secret' => 'your_secret',
),



Step 4: Routes


 First create test route for email sending.

app/Http/routes.php

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



Step 5: Controllers


Add mail function in MailGunController.php like given below:

app/Http/Controllers/MailGunController.php

<?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('mailView', $user, function($message) use ($user) {
	        $message->to($user['email']);
	        $message->subject('Testing Mailgun');
	    });

	    dd('Mail Send Successfully');
    }
}

At last create email template file for send mail



Step 6: Blade


resources/views/emails/mailEvent.blade.php

Hi, I am from rathorji.in from mailgun testing.


Run command below for quick run:

php artisan serve

Run this URL on your browser:

localhost:8000/send-mail-using-mailgun


I hope this example helps you.