How do I convert this mailgun multi attachment cURL request to UrlFetchApp?
Thanks to @Kos and his first comment I got inspiration from MailGun API Python Requests so I can post working code how to send multiple attachments using GAS and mailgun.
function sendEmail(email) { var params = { "from": email.fromName+" <"+email.fromEmail+">", "to": email.toEmail, "subject": email.subject, "replyto": email.fromEmail, "text": email.message, "html": email.htmlMessage, } if ("attachments" in email){ for(var i=0, len=email.attachments.length; i < len; i++){ const fileId = email.attachments[i] const file = DriveApp.getFileById(fileId); var attachmentBlob = file.getBlob() params["attachment["+i+"]"] = attachmentBlob } } if ( "bcc" in email){ params.bcc = email.bcc } var options = { 'method': 'post', 'payload': params, 'headers': { 'Authorization': 'Basic ' + Utilities.base64Encode("api:" + MAILGUN_KEY) } } var response = JSON.parse(UrlFetchApp.fetch(email.url, options))}
where the email data object could look like
var email ={} email.toEmail = "TOemail@email.cz" email.fromEmail = "fromEmail@email.cz" email.fromName = "From email name" email.subject = "From email name" email.message = textVersionOfEmail email.htmlMessage = hTMLVersionOfEmail email.attachments = [] email.attachments.push(file1ID) email.attachments.push(file2ID)
If you know how to be able to name files in the script please leave a note.