Your cart is currently empty!
Step by step guide on integration of mailgun in laravel
Hi guys,
Mailgun provides RESTFul API for service, installing no other libraries and provisioned with a sandbox domain. You can enable a tracking system for the sandbox domain. Laravel provides several drivers like Mailtrap, Mailgun, Amazon SES, Etc. Send an email the way that best fits your business with either SMTP or RESTful API. So let’s check the step-by-step guide on integrating Mailgun email tools in the laravel application. You can edit your application env file with your Mailgun credentials.
Step-by-step integration of Mailgun in laravel
- Create an account on the Mailgun Platform
Before integration, you need to create an account on Mailgun. Click Here to create one.
After creating an account on Mailgun, you will get mail configuration from Mailgun. See the screenshots to get the mail configuration.
Update your env file in your application.
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster@sandboxXXXXXXX
MAIL_PASSWORD=XXXXXXXX
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"
- Create Mail class
You can create a mail class to mail to the recipient. Run the following command.
php artisan make:mail WelcomeMail
Above command, create a mail class on the app/Mail directory. You can modify the code corresponding to your requirements.
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class WelcomeMail extends Mailable
{
use Queueable, SerializesModels;
public $details;
public function __construct($details)
{
$this->details = $details;
}
public function build()
{
return $this->subject('Welcome To Devrohit')
->view('emails.welcomeMail');
}
}
- Create Blade View
You can create a blade file inside the emails folder. They displayed this file in the recipient’s inbox.
resources/views/emails/welcomeMail.blade.php
<html>
<head>
<title>Welcome to Devrohit
</head>
<body>
Hi {{$details['name']}},
Thank you for registering our portal. You will get the best knowledge on this platform.
{{ $deails['body']}}
<p>Thank you</p>
</body>
</html>
- Trigger mail to a recipient
You can call the send method when a user registers to your portal.
$details = [
'name'=> $user->username,
'body'=>'ABCD..........XYZ'
];
\Mail::to($user->email)->send(new \App\Mail\WelcomeMail($details));
You can add the above code to the register function. Now your users will get welcome mail when they register for your application. You measure business impact on your email campaigns with a/b testing. you manage the inbox placement of mail
Now your users will get welcome mail when they register for your application. Do you want to check which mail tools are best for your application? Click Here. I hope that this post (step-by-step guide on integration of mailgun in laravel) has clarified how to integrate mailgun in laravel. If you have any questions, please leave a comment and I will respond as soon as possible.
Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers