Last active
December 2, 2017 14:03
-
-
Save magician11/9e9c2d160b5b60740908ce2e3ea5ef80 to your computer and use it in GitHub Desktop.
How to send an email from Node.js using Gmail
This file contains hidden or 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
module.exports = { | |
email: { | |
address: '[email protected]', | |
password: 'clever-password' | |
} | |
}; |
This file contains hidden or 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
/* | |
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