问题
所有数据都以文本格式发送,而不是以附件的形式发送。
我的代码
var boundary = "__myapp__", nl = "\n";
// var attach = data.toString("base64");
var fileName = "abc.pdf";
var attach = new Buffer(fs.readFileSync( "./"+fileName)) .toString("base64");
var str = ["Content-Type: text/plain; charset=\"UTF-8\"\n",
"MIME-Version: 1.0\n",
"Content-Transfer-Encoding: 7bit\n",
"to: ", to, "\n",
"subject: ", subject, "\n\n",
"Content-Type: multipart/alternate; boundary=" + boundary + nl,
"--" + boundary,
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: 7bit" + nl,
message+ nl,
"--" + boundary,
"--" + boundary,
"Content-Type: Application/pdf; name=myPdf.pdf",
'Content-Disposition: attachment; filename=myPdf.pdf',
"Content-Transfer-Encoding: base64" + nl,
attach,
"--" + boundary + "--"
].join('');
var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-')
.replace(/\//g, '_');
resolve(encodedMail);
邮件的一部分如下
内容类型:multipart / alternate;边界= MyApp的 --_ myapp_Content-Type:text / plain; charset = UTF-8Content-Transfer-Encoding:7位测试消息 - myapp --_ myapp_Content-Type:Application / pdf; name = myPdf.pdfContent-Disposition:attachment; filename = myPdf.pdfContent-Transfer-Encoding:base64 JVBERi0xLjMgCiXi48 / TIAoxIDAgb2JqIAo8PCAKL1R5cGUgL0NhdGFsb2cgCi9QYWdlcyAyIDAgUiAKL1BhZ2VNb2RlIC9Vc2VOb25lIAovVmlld2VyUHJlZmVyZW5jZXMgPDwgCi9GaXRXaW5kb3cgdHJ1ZSAKL1BhZ2VMYXlvdXQgL1NpbmdsZVBhZ2UgCi9Ob25GdWxsU2NyZWVuUGFnZU1vZGUgL1VzZU5vbmUgCj4 +佑+ PiAKZW5kb2JqIAo1IDAgb2JqIAo8PCAKL0xlbmd0aCAyNjQzIAovRmlsdGVyIFsgL0ZsYXRlRGVjb2RlIF0gCj4 + IApzdHJlYW0KeJzdGWtT48jxu3 + FMYMkY8uep2bGkgADBrwseHntLYv3kspe9iqpbFK5fMjfT89LkuUH3H5J1UEVNZru6e7pdw94hEUXw2 + K3UpxMpLdr9872G7 / 9qtf3F92JMVdynGXYJ7xLsmY4N3f / tr5qfPPDu2 + A7Z / WhSSZRUO1YQYnIcOHilFu82 /// lq0RlhxKMLjA3yX + X + pqp91tyXZPO + wiLsU9HYJzzDFQPpAUIL6gT97tZWin + AnFKxzH199 + ssQIywTBkAIURx92EgVBEWQG4dznCc ...更多 文本......最后...... == - myapp -
我认为使用附件发送邮件的模板可能存在一些问题。
任何人都可以在这里提供帮助。感谢
答案 0 :(得分:1)
我能够通过使用适当的消息结构来附加PDF文件。设置正确的mimeType和边界非常重要。
@Tanaike指出的参考(https://stackoverflow.com/a/50540373/13664358)很有帮助!
这里是发送带有PDF附件的电子邮件的全部功能:
const subject = 'My Subject';
const utf8Subject = `=?utf-8?B?${Buffer.from(subject).toString('base64')}?=`;
var fileName = "mypdf.pdf";
var attach = new Buffer.from(fs.readFileSync("./" + fileName)).toString("base64");
var emailBody = 'My Email Message';
var messageParts = [
"MIME-Version: 1.0",
'From: Teste <teste@teste.com>',
'To: Teste <teste@teste.com>',
`Subject: ${utf8Subject}`,
"Content-Type: multipart/mixed; boundary=012boundary01",
'',
"--012boundary01",
"Content-Type: multipart/alternative; boundary=012boundary02",
'',
"--012boundary02",
"Content-type: text/html; charset=UTF-8",
"Content-Transfer-Encoding: quoted-printable",
'',
emailBody,
'',
"--012boundary02--",
"--012boundary01",
"Content-Type: Application/pdf; name=mypdf.pdf",
'Content-Disposition: attachment; filename=mypdf.pdf',
"Content-Transfer-Encoding: base64",
'',
attach,
"--012boundary01--",
]
const message = messageParts.join('\n');
// The body needs to be base64url encoded.
const encodedMessage = Buffer.from(message)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
const gmail = google.gmail({version: 'v1', auth});
gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: encodedMessage
}
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
});
注意:上面的代码将Google api用于nodejs(https://www.npmjs.com/package/googleapis)