Testing emails sent by Craft CMS using Mailtrap
Tags: craftcms, webdev, cms, mailtrap
Published: 2019-07-12 19:45:47

---
title: Testing emails sent by Craft CMS using Mailtrap
published: true
description: Learn how to redirect all Craft CMS emails to fake SMTP server Mailtrap, in order to test and debug your messages.
tags: craftcms,webdev,cms,mailtrap
canonical_url: http://craftsnippets.com/articles/testing-and-debugging-emails-sent-by-craft-cms-using-mailtrap
series: Craft Snippets
cover_image: http://craftsnippets.com/uploads/post_images/letter-mail-mailbox-postbox.jpg
---

This article was originally posted on [craftsnippets.com](http://craftsnippets.com?s=dt). Head there for more Craft-related articles and ready to use template components.

## About mailtrap

[Mailtrap](https://mailtrap.io/) is fake SMTP server that can be used to **test and debug** all emails sent by your website. No matter what recipient address is, Mailtrap server doesn't actually send anything - it just stores all messages to be later reviewed and analyzed.

Using Craft CMS [application configuration file](https://docs.craftcms.com/v3/config/app.html) we can overwrite default mailer adapter that uses local **Sendmail** program and connect to external SMTP server like **Mailtrap**.

## Obtaining Mailtrap credentials

First, you need to get your Mailtrap credentials that will be used to connect to server. 

Register on Mailtrap website and log in. You will see a list of your inboxes - right now there is only one. Click on gear icon on the right and copy credentials from "SMTP settings" tab.

Detailed instructions with screenshots are available in [this post on mailtrap blog](https://blog.mailtrap.io/2018/09/18/mailtrap-getting-started-guide/#first-steps-with-the-basic-mailtrap-functionality).

## Overwriting default mailer adapter

Craft CMS provides three mailer adapters:

* Sendmail - it uses server sendmail functionality. It's default adapter.
* SMTP - can be used to connect to an external SMTP server.
* Gmail - can be used to connect to Gmail server.

There are also plugins that add their own adapters like [Mailgun](https://plugins.craftcms.com/mailgun). To connect to Mailtrap, we will need to switch **Sendmail** mailer adapter to **SMTP** one.

First, let's define our credentials in `.env` file. That's where you should keep all your passwords and sensitive data.

```env
SMTP_HOST=smtp.mailtrap.io
SMTP_PORT=2525
SMTP_USERNAME=your_username
SMTP_PASSWORD=your_password
```

Don't forget to change username and password to these obtained from mailtrap.

Now, open `config/app.php` file. If you didn't modified it earlier, it should contain link to example module - it can be safely removed.

Your mailer configuration should look like this:

```php
<?php
return [
    'components' => [
        'mailer' => function() {
            $settings = \craft\helpers\App::mailSettings();
            $settings->transportType = \craft\mail\transportadapters\Smtp::class;
            $settings->transportSettings = [
                'useAuthentication' => true,
                'host' => getenv('SMTP_HOST'),
                'port' => getenv('SMTP_PORT'),
                'username' => getenv('SMTP_USERNAME'),
                'password' => getenv('SMTP_PASSWORD'),
            ];
            $config = \craft\helpers\App::mailerConfig($settings);
            return Craft::createObject($config);
        },
    ],
];
```

As you can see, we overwrote `mailer` component of Craft CMS app. 

Now, every email sent by Craft CMS will be **redirected to Mailtrap**. It also accounts for emails sent by plugins. You can quickly test if everything works properly by using "forgot password" functionality.

Once you check your Mailtrap inbox you can start inspecting your emails. You can view their preview, HTML code, raw data, analyze them for potential problems. Just keep count of how many emails you send - free Mailtrap account has a monthly limit of 500 messages, but [for a few bucks](https://mailtrap.io/pricing) you can significantly raise that number. 

## Further reading

* [Mailtrap Getting Started Guide](https://blog.mailtrap.io/2018/09/18/mailtrap-getting-started-guide)
* [Craft CMS docs - application configuration file](https://docs.craftcms.com/v3/config/app.htm)