#react-email / mjml with medusajs custom notification
1 messages · Page 1 of 1 (latest)
Yes, you can use whatever templating you want. I'm sure ses accepts html and text version. react-mjml/mjml spits out html in the end (or text).
You do it like in the docs: https://docs.medusajs.com/development/notification/create-notification-provider#sendnotification
On the screenshot where you see // TODO send email, you'll pass order into react-email which will spit out the html for you (or text).
You'll then pass the extracted html from react-mjml into the ses sending function.
ohh okay thank you. I originally used TransactionBaseService instead of AbstractNotificationService and couldn't figure it out how to pass custom arguments into templates
By the way the actual sending should happen where the // TODO send email comment is
The to and data in return is not used here. This is for resending the email. You can read about this in the link above
Okay I see, but I have a question how should I transform react-email into html plain text? I mean, how am I supposed to pass jsx elemnt into render function when my service file is .ts? or can I just change it into .tsx? e.g. I would like to pass order's items into <MyTemplate />
This is not related to medusa. First of all jsx doesn't care about file extension (js,jsx,ts,tsx).
You create your order react-email template (here MyTemplate).
You fetch the order like on my screenshot above.
You import your react template into your notification service and pass the fetched order in props
Some ses pseudocode below, as I don't know the syntax
import { MyTemplate } from './email';
import { render } from '@react-email/render';
const order = await this.orderService.retrieve(data.id)
// ses send function
await sendEmail(
{
to: order.email,
html: render(<MyTemplate order={order} />),
text: render(<MyTemplate order={order} />, {
plainText: true,
});
}
)
If it will yell something about react/jsx (it shouldn't) then do the render in the template file. Export from there two functions that will render html and text version. Import them in notification service and pass the order.
okay I will try out, thanks!
em well, I'm not able to to import anything from .tsx or .jsx file. It is yelling about using jsx elements inside .ts
I don't see the yelling. But have you read what I wrote below the code?
yep I did it and it's throwing "cannot find module" error
Looks like your path is wrong
but it's not at all or maybe I don't know something about that. In src directory I created 'emails' folder for emails templates and there is a stripe-welcome.tsx file which has default export like that:
Might be something with typescript and its config. Maybe indeed typescript jsx needs .tsx. I don't use typescript so I can't help.
okay but thanks for your tries
But the way you're doing is fine. I do the same thing in pure js. Tho I use react-mjml with nodemailer and postmark.
I'll be migrating to react-email too soon. The logic is the same.