Send emails using Strapi

Nikhil Nanjappa
Sep 1, 2018 · 3 min read

If you’re reading this article, I’m assuming you have heard of or worked with Strapi, an open-source NodeJS Content Management Framework to build APIs. If not familiar, you can visit their homepage to get an idea. This guide also requires you to have a generated Strapi project (currently v3.0.0-alpha.14). If not already generated, you can quickly get started here to generate one.

In addition to creating & managing APIs, Strapi also provides loads of additional tools named plugins to aid in your development. One such built-in plugin is the Email plugin which as you guessed is used to shoot out emails. In this article, I will be showing you how to integrate the Email plugin to your Strapi project to send emails.

Note: If using v3.0.0-beta, the plugins folder has now been moved into node_modules. Follow this doc for more info.

Also, list of all current free and premium plugins can be found here.

Establishing some technical details before we start — we will be using Strapi’s default email provider (local email system) which as I mentioned is built-in or already installed in every generated project. If you want to use another provider, you would need to first install the corresponding npm package. You can check npmjs.com for all the email providers list.

Let’s dive into it!

Email API (already exists)

By default strapi exposes the /email endpoint which internally uses the Email plugin.

The routes for which can be found at plugins/email/config/routes.json.

plugins/email/config/routes.json

Basically, nothing for us to do here as its already taken care of!

Amend the Email plugin code

Now, it’s time we amend the plugin file to add our main code.

In plugins/email/controllers/Email.js file, under send action, change the existing line strapi.plugins.email.service.email.send(options, config) to the below code.

plugins/email/controllers/Email.js

You could additionally change the response to something like ctx.send('Email sent'). As simple as that!

Give permissions to all the actions

This part is very important as unauthenticated actions might not be accessible by the email or its underlying functions which could throw unrelated, ambiguous errors which are hard to debug.

Now that the code is ready, you have to make sure that the API consumer or user has the permissions to access all the necessary actions. For this, you would have to open up your UI Admin Panel and goto the ‘Roles & Permissions’ menu. If your using the API publicly, click on the role ‘Public’ and make sure all the actions under every plugin dropdowns of ‘Permissions’ section are checked (or atleast all the ones relating to email’s send action).

Select all actions under every plugin dropdown

Finally, Call the API

Now that we have got everything in place, it’s time we test it out by consuming the /email API we created earlier. You could use an app like Postman to make a POST request to http://localhost:1337/email. If you recall, we have mentioned that the to property of the send function would be fetched from the request body containing a to property (the address the email will be sent to). Therefore, while calling the API, make sure you’re sending a JSON object containing a to property as part of the request body. Something like

Now hit that Send button and 🚀! You will get a response message informing that your email has been sent. Congrats!!

Conclusion

Since it’s a new framework at the time of writing, it was hard for me to find solutions for each roadblock I hit. After constantly being active on Strapi’s Slack channel and going through their source code, I was finally able to get it working. All of these coupled with a lack of good documentation on this topic inspired & motivated me to consolidate all my findings and write this article. Hope it helps!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade