I want to use Google Application Script to send email via mailgun with multiple
attachments.
var params = {
"from": email.fromName+" <"+email.fromEmail+">",
"to": email.toEmail,
"subject": email.subject,
"replyto": email.fromEmail,
"text": email.message,
"html": email.htmlMessage,
};
this works perfectly for one attachment.
params.attachment = DriveApp.getFileById("Google drive file ID").getBlob()
The official mailgun documentation states that "You can post multiple attachment values. Important: You must use multipart/form-data encoding when sending attachments."
How to do that using cURL is
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <YOU@YOUR_DOMAIN_NAME>' \
-F to='foo@example.com' \
-F cc='bar@example.com' \
-F bcc='baz@example.com' \
-F subject='Hello' \
-F text='Testing some Mailgun awesomness!' \
--form-string html='<html>HTML version of the body</html>' \
-F attachment=@files/cartman.jpg \
-F attachment=@files/cartman.png
I cannot add two keys attachment
to object params that is used in UrlFetchApp. Below is code that I tried to make multiple attachemnet work. I got inspired here with encodeURIComponent()
email.attachments = {}
email.attachments.file1 = "1Xq63awYnOfSeL7fStkn0jWsou"
email.attachments.file2 = "1Iy96o9Tw9tvMw7VgZMzCA9mqm"
if ("file1" in email.attachments){
var attachment = []
for (const fileTMP in email.attachments){
const fileId = email.attachments[fileTMP]
const file = DriveApp.getFileById(fileId);
var attachmentBlob = file.getBlob()
attachment.push(attachmentBlob)
// attachment.push(encodeURIComponent(attachmentBlob))
}
// params.attachment = attachment.join(",") // doesnt work, even for 1 file
// params.attachment = attachment // doesnt work, even for 1 file
params.attachment = attachment.join("&") // doesnt work, even for 1 file, even with encodeURIComponent()
}
Could some one help me to send multiple attachments from mailgun via Google Application Script. Can I use custom name for the attachment?
UPDATE
Reply to my enquiry from mailgun support
We don't have direct development support or are able to review
or debug any code in the level of capacity you are seeking so we
would not be able to help here. I suggest you look for online
resources and look for community-made code libraries that might
make what you are trying to accomplish easier, especially since
the langue you are using is one we are not familiar with or in
our documentation.