首页 > 解决方案 > 如何将此 mailgun 多附件 cURL 请求转换为 UrlFetchApp?

问题描述

我想使用 Google 应用程序脚本通过 mailgun 发送带有multiple附件的电子邮件。

  var params = {
    "from": email.fromName+" <"+email.fromEmail+">",
    "to": email.toEmail,
    "subject": email.subject,
    "replyto": email.fromEmail,    
    "text": email.message,
    "html": email.htmlMessage,
  };

这适用于一个附件。

params.attachment = DriveApp.getFileById("Google drive file ID").getBlob()

官方mailgun 文档声明“您可以发布多个附件值。重要提示:发送附件时必须使用 multipart/form-data 编码。”

如何使用 cURL 做到这一点是

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

我无法attachment向 UrlFetchApp 中使用的对象参数添加两个键。下面是我试图使多个附件工作的代码。我在这里受到了 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()
 }

有人可以帮助我通过 Google 应用程序脚本从 mailgun 发送多个附件。我可以为附件使用自定义名称吗?

更新

回复我的询问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.

标签: google-apps-scriptcurlhttp-postmailgun

解决方案


感谢@Kos 和他的第一条评论,我从MailGun API Python Requests中获得了灵感,因此我可以发布如何使用 GAS 和 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))
}

电子邮件数据对象的样子

  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)

如果您知道如何在脚本中命名文件,请留言。


推荐阅读