【发布时间】:2019-03-31 13:38:06
【问题描述】:
我正在使用“nodemailer”模块将 Excel 文件作为电子邮件的附件发送。
注意:我在附件中传递给“内容”的是一个对象数组。
function sendEmailWithAttachments(recipientEmailId, subject, content, next) {
var ormMailerInfo = getORMMailerInfo();
var transporter = nodemailer.createTransport(smtpTransport({
host: ormMailerInfo.orm_mailer_host,
secure: ormMailerInfo.orm_mailer_secure,
port: ormMailerInfo.orm_mailer_port,
auth: {
user: ormMailerInfo.orm_mailer_user,
pass: ormMailerInfo.orm_mailer_pass
}
}));
transporter.sendMail({
from: ormMailerInfo.orm_mailer_user,
to: recipientEmailId,
subject: subject,
attachments: [
{ /* the uniqueness of my question begins from here */
// file being sent is Excel file as '.xlsx' indicates
filename: subject + '.xlsx',
// content/data being sent an array of objects
content: new Buffer(content,'utf-8')
}
]
}, next);
}
我已经成功发送和接收它,但是打开excel文件时,它显示以下错误:
“Excel 无法打开文件 'filename.xlsx',因为文件扩展名的文件格式无效。验证文件没有损坏并且文件扩展名与文件格式匹配。”
有什么帮助吗,亲爱的会员?
【问题讨论】:
-
您使用的是哪个操作系统?试试
.csv扩展 -
如何生成 XLSX 内容?创建 CSV 文件并不是那么简单。也许您需要使用一些库,例如npmjs.com/package/xlsx
-
@Suresh Prajapati,我的操作系统是 Windows 10。我尝试使用 .csv 扩展名并且 excel 文件打开良好,但文件中的数据只是 [object Object] [object Object]
-
@Suresh Prajapati,对不起,这个 [object Object] [object Object] 是由于我所做的这个改变: content: content.toString() 但是当我使用这个时: content: new Buffer(content ,'utf-8'),该文件也可以很好地打开,但根本没有数据,这意味着它只是一个空文件!
标签: node.js nodemailer