Skip to content

Instantly share code, notes, and snippets.

@andris9
Created October 14, 2015 15:59
Show Gist options
  • Save andris9/411857993e8935c77c48 to your computer and use it in GitHub Desktop.
Save andris9/411857993e8935c77c48 to your computer and use it in GitHub Desktop.
'use strict';
// npm install nodemailer nodemailer-smtp-pool
var nodemailer = require('nodemailer');
var smtpPool = require('nodemailer-smtp-pool');
// Create a SMTP transporter object
var transport = nodemailer.createTransport(smtpPool({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: 'username',
pass: 'password'
},
// use up to 5 parallel connections, default is 5
maxConnections: 5,
// do not send more than 10 messages per connection, default is 100
maxMessages: 10,
// no not send more than 5 messages in a second, default is no limit
rateLimit: 5
}));
console.log('SMTP Configured');
// Message object
var message = {
// sender info
from: 'Sender Name <[email protected]>',
// Comma separated list of recipients
to: '"Receiver Name" <[email protected]>',
// Subject of the message
subject: 'Nodemailer is unicode friendly ✔', //
headers: {
'X-Laziness-level': 1000
},
// plaintext body
text: 'Hello to myself!',
// HTML body
html: '<p><b>Hello</b> to myself <img src="cid:[email protected]"/></p>' +
'<p>Here\'s a nyan cat for you as an embedded attachment:<br/><img src="cid:[email protected]"/></p>',
// Apple Watch specific HTML body
watchHtml: '<b>Hello</b> to myself',
// An array of attachments
attachments: [
// String attachment
{
filename: 'notes.txt',
content: 'Some notes about this e-mail',
contentType: 'text/plain' // optional, would be detected from the filename
},
// Binary Buffer attachment
{
filename: 'image.png',
content: new Buffer('iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD/' +
'//+l2Z/dAAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4U' +
'g9C9zwz3gVLMDA/A6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC', 'base64'),
cid: '[email protected]' // should be as unique as possible
},
// File Stream attachment
{
filename: 'nyan cat ✔.gif',
path: __dirname + '/assets/nyan.gif',
cid: '[email protected]' // should be as unique as possible
}
]
};
console.log('Sending Mail');
transporter.sendMail(message, function(error, info) {
if (error) {
console.log('Error occurred');
console.log(error.message);
return;
}
console.log('Message sent successfully!');
console.log('Server responded with "%s"', info.response);
});
@anshulguleria
Copy link

So if I use sendMail few times in a loop do they all get pooled?

for(let i=0; i<5; i++) {
    let changedMessageObj = {}; // add some customizations to message obj like change to field
    transporter.sendMail(message, handler);
}

@devyeshtandon
Copy link

@anshulguleria, any luck figuring this out? I am facing a similar situation and need help in this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment