Send emails using mjml
.
In order to have a design structure, is necessary to set the folder containing the .mjml
files.
{
...config,
templates: `${__dirname}/design`,
}
The library will search for design templates, and overrides at the send time, always in this folder.
We use Sendgrid mail library in case you have an API key already created.
yarn add sendjml @sendgrid/mail
const { sendgrid } = require('sendjml')
sendgrid.config({
apiKey: process.env.SG_API_KEY,
from: 'mymail@mymal.com',
templates: `${__dirname}/design`,
})
const { sendgrid } = require('sendjml')
await sendgrid.send({
to: 'anothermail@anothermail.com',
subject: 'This is an email sent using Sendgrid',
text: 'Sendgrid Lorem ipsum',
})
In order to send emails using Gmail as SMTP, we use nodemailer. Sending this way is necessary to create an OAuth keys in a Google Cloud Platform application following this guide as an example.
yarn add sendjml nodemailer googleapis
In this case, the from
field is not necessary because Gmail uses the one set as gmailUser
.
const { gmail } = require('sendjml')
gmail.config({
gmailUser: process.env.MAIL_USERNAME,
oauthClientId: process.env.OAUTH_CLIENTID,
oauthClientSecret: process.env.OAUTH_CLIENT_SECRET,
oauthRefreshToken: process.env.OAUTH_REFRESH_TOKEN,
})
const { gmail } = require('sendjml')
await gmail.send({
to: 'anothermail@anothermail.com',
subject: 'This is an email sent using Sendgrid',
text: 'Sendgrid Lorem ipsum',
})