sdepold sdepold - 1 year ago 71
Node.js Question

Sending mails with attachment via NodeJS

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

With best regards,

Answer Source

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

var mailer = require('nodemailer');
mailer.SMTP = {
    host: '', 
    use_authentication: true, 
    user: '', 
    pass: 'xxxxxx'

Then read a file and send an email :

fs.readFile("./attachment.txt", function (err, data) {

        sender: '',
        to: '',
        subject: 'Attachment!',
        body: 'mail content...',
        attachments: [{'filename': 'attachment.txt', 'content': data}]
    }), function(err, success) {
        if (err) {
            // Handle error