Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created December 10, 2016 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PandaWhoCodes/531b5b5e1af72eec79819fd218236d93 to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/531b5b5e1af72eec79819fd218236d93 to your computer and use it in GitHub Desktop.
Sending weekly emails using nodemailer
'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" <ashish@myemail.com>',
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