{{craftSnippets}}
Home Articles Plugins Starter theme Components on Github Gists About
  • Home
  • Articles
  • Testing emails sent by Craft CMS using Mailtrap
Posted on Mar 08, 2019 by Piotr Pogorzelski

Testing emails sent by Craft CMS using Mailtrap

Mailtrap mailer config on github gists
Learn how to redirect all Craft CMS emails to fake SMTP server Mailtrap, in order to test and debug your messages.
Table of contents:
  • About mailtrap
  • Obtaining Mailtrap credentials
  • Overwriting default mailer adapter
  • Further reading

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 #

  • Mailtrap Getting Started Guide
  • Craft CMS docs - application configuration file

TAGS:
#email #debugging
If you want to get latest updates on Craft CMS tutorials and components, follow me on Twitter or subscribe to RSS feed.
Quick links for this article:
Mailtrap mailer config on github gists
Articles on blog:
  • Building reactive Craft Commerce product page with Sprig plugin
  • Dynamically generated PDF attachments for Freeform submissions
  • Using template hooks in Craft CMS
  • Alpine JS modal component for Craft CMS
  • Using template UI elements to extend Craft CMS control panel
  • Matrix within a Matrix - possible solutions for Craft CMS
  • Universal email template for Craft CMS
  • Creating attributes table from entry fields in Craft CMS
  • Namespacing forms in Craft CMS
  • Creating map-based navigation for Craft CMS
  • Placeholder image macro for Craft CMS
  • Building AJAX contact form with Craft CMS
  • Using incognito field plugin for Craft CMS
  • Email footer creator made with Craft CMS
  • Infinite scrolling and lazy loading with Craft CMS
  • Using Javascript in Twig templates with Craft CMS
  • Twig templating tips and tricks for Craft CMS
  • Basic SEO functionality for Craft CMS
  • Working with dates in Craft CMS templates
  • Working with SVG images in Craft CMS templates
  • Responsive and lazy-loaded youtube videos with Craft CMS
  • Debugging and inspecting Twig templates in Craft CMS
  • Creating article excerpts with Twig component in Craft CMS
  • Adding favicons to Craft CMS website
  • Truncating text with Twig macros in Craft CMS
  • Universal language switcher for Craft CMS
  • Read time macro for Craft CMS
  • Using attr() function to render HTML attributes in Craft CMS
  • Building dynamic, AJAX based pagination for Craft CMS
  • How to add Disqus comments to Craft CMS website
  • Ellipsis pagination component for Craft CMS
  • Converting email addresses into links using Twig macro
  • Breadcrumb created from URL for Craft CMS
  • Best developer-oriented Craft CMS plugins
  • Search autocomplete component for Craft CMS
  • RSS feed - template component for Craft CMS
  • Testing emails sent by Craft CMS using Mailtrap
  • Quick edit link - Twig component for Craft CMS
  • Filtering entries in control panel using Searchit plugin
  • Fetching routes into Twig templates in Craft CMS


Building reactive Craft Commerce product page with Sprig plugin

Dynamically generated PDF attachments for Freeform submissions

Using template hooks in Craft CMS

Alpine JS modal component for Craft CMS

Using template UI elements to extend Craft CMS control panel

Matrix within a Matrix - possible solutions for Craft CMS

Universal email template for Craft CMS

Creating attributes table from entry fields in Craft CMS

Copyright ©2022 Piotr Pogorzelski