loading...
Cover image for Testing emails sent by Craft CMS using Mailtrap

Testing emails sent by Craft CMS using Mailtrap

piotrpog profile image Piotr Pogorzelski Originally published at craftsnippets.com ・2 min read

This article was originally posted on craftsnippets.com. Head there for more Craft-related articles and ready to use template components.

About mailtrap

Mailtrap 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 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.

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. 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.

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
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 you can significantly raise that number.

Further reading

Discussion

pic
Editor guide