Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Testing emails sent by Craft CMS using Mailtrap
Piotr Pogorzelski
Piotr Pogorzelski

Posted on • Originally published atcraftsnippets.com

     

Testing emails sent by Craft CMS using Mailtrap

This article was originally posted oncraftsnippets.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 totest 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 CMSapplication configuration file we can overwrite default mailer adapter that uses localSendmail program and connect to external SMTP server likeMailtrap.

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 inthis 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 likeMailgun. To connect to Mailtrap, we will need to switchSendmail mailer adapter toSMTP 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.ioSMTP_PORT=2525SMTP_USERNAME=your_usernameSMTP_PASSWORD=your_password
Enter fullscreen modeExit fullscreen mode

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

Now, openconfig/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:

<?phpreturn['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);returnCraft::createObject($config);},],];
Enter fullscreen modeExit fullscreen mode

As you can see, we overwrotemailer component of Craft CMS app.

Now, every email sent by Craft CMS will beredirected 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, butfor a few bucks you can significantly raise that number.

Further reading

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Joined

More fromPiotr Pogorzelski

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp