I am trying to figure out how to send attachments in transactional emails with Mailchimp. According to the documentation, the attachments
array must contain objects with type
, name
and content
properties. The one that I can't figure out is content
. And surprisingly I could find a related question on SO.
The documentation says its value must be:
the content of the attachment as a base64-encoded string
So I have this function that does send the email, but the attachment content is corrupt (the name and type look fine):
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;
};
The attachment comes in as a corrupt PDF file with the right name.
I have also tried setting the content to:
file.getBlob()
file.getBlob().getDataAsString()
file.getBlob().getBytes()
Hopefully someone has done this before :)