Is there any library for NodeJS for sending mails with attachment?
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.
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 } } });
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)); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With