Step by step guide on integration of mailtrap in laravel

Step by step guide on integration of mailtrap in laravel

Hi guys,

Mailtrap is an email marketing tool for your business. Mailtrap provides two environments first staging environment and the second production environment. You can test emails before emails are sent to particular recipients. It also checks email addresses and helps to bypass spam filters.
You can apply email drivers to your laravel application like Mailtrap, Maildrill, Mailgun, Amazon SES, Etc. You can change the credentials of the mailing system in the env file from the laravel application. So let’s check the step-by-step guide on the integration of Mailtrap in laravel.

Steps to integrate Mailtrap in laravel

  1. Create an account on the Mailtrap platform if you don’t have one. Click here to create an account. After creating an account, you need credentials to configure the laravel application. See the below screenshot:

Add details to the env file in the application.

MAIL_DRIVER="smtp"
MAIL_HOST="smtp.mailtrap.io"
MAIL_PORT=587
MAIL_USERNAME="58aXXXXXX"
MAIL_PASSWORD="782XXXXXX"
MAIL_ENCRYPTION="tls"
MAIL_FROM_NAME="${APP_NAME}"
MAIL_FROM_ADDRESS="[email protected]"
  1. To send an email, you need to create a mail class. In this class, you can configure the view will call and object of the user. Run the below command.
php artisan make:mail WelcomeMail

A new file is generated on the app\Mail directory with the name of WelcomeMail. You can see the below code after some changes.

<?php
namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializeModels;

class WelcomeMail extends Mailable
{
	use Queueable, SerializesModels;
	public $details;

	public function __construct($details)
	{
		$this->details = $details;
	}

	public function build()
	{
		return $this->subject('Welcome from Devrohit')
			->view('email.welcome');
	}	
}
?>
  1. Create a view file inside the email folder that we want to send to the user.
resources/views/email/welcome.blade.php
<html>
	<head>
		<title>Devrohit</title>	
	</head>
	<body>
		<p>Hi {{$details['name']}},</p>

		<p>Thank you for register to our portal. welcome to our family.
		{{$details['body']}}</p>
		<p>Thank you</p>
	</body>
</html>
  1. You need to trigger an email when the user registers in your application.
$details = [
	'name'=>$user->username,
	'body'=>'Our web helps you to grow your application',
];

\Mail::to($user->email)->send(new \App\Mail\WelcomeMail($details));

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 mailtrap in laravel) has clarified how to integrate mailtrap 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

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top