根据请求正文在 nodemailer 中发送多个附件
sending mutliple attachments in nodemailer depending on request body
到目前为止,这是我正在处理的代码
function sendMail(req,res){
var transporter = nodemailer.createTransport({
host:process.env.NODEMAILER_HOST,
port:process.env.NODEMAILER_PORT,
auth: {
user: process.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASS
}
})
mailOptions = {
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
bcc: req.body.bcc,
subject: req.body.subject,
text: req.body.text,
html: req.body.html,
attachments: [
{
filename: req.body.attachments[0].filename,
path: req.body.attachments[0].path,
contentType: req.body.attachments[0].contentType
},{
filename: req.body.attachments[1].filename,
path: req.body.attachments[1].path,
contentType: req.body.attachments[1].contentType
}
]
}
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
return res.status(400).json("Failed to Send");
}else{
res.status(201).json(req.body);
}
});
}
这是我的请求正文
{
"from": "example1@gmail.com",
"to": "example2@gmail.com",
"cc": "example3@gmail.com",
"bcc": "test@example.com",
"subject": "Test Request 1",
"text": "Example Text 1",
"html": "<p>Some Paragraph</p>",
"attachments": [
{
"filename": "test.doc",
"path": "dirname/test.doc",
"contentType": "application/doc"
},{
"filename": "test-coverted.pdf",
"path": "dirname/test-converted.pdf",
"contentType": "application/pdf"
}
]
}
如何在不在邮件选项中一一定义的情况下循环浏览附件?
因为如果我迭代 mailOptions,那么我需要创建单独的传输,这反过来会为不同的附件发送不同的电子邮件。
您在响应正文中获得了相同的 json 对象键,因此您可以直接将附件数组分配给 nodemailer mailOption 对象的附件键。参考下文。
function sendMail(req,res){
var transporter = nodemailer.createTransport({
host:process.env.NODEMAILER_HOST,
port:process.env.NODEMAILER_PORT,
auth: {
user: process.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASS
}
})
mailOptions = {
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
bcc: req.body.bcc,
subject: req.body.subject,
text: req.body.text,
html: req.body.html,
attachments: req.body.attachments
}
到目前为止,这是我正在处理的代码
function sendMail(req,res){
var transporter = nodemailer.createTransport({
host:process.env.NODEMAILER_HOST,
port:process.env.NODEMAILER_PORT,
auth: {
user: process.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASS
}
})
mailOptions = {
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
bcc: req.body.bcc,
subject: req.body.subject,
text: req.body.text,
html: req.body.html,
attachments: [
{
filename: req.body.attachments[0].filename,
path: req.body.attachments[0].path,
contentType: req.body.attachments[0].contentType
},{
filename: req.body.attachments[1].filename,
path: req.body.attachments[1].path,
contentType: req.body.attachments[1].contentType
}
]
}
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
return res.status(400).json("Failed to Send");
}else{
res.status(201).json(req.body);
}
});
}
这是我的请求正文
{
"from": "example1@gmail.com",
"to": "example2@gmail.com",
"cc": "example3@gmail.com",
"bcc": "test@example.com",
"subject": "Test Request 1",
"text": "Example Text 1",
"html": "<p>Some Paragraph</p>",
"attachments": [
{
"filename": "test.doc",
"path": "dirname/test.doc",
"contentType": "application/doc"
},{
"filename": "test-coverted.pdf",
"path": "dirname/test-converted.pdf",
"contentType": "application/pdf"
}
]
}
如何在不在邮件选项中一一定义的情况下循环浏览附件?
因为如果我迭代 mailOptions,那么我需要创建单独的传输,这反过来会为不同的附件发送不同的电子邮件。
您在响应正文中获得了相同的 json 对象键,因此您可以直接将附件数组分配给 nodemailer mailOption 对象的附件键。参考下文。
function sendMail(req,res){
var transporter = nodemailer.createTransport({
host:process.env.NODEMAILER_HOST,
port:process.env.NODEMAILER_PORT,
auth: {
user: process.env.NODEMAILER_ADDRESS,
pass: process.env.NODEMAILER_PASS
}
})
mailOptions = {
from: req.body.from,
to: req.body.to,
cc: req.body.cc,
bcc: req.body.bcc,
subject: req.body.subject,
text: req.body.text,
html: req.body.html,
attachments: req.body.attachments
}