Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active December 2, 2017 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/9e9c2d160b5b60740908ce2e3ea5ef80 to your computer and use it in GitHub Desktop.
Save magician11/9e9c2d160b5b60740908ce2e3ea5ef80 to your computer and use it in GitHub Desktop.
How to send an email from Node.js using Gmail
module.exports = {
email: {
address: 'youraddress@gmail.com',
password: 'clever-password'
}
};
/*
This module will accept a recipient email address, subject and body of email (in HTML format),
and send it via a Gmail address.
*/
const nodemailer = require('nodemailer');
const config = require('../config');
module.exports = (to, subject, html) => {
return new Promise((resolve, reject) => {
const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: config.email.address,
pass: config.email.password
}
});
const mailOptions = {
from: config.email.address,
to,
subject,
html
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
reject(error);
} else {
resolve(`Message sent: ${info.response}`);
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment