具有多个附件的 gmail 无法正常工作
gmail with multiple attachments not working
当使用 gmail api 发送带有多个附件的邮件时,邮件发送不正常。附件不在邮件中,而是一些代码在邮件中。我正在使用节点 js 和请求承诺模块来实现这一点。
我试过通过设置附件边界来发送附件。这是我写的一段代码。
let user = await db.model('User').findOne({ _id: userId });
let from = user.crmOptions.email;
let mailString = '';
for (let i in req.files) {
mailString += '--boundary_mail1\n' + ','
mailString += `Content-Type: ${req.files[i].mimetype}\n` + ','
mailString += `Content-Disposition: attachment; filename="${req.files[i].filename}"\n` + ','
mailString += "Content-Transfer-Encoding: base64\n\n" + ','
mailString += `${fs.readFileSync(req.files[i].path).toString('base64')}` + ',' + '\n'
if (i !== req.files.length - 1)
mailString += ','
}
let raw = [
'MIME-Version: 1.0\n',
"to: ", req.body.to, "\n",
"from: ", from, "\n",
"cc: ", req.body.cc ? req.body.cc : '', "\n",
"bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
"subject: ", req.body.subject ? req.body.subject : '', "\n",
"Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",
"--boundary_mail1\n",
"Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",
"--boundary_mail2\n",
"Content-Type: text/html; charset=UTF-8\n",
"Content-Transfer-Encoding: quoted-printable\n\n",
req.body.message = req.body.message ? req.body.message : '', "\n\n",
"--boundary_mail2--\n",
mailString,
'--boundary_mail1--',
].join('');
const id = 'me';
let options = {
url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
method: 'POST',
headers: {
'Authorization': `Bearer ${user.crmOptions.access_token}`,
'Content-Type': 'message/rfc822'
},
body: raw
};
await request(options).then(async body => {
console.log("Body: ", body);
}).catch(err => {
console.log("Error: ", err);
})
我希望发送带有多个附件的电子邮件。但是我也无法发送带有单个附件的邮件。谁能帮我解决这个问题。
我发现了问题家伙。刚刚删除不需要的逗号及其工作。这是修改后的代码。
let user = await db.model('User').findOne({ _id: userId });
let from = user.crmOptions.email;
let mailString = '';
for (let i in req.files) {
mailString += '--boundary_mail1\n'
mailString += `Content-Type: ${req.files[i].mimetype}\n`
mailString += `Content-Disposition: attachment; filename="${req.files[i].filename}"\n`
mailString += "Content-Transfer-Encoding: base64\n\n"
mailString += `${fs.readFileSync(req.files[i].path).toString('base64')}` + '\n'
}
let raw = [
'MIME-Version: 1.0\n',
"to: ", req.body.to, "\n",
"from: ", from, "\n",
"cc: ", req.body.cc ? req.body.cc : '', "\n",
"bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
"subject: ", req.body.subject ? req.body.subject : '', "\n",
"Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",
"--boundary_mail1\n",
"Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",
"--boundary_mail2\n",
"Content-Type: text/html; charset=UTF-8\n",
"Content-Transfer-Encoding: quoted-printable\n\n",
req.body.message = req.body.message ? req.body.message : '', "\n\n",
"--boundary_mail2--\n",
mailString,
'--boundary_mail1--',
].join('');
const id = 'me';
let options = {
url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
method: 'POST',
headers: {
'Authorization': `Bearer ${user.crmOptions.access_token}`,
'Content-Type': 'message/rfc822'
},
body: raw
};
await request(options).then(async body => {
console.log("Body: ", body);
}).catch(err => {
console.log("Error: ", err);
})
当使用 gmail api 发送带有多个附件的邮件时,邮件发送不正常。附件不在邮件中,而是一些代码在邮件中。我正在使用节点 js 和请求承诺模块来实现这一点。
我试过通过设置附件边界来发送附件。这是我写的一段代码。
let user = await db.model('User').findOne({ _id: userId });
let from = user.crmOptions.email;
let mailString = '';
for (let i in req.files) {
mailString += '--boundary_mail1\n' + ','
mailString += `Content-Type: ${req.files[i].mimetype}\n` + ','
mailString += `Content-Disposition: attachment; filename="${req.files[i].filename}"\n` + ','
mailString += "Content-Transfer-Encoding: base64\n\n" + ','
mailString += `${fs.readFileSync(req.files[i].path).toString('base64')}` + ',' + '\n'
if (i !== req.files.length - 1)
mailString += ','
}
let raw = [
'MIME-Version: 1.0\n',
"to: ", req.body.to, "\n",
"from: ", from, "\n",
"cc: ", req.body.cc ? req.body.cc : '', "\n",
"bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
"subject: ", req.body.subject ? req.body.subject : '', "\n",
"Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",
"--boundary_mail1\n",
"Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",
"--boundary_mail2\n",
"Content-Type: text/html; charset=UTF-8\n",
"Content-Transfer-Encoding: quoted-printable\n\n",
req.body.message = req.body.message ? req.body.message : '', "\n\n",
"--boundary_mail2--\n",
mailString,
'--boundary_mail1--',
].join('');
const id = 'me';
let options = {
url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
method: 'POST',
headers: {
'Authorization': `Bearer ${user.crmOptions.access_token}`,
'Content-Type': 'message/rfc822'
},
body: raw
};
await request(options).then(async body => {
console.log("Body: ", body);
}).catch(err => {
console.log("Error: ", err);
})
我希望发送带有多个附件的电子邮件。但是我也无法发送带有单个附件的邮件。谁能帮我解决这个问题。
我发现了问题家伙。刚刚删除不需要的逗号及其工作。这是修改后的代码。
let user = await db.model('User').findOne({ _id: userId });
let from = user.crmOptions.email;
let mailString = '';
for (let i in req.files) {
mailString += '--boundary_mail1\n'
mailString += `Content-Type: ${req.files[i].mimetype}\n`
mailString += `Content-Disposition: attachment; filename="${req.files[i].filename}"\n`
mailString += "Content-Transfer-Encoding: base64\n\n"
mailString += `${fs.readFileSync(req.files[i].path).toString('base64')}` + '\n'
}
let raw = [
'MIME-Version: 1.0\n',
"to: ", req.body.to, "\n",
"from: ", from, "\n",
"cc: ", req.body.cc ? req.body.cc : '', "\n",
"bcc: ", req.body.bcc ? req.body.bcc : '', "\n",
"subject: ", req.body.subject ? req.body.subject : '', "\n",
"Content-Type: multipart/mixed; boundary=boundary_mail1\n\n",
"--boundary_mail1\n",
"Content-Type: multipart/alternative; boundary=boundary_mail2\n\n",
"--boundary_mail2\n",
"Content-Type: text/html; charset=UTF-8\n",
"Content-Transfer-Encoding: quoted-printable\n\n",
req.body.message = req.body.message ? req.body.message : '', "\n\n",
"--boundary_mail2--\n",
mailString,
'--boundary_mail1--',
].join('');
const id = 'me';
let options = {
url: "https://www.googleapis.com/upload/gmail/v1/users/" + id + "/messages/send?uploadType=multipart",
method: 'POST',
headers: {
'Authorization': `Bearer ${user.crmOptions.access_token}`,
'Content-Type': 'message/rfc822'
},
body: raw
};
await request(options).then(async body => {
console.log("Body: ", body);
}).catch(err => {
console.log("Error: ", err);
})