How to attach file to an email with nodemailer

前端 未结 8 2121
南笙
南笙 2020-12-04 19:49

I have code that send email with nodemailer in nodejs but I want to attach file to an email but I can\'t find way to do that I search on ne

8条回答
  •  庸人自扰
    2020-12-04 20:24

    var mailer = require('nodemailer');
    mailer.SMTP = {
        host: 'host.com', 
        port:587,
        use_authentication: true, 
        user: 'you@example.com', 
        pass: 'xxxxxx'
    };
    
    Then read a file and send an email :
    
    fs.readFile("./attachment.txt", function (err, data) {
    
        mailer.send_mail({       
            sender: 'sender@sender.com',
            to: 'dest@dest.com',
            subject: 'Attachment!',
            body: 'mail content...',
            attachments: [{'filename': 'attachment.txt', 'content': data}]
        }), function(err, success) {
            if (err) {
                // Handle error
            }
    
        }
    });
    

提交回复
热议问题