我试图弄清楚如何使用 Mailchimp 在交易电子邮件中发送附件。根据文档,attachments
数组必须包含具有type
,name
和content
属性的对象。我想不通的是content
。令人惊讶的是,我可以找到关于 SO 的相关问题。
文档说它的值必须是:
附件内容为 base64 编码字符串
所以我有这个发送电子邮件的功能,但附件内容已损坏(名称和类型看起来不错):
const sendEmail = emailObj => {
console.log('sendEmail()');
const URL = 'https://mandrillapp.com/api/1.0/messages/send';
const { html, subject, toEmail, attachmentId } = emailObj;
const file = DriveApp.getFileById(attachmentId);
const type = file.getMimeType();
const name = file.getName();
const content = Utilities.base64Encode(file.getBlob().getDataAsString());
const options = {
header: {
'Content-Type': 'application/json',
},
payload: JSON.stringify({
key: 'key',
message: {
from_email: 'email@domain.com',
subject,
html,
to: [
{
email: toEmail,
type: 'to',
},
],
attachments: [
{
type,
name,
content,
},
],
},
}),
};
const response = UrlFetchApp.fetch(URL, options);
console.log(response.getContentText());
return emailObj;
};
附件是带有正确名称的损坏的 PDF 文件。
我也尝试将内容设置为:
file.getBlob()
file.getBlob().getDataAsString()
file.getBlob().getBytes()
希望有人以前做过这个:)