Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ctyler's avatar

How to test email locally

Hello, I installed my laravel application using composer and I am using WAMP server for the hosting. What is the best way to test email locally (other then Log). I have never used a local SMTP server so any input would be appreciated.

0 likes
6 replies
LaryAI's avatar
Level 58

Testing emails locally in a Laravel application can be efficiently done using Mailtrap or similar services, which simulate an SMTP server. Here’s how you can set it up:

Step 1: Sign Up for Mailtrap

First, you need to create an account on Mailtrap. It’s free for basic use, which is sufficient for local development testing.

Step 2: Configure Laravel

Once you have your Mailtrap account, you will be given a username and password for the SMTP server. You need to configure these in your Laravel environment file (.env). Update the following keys in your .env file:

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=<your-mailtrap-username>
MAIL_PASSWORD=<your-mailtrap-password>
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

Replace <your-mailtrap-username> and <your-mailtrap-password> with the credentials provided by Mailtrap.

Step 3: Update Mail Configuration

Ensure that your config/mail.php is set to use the environment variables by checking these lines:

'host' => env('MAIL_HOST', 'smtp.mailtrap.io'),
'port' => env('MAIL_PORT', 2525),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'from' => [
    'address' => env('MAIL_FROM_ADDRESS', '[email protected]'),
    'name' => env('MAIL_FROM_NAME', 'Example'),
],

Step 4: Send a Test Email

You can now test sending an email from your application. Here’s a simple example using a route:

Route::get('/send-mail', function () {
    \Mail::raw('This is a test email', function ($message) {
        $message->to('[email protected]')->subject('Test Email');
    });

    return 'Email sent!';
});

Visit this route in your browser or use a tool like Postman to trigger the email sending.

Step 5: Check Mailtrap Inbox

Go to your Mailtrap inbox; you should see the email you just sent. This allows you to test different email formats and workflows without sending actual emails to real addresses.

Conclusion

Using Mailtrap for local email testing in Laravel is straightforward and avoids the need to set up a local SMTP server. It’s a great tool for development and testing, ensuring that your email functionalities work as expected before going live.

ctyler's avatar

@Amaury I really like helo. However, it is $49 and it does not say if that is a one time, monthly, yearly. Would you happen to know. This really is a pet peeve of mine.

Amaury's avatar

@ctyler I think it is a one time. I bought a key who expired 2 years ago and I still can use it. Maybe I miss new features but it still working fine.

The price is nothing compared to the effectiveness and ease of use. And if in one or two years I have to pay something again, it's worth it.

1 like
Himmat's avatar

@ctyler

For localhost development purposes mailtrap.io is best to test the mail function, Another thing you can make setup-mailhog this is also good for localhost.

1 like

Please or to participate in this conversation.