Skip to content

Instantly share code, notes, and snippets.

@schadokar
Last active July 19, 2020 15:02
Show Gist options
  • Save schadokar/6a7cdbcc11988c1a5f64d72e37968045 to your computer and use it in GitHub Desktop.
Save schadokar/6a7cdbcc11988c1a5f64d72e37968045 to your computer and use it in GitHub Desktop.
Basic send mail using nodejs
"use strict";
const nodemailer = require("nodemailer");
/**
* sendEmail
* @param {Object} mailObj - Email meta data and body
* @param {String} from - Email address of the sender
* @param {Array} recipients - Array of recipients email address
* @param {String} subject - Subject of the email
* @param {String} message - message
*/
const sendEmail = async (mailObj) => {
const { from, recipients, subject, message } = mailObj;
try {
// Create a transporter
let transporter = nodemailer.createTransport({
host: "smtp-relay.sendinblue.com",
port: 587,
auth: {
user: "hello@schadokar.dev",
pass: "SMTP-KEY",
},
});
// send mail with defined transport object
let mailStatus = await transporter.sendMail({
from: from, // sender address
to: recipients, // list of recipients
subject: subject, // Subject line
text: message, // plain text
});
console.log(`Message sent: ${mailStatus.messageId}`);
return `Message sent: ${mailStatus.messageId}`;
} catch (error) {
console.error(error);
throw new Error(
`Something went wrong in the sendmail method. Error: ${error.message}`
);
}
};
const mailObj = {
from: "hello@schadokar.dev",
recipients: ["me@schadokar.dev"],
subject: "Sending email by nodejs",
message: "Hello World;",
};
sendEmail(mailObj).then((res) => {
console.log(res);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment