Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending mails with attachment via NodeJS

Is there any library for NodeJS for sending mails with attachment?

like image 947
sdepold Avatar asked Jan 12 '11 19:01

sdepold


People also ask

Can you send emails with node js?

Whether you're building the next Gmail, cutting-edge email marketing software, or just want to program notifications into your Node. js app, it's easy to send emails using readily-made tools and services. Node. js is one of the most popular (if not the most popular) server-side runtime environment for web applications.


2 Answers

Yes, it is pretty simple, I use nodemailer: npm install nodemailer --save

var mailer = require('nodemailer'); mailer.SMTP = {     host: 'host.com',      port:587,     use_authentication: true,      user: '[email protected]',      pass: 'xxxxxx' }; 

Then read a file and send an email :

fs.readFile("./attachment.txt", function (err, data) {      mailer.send_mail({                sender: '[email protected]',         to: '[email protected]',         subject: 'Attachment!',         body: 'mail content...',         attachments: [{'filename': 'attachment.txt', 'content': data}]     }), function(err, success) {         if (err) {             // Handle error         }      } }); 
like image 172
Philippe Avatar answered Sep 20 '22 04:09

Philippe


Try with nodemailer, for example try this:

  var nodemailer = require('nodemailer');   nodemailer.SMTP = {      host: 'mail.yourmail.com',      port: 25,      use_authentication: true,      user: '[email protected]',      pass: 'somepasswd'    };    var message = {            sender: "[email protected]",             to:'[email protected]',            subject: '',             html: '<h1>test</h1>',           attachments: [           {                filename: "somepicture.jpg",                 contents: new Buffer(data, 'base64'),                cid: cid             }            ]        }; 

finally, send the message

    nodemailer.send_mail(message,          function(err) {            if (!err) {              console.log('Email send ...');         } else console.log(sys.inspect(err));            }); 
like image 40
omolina Avatar answered Sep 22 '22 04:09

omolina