我试图通过电子邮件发送我的流数据,使用的是 Nodemailer 但不知为何,该附件显示为 0 kb
当我下载它并查看它的信息时,我如何正确地发送流& 它的数据作为附件?我如何才能正确地将流& 它的数据作为附件发送?数据流应该包含一个 PKPass
以附件形式发送响应是否是一个更好的选择?我正在使用 passkit-generator 以产生 PKPass
const stream = examplePass.generate();
res.set({
'Content-Type': 'application/vnd.apple.pkpass',
'Content-disposition': `attachment; filename=${passName}.pkpass`,
});
stream.pipe(res);
//Send the receipt email
stream.on('finish', (attach) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: '[email protected]',
pass: 'password',
},
});
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'You\'re on your way to ',
html: '<h1>Reciept email</h1>',
attachments: [
{
filename: 'Event.pkpass',
contentType: 'application/vnd.apple.pkpass',
content: stream,
},
],
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('success ' + info);
});
});
似乎在这种情况下,发送文件的唯一方法是读取整个流,并将其作为一个字符串或缓冲区发送。
const stream = examplePass.generate();
res.set({
'Content-Type': 'application/vnd.apple.pkpass',
'Content-disposition': `attachment; filename=${passName}.pkpass`,
});
stream.pipe(res);
const chunks = [];
stream.on('data', chunk => {
chunks.push(chunk);
});
stream.on('end', () => {
const content = Buffer.concat(chunks);
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: '[email protected]',
pass: 'password',
},
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'You\'re on your way to ',
html: '<h1>Reciept email</h1>',
attachments: [
{
filename: 'Event.pkpass',
contentType: 'application/vnd.apple.pkpass',
content
},
],
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('success:', info);
});
});
但是,如果文件很大的话,你需要小心使用这种方法,因为它是完全加载到RAM中的。
你的问题是,你把流传给了 res
是一个 "StreamReader",也就是消费者,而你只需要把它传递给nodeMailer,它本身就是一个StreamReader。
const stream = examplePass.generate();
// res.set({
// 'Content-Type': 'application/vnd.apple.pkpass',
// 'Content-disposition': `attachment; filename=${passName}.pkpass`,
// });
// stream.pipe(res); <- remove this
//Send the receipt email
stream.on('finish', (attach) => {
let transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false,
requireTLS: true,
auth: {
user: '[email protected]',
pass: 'password',
},
});
let mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: "You're on your way to ",
html: '<h1>Reciept email</h1>',
attachments: [
{
filename: 'Event.pkpass',
contentType: 'application/vnd.apple.pkpass',
content: stream,
},
],
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error.message);
}
console.log('success ' + info);
});
});
这就是我的邮件附件流。
你好,我有这样的问题`file:/homeracalJavaScriptNodeWeb-Envwebpack.config.dev.js:10path.resolve(__dirname, 'srcindex')^。
ReferenceError: __dirname is not defined
at file:///home/racal/JavaScript/Node/Web-Env/webpack.config.dev.js:10:18
at ModuleJob.run (internal/modules/esm/module_job.js:138:23)
at async Loader.import (internal/modules/esm/loader.js:178:24)
`