是否有用于NodeJS的库,用于发送带有附件的邮件?
是的,这非常简单,我使用nodemailer: npm install nodemailer --save
npm install nodemailer --save
var mailer = require('nodemailer'); mailer.SMTP = { host: 'host.com', port:587, use_authentication: true, user: 'you@example.com', pass: 'xxxxxx' };
然后阅读文件并发送电子邮件:
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 } } });