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

ArjunAmrutiya
3 min readSep 17, 2023

--

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

This command generates a new Mailable class in the app/Mail directory.

Step 3: Customize the Email

Open the SampleEmail.php file generated in the previous step. Customize this Mailable class according to your email's content, subject, and recipient by modifying the build method. Here's an example:

use Illuminate\Mail\Mailable;

class SampleEmail extends Mailable
{
public function build()
{
return $this->subject('Sample Email')
->view('emails.sample'); // Create a corresponding view file in resources/views/emails
}
}

Step 4: Send the Email

To send the email, you can use Laravel’s Mail facade. For instance, to send the SampleEmail Mailable to a recipient, use the following code:

use Illuminate\Support\Facades\Mail;
use App\Mail\SampleEmail;

Mail::to('recipient@example.com')->send(new SampleEmail());

Step 5: Test in Mailtrap

When you send an email, it will be directed to your Mailtrap inbox. Log in to your Mailtrap account, open your inbox, and you’ll find the emails you sent. You can inspect the email’s content, headers, and attachments, ensuring everything operates as expected.

Conclusion:
Sending emails with Mailtrap in Laravel is a streamlined process. By following this step-by-step guide, you can effortlessly set up email sending, thoroughly test your email functionality, and verify that your email features perform flawlessly during the development phase. This approach allows you to identify and rectify any issues or errors before deploying your application, guaranteeing a seamless user experience.

Go forth and get more out of your content. Go forth and conquer Medium! (and while you’re at it, follow me on Medium! and feel free to Subscribe)

Found this post useful? Kindly tap the 👏 button below! :)

🌟 Enjoy my blogging content? Support my work by buying me a virtual coffee on BuyMeACoffee! Your contributions help fuel insightful tech articles. Join hands in making coding more accessible and exciting for all. https://www.buymeacoffee.com/arjunamrutiya🚀

--

--

ArjunAmrutiya

👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!