0

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

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 脚本中使用此代码。

4

2 回答 2

2

您需要使用 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);

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

于 2013-10-14T10:09:32.777 回答
-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());
}

示例代码摘自 Amit Agarwal 的网站 ctrlq

于 2016-02-06T01:39:13.843 回答