|
| 1 | +# SendGrid |
| 2 | + |
| 3 | +Handle sending emails to customers related to orders, restock notifications, users, or custom events. |
| 4 | + |
| 5 | +[SendGrid Plugin Documentation](https://docs.medusajs.com/plugins/notifications/sendgrid) | [Medusa Website](https://medusajs.com) | [Medusa Repository](https://github.com/medusajs/medusa) |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Send emails when an event related to orders, restock notifications, or users is triggered. |
| 10 | +- Use dynamic templates in SendGrid to build the emails to be sent. |
| 11 | +- Send emails with SendGrid for custom events. |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +- [Medusa backend](https://docs.medusajs.com/development/backend/install) |
| 18 | +- [SendGrid account](https://signup.sendgrid.com/) |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +## How to Install |
| 23 | + |
| 24 | +1\. Run the following command in the directory of the Medusa backend: |
| 25 | + |
| 26 | + ```bash |
| 27 | + npm install medusa-plugin-sendgrid |
| 28 | + ``` |
| 29 | + |
| 30 | + ```bash |
| 31 | + yarn add medusa-plugin-sendgrid |
| 32 | + ``` |
| 33 | + |
| 34 | +2\. Set the following environment variable in `.env`: |
| 35 | + |
| 36 | + ```bash |
| 37 | + SENDGRID_API_KEY=<API_KEY> |
| 38 | + SENDGRID_FROM=<SEND_FROM_EMAIL> |
| 39 | + # IDs for different email templates |
| 40 | + SENDGRID_ORDER_PLACED_ID=<ORDER_PLACED_TEMPLATE_ID> # example |
| 41 | + ``` |
| 42 | + |
| 43 | +3\. In `medusa-config.js` add the following at the end of the `plugins` array: |
| 44 | + |
| 45 | + ```ts |
| 46 | + const plugins = [ |
| 47 | + // ..., |
| 48 | + { |
| 49 | + resolve: `medusa-plugin-sendgrid`, |
| 50 | + /** @type {import('medusa-plugin-sendgrid').PluginOptions} */ |
| 51 | + options: { |
| 52 | + api_key: process.env.SENDGRID_API_KEY, |
| 53 | + from: process.env.SENDGRID_FROM, |
| 54 | + templates: { |
| 55 | + order_placed_template: process.env.SENDGRID_ORDER_PLACED_ID, |
| 56 | + }, |
| 57 | + localization: { |
| 58 | + "de-DE": { // locale key |
| 59 | + order_placed_template: |
| 60 | + process.env.SENDGRID_ORDER_PLACED_ID_LOCALIZED, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + ] |
| 66 | + ``` |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +## Test the Plugin |
| 71 | + |
| 72 | +1\. Run the following command in the directory of the Medusa backend to run the backend: |
| 73 | + |
| 74 | + ```bash |
| 75 | + npm run start |
| 76 | + ``` |
| 77 | + |
| 78 | +2\. Place an order using a storefront or the [Store APIs](https://docs.medusajs.com/api/store). You should receive a confirmation email. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +## Additional Resources |
| 83 | + |
| 84 | +- [SendGrid Plugin Documentation](https://docs.medusajs.com/plugins/notifications/sendgrid) |
0 commit comments