medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.62k stars 2.42k forks source link

Sendgrid plugin not sending emails for events with templates configured in plugin options #8539

Open NicolasGorga opened 1 month ago

NicolasGorga commented 1 month ago

Bug report

Describe the bug

When installing the medusa-plugin-sendgrid plugin, configuring a verified single sender in Sendgrid as per Sender Verification, creating en email template and setting the environment variables needed for configuring the plugin to send emails on order_placed events, no emails are sent after generating an order.

Running 'yarn dev' shows warning after initializing plugins:

warn: The subscriber in E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\subscribers\order.js is missing a config. warn: The subscriber in E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\subscribers\restock.js is missing a config. warn: The subscriber in E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\subscribers\user.js is missing a config.

Tried calling endpoint 'sendgrid/send' fro further debugging and got the following error stack, which seems as if the body of the post request is resolved to undefined, which is what gets passed to the sendEmail function in send-email.js:

TypeError: Cannot read properties of undefined (reading 'isMultiple') at MailService.send (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\@sendgrid\mail\src\classes\mail-service.js:184:23) at _callee5$ (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:384:39) at tryCatch (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:17:1360) at Generator. (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:17:4177) at Generator.next (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:17:2211) at asyncGeneratorStep (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:18:103) at _next (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:19:194) at E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:19:364 at new Promise () at SendGridService. (E:\proyectos personales\centr0\temp\sendgrid_test\sendgrid_test\node_modules\medusa-plugin-sendgrid\services\sendgrid.js:19:97)

To discard it being a configuration problem, i created a simple GET custom api route, like this:

`import { MedusaRequest, MedusaResponse } from "@medusajs/medusa";

export const GET = async (req: MedusaRequest, res: MedusaResponse) => { const sendgridService = req.scope.resolve('sendgridService'); const data = await sendgridService.sendNotification('order.placed', {id: 'order_01J4T36SJQ60FH3NT9ATC12S8G'}); res.status(200).send(data); }`

And confirmed the email is sent correctly, so it appears to be a problem with the emission / subscription to the event.

image

System information

Medusa version (including plugins): Medusa: 1.20.9 and medusa-plugin-sendgrid: 1.3.13 Node.js version: 20 Database: postgres Operating system: Windows Browser (if relevant):

Steps to reproduce the behavior

  1. Create a new Medusa app following Get Started
  2. Install plugin with: yarn add medusa-plugin-sendgrid
  3. Follow steps at Medusa Sendgrid to configure verified Sendgrid Single Sender, set .env variables and set plugin options in medusa-config.js
  4. Create an email template, under dynamic templates of your Sendgrid account
  5. Set the 'order_placed_template' of the plugin options in medusa-config.js to the value of the id of the template available in Sendgrid
  6. Start backend with 'yarn dev'
  7. Confirm you see warnings after plugins intialization in logs
  8. Log in to Admin Dashboard
  9. Generate a draft order
  10. Confirm the p[ayment, which generates the order that should trigger the configured event
  11. Verify the receiver of the email, configured in the plugin options doesn't receive the email
  12. Make a POST request to route 'sendgrid/send' with required body as seen in send-email.js
  13. Verify you see error stack trace mentioned in bug description section

Expected behavior

After linking an event to a Sendgrid template in plugin options, when event is emitted, the email to be sent

When making a POST request to 'sendgrid/send' api route, with required body properties, for the email to be sent

Screenshots

image image image

irab commented 1 month ago

What is your Redis configuration?

AFAIK a full Redis setup is required for SendGrid to function and I can see the "A fake Redis instance will be used" message being logged.

NicolasGorga commented 1 month ago

Hey @irab yeah I was trying to use it with the local-event-bus, since nowhere in the docs I found a statement mentioning it only works with Redis configured.

After setting up a Redis instance it is working with medusa-plugin-sendgrid-typescript, haven't tried with this one since it discouraged me the notion i got that it is not being actively maintained.

I guess the issue should remain Open until they make a correction to the docs, it will hopefully save someone else time, I spent a few days banging my head with something that "should" work by following the docs instructions

olivermrbl commented 3 weeks ago

@NicolasGorga, the logs you see about the subscribers missing a config are misleading. The subscribers are in fact getting registered, but because they use the old subscriber syntax, our loader will complain.

I just ran through all of the outlined steps and was unfortunately unable to reproduce your issue. What logs are you seeing when the order is placed?

raphaelbernhart commented 1 week ago

Same here. Currently not working.

Logs when order is placed: image

carlosxmerca commented 1 day ago

Hi @NicolasGorga, Thank you a lot! after I had Redis configured it worked fine