发送附件 nodemailer 而不在服务器中存储文件
send attachement nodemailer wihout storing file in server
我正在寻找一种使用 nodemailer、multer、express 发送附件而无需将文件存储在服务器中的方法。
这是我实际的 sendEmai 功能,我想更新它以便我可以发送附件
exports.sendMail = (req, res) => {
let credentials=await Integration_mail.findOne({
where: {
userId: req.body.userId,
},
})
//decrypt password
var bytes = CryptoJS.AES.decrypt(
credentials.password,
secretconfig.secret
);
var originalPwd = bytes.toString(CryptoJS.enc.Utf8);
let transporter = nodemailer.createTransport({
host: credentials.host_smtp,
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: credentials.user,
pass: originalPwd,
},
});
const { destMail, subject, text } = req.body;
var mailOptions = {
from: credentials.user,
to: destMail,
subject: subject,
text: text,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return res.status(500).json({ msg: error });
} else {
return res.status(200).json({ msg: 'email sent' });
}
});
})
});
};
感谢您的帮助
所以经过一段时间的研究,我使用表单数据上传文件,只使用我通过 nodemailer 发送的文件的缓冲区,而不必将它上传到服务器并使用它的路径。
这是代码:
let transporter = nodemailer.createTransport({
host: host_smtp,
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP
tls: {
ciphers: "SSLv3",
},
auth: {
user: credentials.email,
pass: credentials.password,
},
});
const { to, subject, text, cc, bcc } = req.body;
let attachments = [];
if (req.files) {
let files = req.files.file;
//if its an array of files I preferred to not allow uploads > 20Mb
if (Array.isArray(files)) {
size = files.reduce((acc, file) => acc + file.size, 0);
if (size > 20971520)
return res.status(500).json({ message: "files size cannot be greater than 20MB" });
attachments = files.map((file) => ({
filename: file.name,
content: file.data,
encoding: file.encoding,
}));
} else {
if (files.size > 20971520)
return res.status(500).json({ message: "files size cannot be greater than 20MB" });
attachments = [
{
filename: files.name,
content: files.data,
encoding: files.encoding,
},
];
}
}
const mailOptions = {
from: credentials.email,
to: to,
cc: cc,
bcc: bcc,
subject: subject,
html: text,
attachments: attachments,
};
await transporter.sendMail(mailOptions);
这是使用表单数据的请求正文的屏幕截图
这里是对官方文档的引用:
https://nodemailer.com/message/attachments/
我正在寻找一种使用 nodemailer、multer、express 发送附件而无需将文件存储在服务器中的方法。
这是我实际的 sendEmai 功能,我想更新它以便我可以发送附件
exports.sendMail = (req, res) => {
let credentials=await Integration_mail.findOne({
where: {
userId: req.body.userId,
},
})
//decrypt password
var bytes = CryptoJS.AES.decrypt(
credentials.password,
secretconfig.secret
);
var originalPwd = bytes.toString(CryptoJS.enc.Utf8);
let transporter = nodemailer.createTransport({
host: credentials.host_smtp,
port: 587,
secure: false, // upgrade later with STARTTLS
auth: {
user: credentials.user,
pass: originalPwd,
},
});
const { destMail, subject, text } = req.body;
var mailOptions = {
from: credentials.user,
to: destMail,
subject: subject,
text: text,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return res.status(500).json({ msg: error });
} else {
return res.status(200).json({ msg: 'email sent' });
}
});
})
});
};
感谢您的帮助
所以经过一段时间的研究,我使用表单数据上传文件,只使用我通过 nodemailer 发送的文件的缓冲区,而不必将它上传到服务器并使用它的路径。 这是代码:
let transporter = nodemailer.createTransport({
host: host_smtp,
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP
tls: {
ciphers: "SSLv3",
},
auth: {
user: credentials.email,
pass: credentials.password,
},
});
const { to, subject, text, cc, bcc } = req.body;
let attachments = [];
if (req.files) {
let files = req.files.file;
//if its an array of files I preferred to not allow uploads > 20Mb
if (Array.isArray(files)) {
size = files.reduce((acc, file) => acc + file.size, 0);
if (size > 20971520)
return res.status(500).json({ message: "files size cannot be greater than 20MB" });
attachments = files.map((file) => ({
filename: file.name,
content: file.data,
encoding: file.encoding,
}));
} else {
if (files.size > 20971520)
return res.status(500).json({ message: "files size cannot be greater than 20MB" });
attachments = [
{
filename: files.name,
content: files.data,
encoding: files.encoding,
},
];
}
}
const mailOptions = {
from: credentials.email,
to: to,
cc: cc,
bcc: bcc,
subject: subject,
html: text,
attachments: attachments,
};
await transporter.sendMail(mailOptions);
这是使用表单数据的请求正文的屏幕截图
这里是对官方文档的引用: https://nodemailer.com/message/attachments/