Member-only story
Send Email with Mailtrap in Laravel: A Step-by-Step Guide

Introduction:
Email communication is an integral part of many web applications. Laravel, a popular PHP web application framework, makes it easy to send emails. In this step-by-step guide, we’ll explore how to send emails using Mailtrap, a powerful tool for email testing, with Laravel. Mailtrap allows you to test your email functionality in a safe environment without sending emails to real recipients.
Prerequisites:
Before we get started, make sure you have the following prerequisites:
Laravel Installed:
You should have Laravel installed on your development machine. If not, you can follow the official Laravel installation guide at https://laravel.com/docs/8.x/installation.
Mailtrap Account:
Create a free Mailtrap account if you don’t have one already. You can sign up at https://mailtrap.io/.
Access to Your Laravel Project:
Open your Laravel project in your preferred code editor or IDE.
Now, let’s dive into the step-by-step process of sending emails using Mailtrap in Laravel.
Step 1: Set Up Mailtrap Credentials
First, log in to your Mailtrap account and create a new inbox. Once you have an inbox, Mailtrap provides SMTP credentials that you’ll need to use in your Laravel application. These credentials are used to route emails to your Mailtrap inbox instead of sending them to real recipients.
In your Laravel project, open the .env
file and update the MAIL_DRIVER
, MAIL_HOST
, MAIL_PORT
, MAIL_USERNAME
, and MAIL_PASSWORD
variables to match your Mailtrap SMTP settings. Here's an example of how your .env
file might look:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_mailtrap_username
MAIL_PASSWORD=your_mailtrap_password
MAIL_ENCRYPTION=tls
Step 2: Create an Email
In Laravel, emails are created as Mailable classes. To create an email, you can use the following Artisan command:
php artisan make:mail SampleEmail