Send email programmatically

In this guide we will see how to use the Email plugin to send email where you want in your app.

For this example we want to receive an email when a new article’s comment is posted and if it contains bad words.

Introduction

What we want here is to add some custom logic and call the email service when a Comment is created via the POST /comments endpoint.

To be able to do that, you have first to understand some concepts.

When you create a content type, it generates an API with the following list of endpoints.

Each of these endpoint triggers a controller action. Here is the list of controller actions that exist by default when a content type is created.

If you check the controller file of your generated API ./api/{content-type}/controller/{Content-Type}.js, you will see an empty file. It is because all the default logic is managed by Strapi. But you can override these actions with your own code.

And that is what we will do to add our custom code.

Example

To keep the code example easy to follow, we will just have a Comment content type and omit the Author and Article relations.

So lets create a Comment content type with just one Text field named content.

When the content type is created, allow the create function for the Public role.

To check if bad words are in the comment we will use bad-words node moduleSend email programmatically - 图1 (opens new window). You will have to install it in your application.

Override controller action

To customize the function that creates a comment we will have to override the create function.

First, to see the difference, let’s request POST /comment with that is fucking good! for the content attribute. You will see your comment is successfully created.

Now let’s start the customization.

Path — ./api/comment/controller/Comment.js

  1. module.exports = {
  2. async create() {
  3. return 'strapi';
  4. },
  5. };

After saving the new function, let’s restart the POST /comment request. We will see strapi as response.

Get the comment creation back

We now know the function we have to update. Let’s get back to the original function.

In the controller documentation you will find the default implementation of every action. It will help you overwrite the create logic.

Path — ./api/comment/controller/Comment.js

  1. const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
  2. module.exports = {
  3. async create(ctx) {
  4. let entity;
  5. if (ctx.is('multipart')) {
  6. const { data, files } = parseMultipartData(ctx);
  7. entity = await strapi.services.comment.create(data, { files });
  8. } else {
  9. entity = await strapi.services.comment.create(ctx.request.body);
  10. }
  11. return sanitizeEntity(entity, { model: strapi.models.comment });
  12. },
  13. };

And now the comment creation is back.

Apply our changes

We want to check if the content of the comment contains any bad words.

If it does, we want to send an email using the Email plugin

Path — ./api/comment/controller/Comment.js

  1. const { parseMultipartData, sanitizeEntity } = require('strapi-utils');
  2. const Filter = require('bad-words');
  3. const filter = new Filter();
  4. module.exports = {
  5. async create(ctx) {
  6. let entity;
  7. if (ctx.is('multipart')) {
  8. const { data, files } = parseMultipartData(ctx);
  9. entity = await strapi.services.comment.create(data, { files });
  10. } else {
  11. entity = await strapi.services.comment.create(ctx.request.body);
  12. }
  13. entity = sanitizeEntity(entity, { model: strapi.models.comment });
  14. // check if the comment content contains a bad word
  15. if (entity.content !== filter.clean(entity.content)) {
  16. // send an email by using the email plugin
  17. await strapi.plugins['email'].services.email.send({
  18. to: 'paulbocuse@strapi.io',
  19. from: 'admin@strapi.io',
  20. subject: 'Comment posted that contains a bad words',
  21. text: `
  22. The comment #${entity.id} contain a bad words.
  23. Comment:
  24. ${entity.content}
  25. `,
  26. });
  27. }
  28. return entity;
  29. },
  30. };

And tada, it works.