Nodejs 通过电子邮件发送流
Nodejs send stream via email
我正在尝试使用 Nodemailer but for some reason, the attachment is coming up as 0 kb
when I download it and look at its info. How can I properly send the stream & it's data as an attachment? The stream should contain a PKPass
is it a better option to send the response as an attachment? I'm using 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: 'email4@gmail.com',
pass: 'password',
},
});
let mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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: 'email4@gmail.com',
pass: 'password',
},
});
const mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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: 'email4@gmail.com',
pass: 'password',
},
});
let mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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:///home/racal/JavaScript/Node/Web-Env/webpack.config.dev.js:10
path.resolve(__dirname, 'src/index')
^
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)
`
我正在尝试使用 Nodemailer but for some reason, the attachment is coming up as 0 kb
when I download it and look at its info. How can I properly send the stream & it's data as an attachment? The stream should contain a PKPass
is it a better option to send the response as an attachment? I'm using 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: 'email4@gmail.com',
pass: 'password',
},
});
let mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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: 'email4@gmail.com',
pass: 'password',
},
});
const mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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: 'email4@gmail.com',
pass: 'password',
},
});
let mailOptions = {
from: 'email4@gmail.com',
to: 'emailTo1@gmail.com',
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:///home/racal/JavaScript/Node/Web-Env/webpack.config.dev.js:10 path.resolve(__dirname, 'src/index') ^
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)
`