Skip to content

Instantly share code, notes, and snippets.

@voz
Last active March 15, 2016 11:23
Show Gist options
  • Save voz/5d6fa8ac6288cc084d1c to your computer and use it in GitHub Desktop.
Save voz/5d6fa8ac6288cc084d1c to your computer and use it in GitHub Desktop.
A script to send (survey) emails with Amazon SES
/**
* This script allows sending emails with Amason SES (you should already have an account).
*
* It can be useful, for instance, when you want to distribute a survey done with Google Forms,
* but Google keeps on telling you:
* "You've temporarily exceeded your email quota. Please wait for a while and try sending again."
*
* Before running the script, install nodemailer and lodash:
* `$ npm install nodemailer`
* `$ npm install lodash`
*
* To use the script, just configure the fields below and run `node email-with-ses.js`
*
* Feel free to suggest improvements.
*
* Licensed under the MIT license.
*/
// ------ Mailer Transport Config ----- //
var sesMailConfig = {
service: 'SES',
auth: {
user: 'ABCDEFGABCDEFGABCDEF', // TODO: put your own
pass: 'Bypgp+Bypgp+Bypgp+Bypgp+Bypgp+Bdpgp+Bypgp+BB' // TODO: put your own
}
}
// to avoid blockage, we send emails in batches and wait some time inbetween
var maxEmailsInBatch = 14; // my AWS SES limit 14 emails/second
var timeToWait = 2000; // wait 2 seconds just to be sure
// ------ Email Field Values ----- //
var fromEmail = 'Graasp <[email protected]>'; // TODO: put your own
var recipientEmails = [
'[email protected]',
'[email protected]',
'[email protected]'
];
// TODO: put your own email body
// Use this tool to get html from text: http://www.textfixer.com/html/convert-text-html.php
var htmlBody = '<p>Hello,</p> <p>We are developers of Graasp from EPFL and are constantly looking for ways to improve your experience using the Graasp platform. For this reason, we have designed a survey and would like to get your __honest__ answers: https://goo.gl/5SufuJ</p> <p>The survey consists of three small parts:<br> 1) the System Usability Scale survey, which is a standard way of evaluating usability (ease of use) of information systems<br> 2) two question, where we would like to hear your suggestions about what should be improved in the platform<br> 3) some information about you so that we have a better idea of who is responding</p> <p>This survey is completely anonymous unless you explicitly allow us to contact you by specifying your email at the end of the survey.</p> <p>The survey should take under 8 minutes to complete. Your contribution will really help us to improve the platform.</p> <p>If you have any additional questions related to this survey feel free to contact [email protected]</p> <p>To continue with the survey, please follow the link https://goo.gl/5SufuJ</p> <p>Best regards,<br> Graasp team</p>';
var emailObj = {
from: fromEmail,
replyTo: fromEmail,
bcc: '[email protected]', // TODO: put your own email if you want to be BCCed
subject: 'Help us to improve your Graasp experience', // TODO: put your own email subject
html: htmlBody
}
// ------ Main mail sending script ----- //
var nodemailer = require('nodemailer');
var _ = require('lodash');
var sesTransport = nodemailer.createTransport(sesMailConfig);
console.log('Email service ' + sesMailConfig.service + ' was initialized');
console.log('Sending emails...');
var sendEmails = function (recipients) {
_.each(recipients, function (recipientEmail) {
emailObj.to = recipientEmail;
sesTransport.sendMail(emailObj, function (err, info) {
if (err) return console.error('An error has occured when sending to ' + recipientEmail + ' : ' + err);
console.log('Message was sent to ' + recipientEmail + ' without errors. Response: ' + info.response);
});
})
}
var sendBatchScheduleNext = function () {
var thisBatchRecipients = recipientEmails.splice(0, maxEmailsInBatch);
sendEmails(thisBatchRecipients);
console.log('---')
console.log('Sending ' + maxEmailsInBatch + ' emails. Left: ' + recipientEmails.length + ' emails to send');
// if some emails still left
if (recipientEmails.length > 0) {
setTimeout(function () {
// select 14 emails for this batch
sendBatchScheduleNext();
}, timeToWait);
}
}
// finally, start with the first batch
sendBatchScheduleNext();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment