【问题标题】:Use Mandrill API in Google Apps Script在 Google Apps 脚本中使用 Mandrill API
【发布时间】:2013-10-21 15:53:48
【问题描述】:

我想在我的谷歌应用程序脚本中使用 mandrill 电子邮件发送 API。在谷歌脚本中,我必须使用 JSON 代码,但我不知道如何使用它。我是 Google 应用脚本的新手。

var m = new mandrill.Mandrill('XXXXXXXXXXX');
var from_email = "user4@gmail.com";
var to = '[
{
  "email": "recipient.email@example.com",
    "name": "Recipient Name"
}
],';

// create a variable for the API call parameters
var params = {
  "message": {
    "from_email":from_email,
    "to":[{"email":to}],
    "subject": "Sending a text email from the Mandrill API",
    "text": "I'm learning the Mandrill API at Codecademy, it's very difficult."
  }
};

function sendTheMail() {
  // Send the email!
  alert('this is a mail script');
  m.messages.send(params, function(res) {
    log(res);
  }, function(err) {
    log(err);
  });
}

我不知道如何在 Google Apps 脚本中使用此代码。

【问题讨论】:

    标签: google-apps-script mandrill


    【解决方案1】:

    您需要使用 urlfetchapp。

    var url = "https://mandrillapp.com/api/1.0/messages/send.json";
    var your_key = "xxxxxxxxxxxxx";
    var from_email = "user4@gmail.com";
    var to = [{
        "email": "recipient.email@example.com",
        "name": "Recipient Name"
    }];
    
    var params = {
        "key": your_key,
        "message": {
            "from_email":from_email,
            "to":[{"email":to}],
            "subject": "Sending a text email from the Mandrill API",
            "text": "I'm learning the Mandrill API at Codecademy, it's very difficult."
        }
    };
    
    var payload = JSON.stringify(params);
    
    var options = {
        'method': 'post',
        'payload': payload,
        'contentType' : 'application/json'
    };
    
    var response = UrlFetchApp.fetch(url, options);
    

    没有测试过这段代码,但应该是这样的。

    【讨论】:

    • 我收到了这个错误Request failed for https://mandrillapp.com/api/1.0/messages/send.json returned code 500.
    • 请查看我的代码更改。您需要对 json 进行字符串化。尝试使用 chrome 扩展“邮递员”来尝试发布到 API。
    • 我建议的唯一更改是:将 "to":[{"email":to}], 替换为 "to":to, den 它可以正常工作...
    • 嘿,如果我想发送模板,我该怎么办?而不是var response = UrlFetchApp.fetch(url, options); 我必须使用什么?
    【解决方案2】:

    我粘贴示例代码示例以通过 Mandrill 发送带有来自 Google Drive 的附件文件的电子邮件。

    function sendEmail() {
    
      var MANDRILL_API_KEY = "<<your key here>>";
    
      var files = [
        "<<Google Drive File ID 1>>",  
        "<<Google Drive File ID 2>>",  
        "<<Google Drive File ID 3>>"  
      ];
    
      var recipients = [
        {
          "email": "ctrlq+to@labnol.org",
          "name": "Amit Agarwal",
          "type": "to"
        }, {
          "email": "ctrlq+cc@labnol.org",
          "type": "cc"
        }, {
          "email": "ctrlq+bcc@gmail.com",
          "type": "bcc"
        }
      ];
    
      var attachments = [];
    
      for (var f in files) {
        var file = DriveApp.getFileById(files[f]);
        attachments.push({
          "type": file.getMimeType(),
          "name": file.getName(),
          "content": Utilities.base64Encode(file.getBlob().getBytes())
        });
      }
    
      var params = {
        "key": MANDRILL_API_KEY,
        "message": {
          "from_email": "<<Sender's Email Address>>",
          "from_name": "<<Sender Name>>",
          "to": recipients,
          "attachments": attachments,
          "headers": {
            "Reply-To": "reply@example.com"
          },
          "subject": "Enter email subject",
          "text"   : "Enter email body in plain text",
          "html"   : "Enter HTML content with <b>tags</b>"
        }
      };
    
      var response = UrlFetchApp.fetch(
        "https://mandrillapp.com/api/1.0/messages/send.json", {
          'method': 'POST',
          'payload': JSON.stringify(params),
          'contentType': 'application/json'
        });
    
      Logger.log(response.getContentText());
    }
    

    示例代码提取自website ctrlq of Amit Agarwal

    【讨论】:

      猜你喜欢
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多