0

I need to do a new api in order to send an email with sendgrid. I followed the official doc and other examples so I did:

config/plugins

module.exports = ({ env }) => ({
  email: {
    provider: 'sendgrid',
    providerOptions: {
      apiKey: env('SENDGRID_API_KEY'),
    },
    settings: {
      defaultFrom: 'juliasedefdjian@strapi.io',
      defaultReplyTo: 'juliasedefdjian@strapi.io',
    },
  },
});

then I did a new folder named email in api folder

api/email/config/routes.json

{
  "routes": [
    {
      "method": "POST",
      "path": "/email",
      "handler": "email.index",
      "config": {
        "policies": []
      }
    }
  ]
}

finally under api/email/controllers/email.js

const { default: createStrapi } = require('strapi');

module.exports = {
  index: async (ctx) => {
    //build email with data from ctx.request.body

    await createStrapi.plugins['email'].services.email.send({
      to: 'email@email.com',
      from: 'email@email.com',
      replyTo: 'email@email.com',
      subject: 'test',
      text: 'test',
    });
ctx.send('Email sent!');
  },
};

The real problem is that /email api returns me a 403 even if I did this from the dashboard:

enter image description here

I have done many APIs with strapi but I have never sent emails with it.

Is there a way to add permissions from the code? I have to say that if I use GET method it works, but I need to do it with a POST method, which doesn't. Did I miss something?

  • This error could occur if the *FROM* address is not verified by the sendgrid. Does [this](https://stackoverflow.com/questions/61149142/node-js-sendgrid-mail-403-forbidden-error) help? – Rahul Singh Mar 24 '21 at 21:29
  • Yes thank you Singh! There was something wrong with Sendgrid! –  Mar 25 '21 at 08:17

0 Answers0