May 13, 2022
.
Admin
Hi Guys,
Today, I will learn you how to send mail using mailgun in laravel 9. we will show step-by-step send mail using the mailgun example in laravel 9. Mailgun is a very popular API to send email from a website. It is very fast to send mail and also it track the mail. Tracking email is a very important feature of mailgun api and you can also see how many users open your mail, and click on your mail too. Mailgun send mail like a working gun.
In this post, I would like to show you how to setting of mailgun in our laravel 9 application. In this example, you can learn to send simple mail using mailgun api. If you are using mailgun for sending email then you can save loading time and you can get mail fast.
Step 1: Download Laravel
Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.
composer create-project laravel/laravel example-app
Step 2: Mail Configuration
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=yourUserName
MAIL_PASSWORD=yourPassword
MAIL_ENCRYPTION=tls
Step 3: 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 the name you can see below the screen and copy the domain name and API Key from like bellow image.
Step 4: Add Services
Now you have to open services.php and add mailgun configuration this way :
config/services.php
'mailgun' => array(
'domain' => 'your_domain',
'secret' => 'your_secret',
),
Step 5: Add Route
Here we are ready to send mail for a test so first create a test route for email sending.
routes/web.php
<?php
use App\Http\Controllers\MailGunController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/send-mail-using-mailgun', [MailGunController::class, 'index'])->name('send.mail.using.mailgun.index');
Step 6: Add Controller
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
{
/**
* Write code on Method
*
* @return response()
*/
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 the mailEvent.blade.php file in the emials folder.
Step 7: Add Blade File
resources/views/emails/mailEvent.blade.php
Hi, I am from nicesnippets.com from mailgun testing.
Run Laravel App:
All steps have been done, now you have to type the given command and hit enter to run the laravel app:
php artisan serve
Now, you have to open web browser, type the given URL and view the app output:
http://localhost:8000/send-mail-using-mailgun
It will help you...
#Laravel 9