Laravel Mailgun Setup Example

By Hardik Savani November 5, 2023 Category : Laravel Mailgun API

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 5 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.

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

.env

MAIL_DRIVER=mailgun

MAIL_HOST=smtp.mailgun.org

MAIL_PORT=587

MAIL_USERNAME=itsolutionstuff@gmail.com

MAIL_PASSWORD=mypassword

MAIL_ENCRYPTION=tls

Ok, now we 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.

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

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

config/services.php

 'mailgun' => array(

'domain' => 'itsolutionstuff.com',

'secret' => 'key-11796c09e58-056a9e975c96dd334da0dd',

),

Now we are ready to send mail for test so first create test route for email sending.

app/Http/routes.php

Route::get('mail', 'HomeController@mail');

Ok, now add mail function in HomeController.php file so add this way :

app/Http/Controllers/HomeController.php

public function mail()

{

$user = User::find(1)->toArray();

Mail::send('emails.mailEvent', $user, function($message) use ($user) {

$message->to($user->email);

$message->subject('Mailgun Testing');

});

dd('Mail Send Successfully');

}

At last create email template file for send mail so let's create mailEvent.blade.php file in emials folder.

resources/views/emails/mailEvent.blade.php

 

Hi, I am from Itsolutionstuff.com from mailgun testing.

Now try...

Shares