strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
62.97k stars 7.92k forks source link

Errors from provider-email-sendgrid are _very_ confusing #14291

Open Paratron opened 2 years ago

Paratron commented 2 years ago

I found a bug in provider-email-sendgrid which caused very confusing error responses when trying to create a user.

Describe the bug

I was trying to register a new user through the REST API. The API response was status 400: Forbidden. The weird thing: a database record for the new user was created! No confirmation mail was sent out.

At first, I thought it could not be the email plugin because when I hit the "Send test mail" button in the admin UI, I received a test mail just fine.

In the end, it still turned out to be the sendgrid email plugin. What I did not take into consideration was that even tough in /config/plugins.js I configured a defaultFrom address, this is eventually overridden through a default address stored with the E-Mail Address Confirmation template. There is no-reply@strapi.io set by default which caused sendgrid to refuse to send the mail because thats not a valid sender address for my account.

Now the problem is that in fact, sendgrid response with 400: forbidden here, its still a bug that THIS EXACT error bubbles up until the REST endpoint of strapi ultimatively returns this to the browser. There is nothing in the console of the strapi node process.

The plugin throws the error object, here: https://github.com/strapi/strapi/blob/main/packages/providers/email-sendgrid/lib/index.js#L29

However, the code upwards looks only for the message property of the error, which is in fact "forbidden". But there is much more information here:

{
  "message": "Forbidden",
  "code": 403,
  "response": {
    "body": {
      "errors": [
        {
          "message": "The from address does not match a verified Sender Identity. Mail cannot be sent until this error is resolved. Visit https://sendgrid.com/docs/for-developers/sending-email/sender-identity/ to see the Sender Identity requirements",
          "field": "from",
          "help": null
        }
      ]
    }
  }
}

The sendgrid plugin should actually look into the error object coming from sendgrid and at least provide some meaningful message in the node console instead of throwing the whole object upwards to code which is not aware of the additional information inside the response property.

So what I think should be done (thats full up to you tough):

Steps to reproduce the behavior

  1. Install and configure the sendgrid plugin
  2. Leave confirmation template with the default strapi address
  3. Register a new user with enabled email confirmation
  4. See error 400: Forbidden - no user data is returned, user record still created

Expected behavior

The data of the created user should be sent in the REST api response, even tough no confirmation mail could be sent. There should be meaningful error messages in the Strapi process logs.

vikasvmads commented 2 years ago

Hi, If the issue is open, I want to work on it. :)

bayramorhan commented 1 year ago

Up up +++

I am having this major issue when trying to send confirmation e-mail on register with SendGrid

"@strapi/provider-email-sendgrid": "^4.6.1",

email: {
      config: {
        provider: 'sendgrid',
        providerOptions: {
          apiKey: env('SENDGRID_API_KEY'),
        },
        settings: {
          defaultFrom: 'my verified sendgrid email',
          defaultReplyTo: 'my verified sendgrid email',
          testAddress: '*******',
        },
      },
    }

But strapi using Administration Panel no-reply@strapi.io as from email instead my verified sendgrid email in email.settings.defaulfFrom at plugins.js and it returns 400 Error but still creates the user.

It works if I change this from: from || settings.defaultFrom,

to from: settings.defaultFrom,

in node_modules/@strapi/provider-email-sendgrid/lib/index.js

Using this method with Nuxt 3

"@nuxtjs/strapi": "^1.7.1",

// Strapi Register Method
const { register } = useStrapiAuth();
Paratron commented 1 year ago

You need to set your from mail inside the strapi admin panel settings as a workaround.

bayramorhan commented 1 year ago

You need to set your from mail inside the strapi admin panel settings as a workaround.

Which version you are using? Because there is no such setting in the admin panel. Even tho its not clear and doesnt exists on the docs

Paratron commented 1 year ago

Granted, UX wise its super strange but you need to go to the Email Templates section. Each template has its own email send address for some reason - and that defaults to no-reply@strapi.io

grafik

bayramorhan commented 1 year ago

Granted, UX wise its super strange but you need to go to the Email Templates section. Each template has its own email send address for some reason - and that defaults to no-reply@strapi.io

grafik

yes I think this is exactly what you were trying to say in your issue description :)

jj-theInvincible commented 1 year ago

Up up +++

I am having this major issue when trying to send confirmation e-mail on register with SendGrid

"@strapi/provider-email-sendgrid": "^4.6.1",

email: {
     config: {
       provider: 'sendgrid',
       providerOptions: {
         apiKey: env('SENDGRID_API_KEY'),
       },
       settings: {
         defaultFrom: 'my verified sendgrid email',
         defaultReplyTo: 'my verified sendgrid email',
         testAddress: '*******',
       },
     },
   }

But strapi using Administration Panel no-reply@strapi.io as from email instead my verified sendgrid email in email.settings.defaulfFrom at plugins.js and it returns 400 Error but still creates the user.

It works if I change this from: from || settings.defaultFrom,

to from: settings.defaultFrom,

in node_modules/@strapi/provider-email-sendgrid/lib/index.js

Using this method with Nuxt 3

"@nuxtjs/strapi": "^1.7.1",

// Strapi Register Method
const { register } = useStrapiAuth();

Same issue, it just doesn't seem to work. Here's what I did:

- Update my .config/plugins.js file

module.exports = ({ env }) => ({ // ... email: { config: { provider: 'sendgrid', // For community providers pass the full package name (e.g. provider: 'strapi-provider-email-mandrill') providerOptions: { apiKey: env('SENDGRID_API_KEY'), }, settings: { defaultFrom: 'verifiedmail@myorg.com', defaultReplyTo: 'verifiedmail@myorg.com', testAddress: 'verifiedmail@myorg.com', }, }, }, // ... });


But after following these steps, here's the weird thing:
- It still doesn't show my email settings in configuration 
![image](https://user-images.githubusercontent.com/23653713/228090275-5488513b-f227-4eb0-99ef-0acfe20824ea.png)
- When I search for providers, it shows that sendgrid is installed
![image](https://user-images.githubusercontent.com/23653713/228090388-1f80da05-0af4-419b-8d1f-e079e443bd0a.png)
ralyodio commented 1 year ago

anyone know how to fix this?