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 testing of emails before email sends 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 integration of mailtrap in laravel.
Steps to integrate Mailtrap in laravel
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="support@company.com"
- To send an email, you need to create a mail class. In this class, you can configure view will call and object of user. Run the below command.
php artisan make:mail WelcomeMail
New file generates on 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');
}
} ?>
- 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>
- 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 user will get welcome mail when they register for your application. Do you want to check which mail tools 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
Originally published at https://www.devrohit.com on March 23, 2022.