【发布时间】:2021-06-13 14:37:14
【问题描述】:
如何使用 Javascript 中的 GMAIL API 发送带有附件 PDF 文件(超过 10 MB)的电子邮件。我尝试使用一些 PDF 文件附件代码并发送电子邮件,如下所示。
const base64Data = 'Base64 PDF File string here (10 MB)'
const mixedB = 'mixedB';
const relatedB = 'relatedB';
const alternativeB = 'alternativeB';
const To = 'test@gmail.com'
const message = '<p>This is TEST EMAIL with PDF Attachment</p>'
const messageParts = [
'From: ' + 'vijay@test.com',
'To: ' + To,
`Subject: This is Test Email`,
'MIME-Version: 1.0',
'Content-Type: multipart/mixed; boundary="' + mixedB + '"',
'',
'--' + mixedB,
'Content-Type: multipart/related; boundary="' + relatedB + '"',
'',
'--' + relatedB,
'Content-Type: multipart/alternative; boundary="' + alternativeB + '"',
'',
'--' + alternativeB,
'Content-Type: text/html; charset=utf-8',
'',
message, // html content.
'',
'--' + alternativeB + '--',
'',
'--' + relatedB + '--',
'',
'--' + mixedB,
'Content-Type: application/pdf;name="attachedFile.pdf"',
'Content-Transfer-Encoding: base64',
'Content-Disposition: attachment;filename="attachedFile.pdf"',
'',
base64Data, // base64 data of the file.
'',
'--' + mixedB + '--'
]
let aaa = messageParts.join('\r\n')
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'uploadType': 'multipart',
'resource': {
'raw': window.btoa(aaa).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
});
return sendRequest.execute();
【问题讨论】:
-
代码是否适用于不同的、更小的 base64 字符串?您确定 base64 字符串是正确的,并且没有一个字节丢失或损坏吗?
-
是的,适用于较小的文件(PDF 和 PNG)
-
base64.guru/converter/encode/pdf,我正在使用此站点从 PDF 进行 Base64 转换
标签: javascript gmail gmail-api