medusa icon indicating copy to clipboard operation
medusa copied to clipboard

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

Open NicolasGorga opened this issue 6 months ago • 8 comments

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

NicolasGorga avatar Aug 09 '24 15:08 NicolasGorga