Skip to content

Instantly share code, notes, and snippets.

@zgotsch
Created November 3, 2015 03:10
Show Gist options
  • Save zgotsch/72a1a27d79ede4f8b7db to your computer and use it in GitHub Desktop.
Save zgotsch/72a1a27d79ede4f8b7db to your computer and use it in GitHub Desktop.
Fancy email with nodemailer
var nodemailer = require('nodemailer');
var fs = require('fs');
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'example@gmail.com',
pass: 'yourpasswordhere'
}
});
// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails
// setup e-mail data with unicode symbols
var mailOptions = {
from: 'Zach\'s Node ✔ <alice@gmail.com>', // sender address
to: 'bob@gmail.com', // list of receivers
subject: 'This is an important test email ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<div><p><b>Hello world &#x2603;</b></p><img src="cid:poliwag" /></div>', // html body
attachments: [
{
filename: 'poliwag.jpg',
cid: 'poliwag',
content: fs.createReadStream('./poliwag.jpg'),
}
]
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment