Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
FIDO2AppBackend/helpers/mailer.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
47 lines (43 sloc)
1.15 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Mailer service to handle email sending | |
* @module service/mailer | |
*/ | |
import nodemailer from "nodemailer"; | |
/** | |
* Sends an email | |
* @param {Array} recipients email addresses of recipients | |
* @param {String} subject email subject | |
* @param {String} bodyHtml email body | |
* @returns {Object} status of the email | |
*/ | |
const mailTo = async (recipients, { subject, bodyHtml }) => { | |
const transporter = nodemailer.createTransport({ | |
host: "mail.privateemail.com", | |
port: 465, | |
auth: { | |
user: process.env.MAILER_USER, | |
pass: process.env.MAILER_PASSWORD, | |
}, | |
}); | |
const data = { | |
from: process.env.MAILER_USER, | |
to: recipients, | |
subject: subject, | |
html: bodyHtml, | |
}; | |
return await sendEmail(transporter, data); | |
}; | |
/** | |
* Wrapper for sending emails that returns a promise | |
* @param {Object} transporter Nodemailer transporter object | |
* @param {Object} data data to be send | |
*/ | |
const sendEmail = async (transporter, data) => | |
new Promise((resolve, reject) => { | |
transporter.sendMail(data, (err, info) => { | |
if (err) reject(err); | |
console.log('Sending email: ', info) | |
resolve(info); | |
}); | |
}); | |
export default mailTo; |