Skip to content

Instantly share code, notes, and snippets.

@NotTimTam
Last active March 24, 2023 17:06
Show Gist options
  • Save NotTimTam/f2cd49d2f31afd380d69c91fe2a4bd1e to your computer and use it in GitHub Desktop.
Save NotTimTam/f2cd49d2f31afd380d69c91fe2a4bd1e to your computer and use it in GitHub Desktop.
Uses nodemailer-mailgun-transport and nodemailer to send emails through mailgun's SMTP tools.
/**
* mail.js
* 2023-03-24
* Timothy Phillips
*/
const nodemailer = require("nodemailer");
const mg = require("nodemailer-mailgun-transport");
const MAILGUN_USERNAME = process.env.MAILGUN_USERNAME;
const domain = process.env.MAILGUN_DOMAIN;
const mailGunApiKey = process.env.MAILGUN_API_KEY;
const postmasterAddress = process.env.POSTMASTER_ADDRESS;
const auth = {
auth: {
api_key: mailGunApiKey,
domain,
secure: true,
},
};
const sendEmail = async (recipients, subject, data) => {
// Create email transporter.
const transport = nodemailer.createTransport(mg(auth));
transport.sendMail(
{
from: MAILGUN_USERNAME,
to: recipients,
subject: subject,
html: data,
},
(err, i) => {
if (err) {
console.error(err);
} else {
console.log(`Sent an email ("${subject}") to: ${recipients}.`);
}
}
);
};
module.exports = {
sendEmail,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment