Most of the apps send out emails, but what is the best way to do it ?
Well, I don’t know about the best, but let’s explore this solution.
We want emails to:
- Be able to be found easily in a centralized place. It’s very important they are not spread all over the app so we have control over them.
- Be able to have a nice layout for everyone of them
- Be easy to develop and easy to maintain
So, if we want to keep them centralized, it’s clear that we must use Events. Therefore, whenever we want to send an email we need to dispatch an event.
So, how do we render our emails ? React baby!
So we want to create a service that sends an email based on a React template. Let’s see how it would look like:
|
|
Cool I think it’s self descriptive what this function does, here is how it would look in a real-life scenario:
|
|
And to actually send it:
|
|
Now if you want to create a layout enveloping your template it’s quite easy, you would do something like:
|
|
And because we want to keep the send email as flexible as possible we will use the layout inside our templates:
|
|
Both Layout
and ItemNewEmail
can change in anyway you like, depending on your context.
Of course you can have multiple layouts, or no layout, it’s up to the email template to decide, not the send function, don’t forget this.
Isn’t this easy and clean ?