将数组从jquery传递给mandrill api以发送邮件

时间:2013-05-31 03:55:23

标签: jquery mandrill

我有一系列电子邮件地址,如下所示:

var to = '[user1@gmail.com,user2@gmail.com,user3@gmail.com]';

我将它传递给javscript脚本以通过Mandrill api发送邮件,如下所示:

function log(obj) {
    $('#response').text(JSON.stringify(obj));
}

// create a new instance of the Mandrill class with your API key
var m = new mandrill.Mandrill('BK0z-Ark1NAZCkc2PwbSRw');
var from_email = "user4@gmail.com";
var to = '[user1@gmail.com,user2@gmail.com,user3@gmail.com]';
// 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);
    });
}

何时“to”变量只是一个电子邮件地址:“user1@gmail.com”没有问题。

当我尝试将其作为数组传递时,如上所述,json响应为:

[{"email":"[user1@gmail.com,user2@gmail.com,user3@gmail.com]","status":"invalid","_id":"1e25584e26c7447bb40b14dfc6b4f7fc","reject_reason":null}]

并且不发送邮件。

如何解决此问题并正确传递数组?我已经尝试过各种组合,我能想到......

1 个答案:

答案 0 :(得分:0)

"to": [
            {
                "email": "recipient.email@example.com",
                "name": "Recipient Name"
            }
        ],

在'to'中使用JSON而不是数组。