A library to facilitate the use of nodemailer.
You may install this module using npm or Yarn.
npm i xander-email
Or yarn add xander-email
for Yarn
import xanderEmail from "xander-email"
transport.sendMail({
from: "Support <support@gmail.com>",
to: "Client <client@gmail.com>",
subject: "Test",
html: xanderEmail("view1", "./src/modules/mailer/views", { title: "xander-email" })
})
Assuming that you already know how to use nodemailer
, start creating a folder for the email's views.
This views is basically web pages (html), so you just need to create elements like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div style="background-color: black; height: 100vh;">
{{title}}
{{greenBox}}
</div>
</body>
</html>
You probably notice that this two lines are not html elements.
So... What is it?
{{title}}
{{greenBox}}
Well, those are components, which can be called from multiple different Views
.
And in reality to take an example, here we have the title
component:
<h1 style="color: white; font-family: Arial, Helvetica, sans-serif;">
{{-title}}
</h1>
There are variables too, we will take a look how to use them later.
{{-title}}
Before, you need to make sure that the components
folder is inside the views
folder. Just like this:
And the name of the components
folder needs to be in lowercase (for now).
Now we just need to create the transport of nodemailer, and when sending the Email, write on the html param something like:
import xanderEmail from "xander-email"
transport.sendMail({
from: "Support <support@gmail.com>",
to: "Client <client@gmail.com>",
subject: "Test",
html: xanderEmail("body", "./src/modules/mailer/views", { title: "xander-email" }) // <--
})
- viewName(the name of the view file that you want)
- viewsPath(the path to your views folder, and it includes the view folder itself)
- variables(an object that receives keys (name of the variables of the views) and the values of it)