Laravel 8 Send Mail using Mailgun Example

Admin   Laravel   158  2021-03-15 07:50:05

Hi Guys,

Today,I will learn you how to send mail using mailgun in laravel 8.we will show setp by step send mail using mailgun example in laravel 8.Mailgun is very popular API to send email from website. It is very fast to send mail and also it track the mail. Tracking email is very important feature of mailgun api and you can also see how much user open your mail, click on your mail too. Mailgun send mail like work gun.

In this post i would like to show you how to setting of mailgun in our laravel 8 application. In this example you can learn to send simple mail using mailgun api. If you are use mailgun for sending email then you can save loading time and you can get mail fast.

Step 1: .env

First we will add configration on mail. i added my gmail account configration. so first open .env file and bellow code:

 

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

Now, I need to add secret and domain of mailgun api configration. So first create new account in mailgun.com SignUp if you don't have before. After registeration active your mailgun account and click on Domails and click on Add New Domail button. then you can see bellow screen.

Next add name you can see bellow screen and copy domain name and API Key from like bellow image.

Step 3: Services

Now you have to open services.php and add mailgun configration this way :

config/services.php

 

'mailgun' => array(

'domain' => 'your_domain',

'secret' => 'your_secret',

),

Step 4: Routes

Here we are ready to send mail for test so 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

Next,We are add mail function in MailGunController.php file so add this way :

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 so let's create mailEvent.blade.php file in emials folder.

Step 6: Blade

resources/views/emails/mailEvent.blade.php

 

Hi, I am from from mailgun testing.

Now we are ready to run our send mail using mailgun example with laravel 8 so run bellow command for quick run:

 

php artisan serve

Now you can open bellow URL on your browser:

 

localhost:8000/send-mail-using-mailgun

It will help you...