Created
December 10, 2016 09:27
-
-
Save PandaWhoCodes/531b5b5e1af72eec79819fd218236d93 to your computer and use it in GitHub Desktop.
Sending weekly emails using nodemailer
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
'use strict'; | |
var nodemailer = require('nodemailer'); | |
var schedule = require('node-schedule'); | |
//configure nodemailer | |
// visit: https://nodemailer.com/ | |
// For other types of transports (Amazon SES, Sendgrid,mailchimp...) see https://nodemailer.com/2-0-0-beta/setup-transporter/ | |
// visit the following to get a more rudimentary usage of nodemailer | |
// https://gist.github.com/reach2ashish/4bce68c3e5950916320b5bbcca0cf6b8 | |
var mailTransport = nodemailer.createTransport('smtps://<user>%40gmail.com:<password>@smtp.gmail.com'); | |
// users is a json list of users | |
//email html has the html of the email | |
// you can use escape in node.js to generate your email html | |
function sendWeekly(users, emailHtml) { | |
Object.keys(users).forEach(function(uid) { | |
var user = users[uid]; | |
if (user.email) { | |
var mailOptions = { | |
from: '"Ashish Cherian" <[email protected]>', | |
to: user.email, | |
subject: 'This week\'s top posts!', | |
html: emailHtml | |
}; | |
mailTransport.sendMail(mailOptions).then(function() { | |
console.log('Weekly top posts email sent to: ' + user.email); | |
// Save the date at which we sent the weekly email. | |
}).catch(function(error) { | |
console.log('Failed to send weekly top posts email:', error); | |
}); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment