jquery的从阵列发送电子邮件与山魈(jquery sending email with mand

2019-10-29 03:54发布

我这个拼命的挣扎。 我想收集电子邮件地址从checkboxed表数组(一些检查,但不是全部),并把它们传递给山魈JavaScript代码来发送。

这里有2个问题:

  1. 如何发送多封电子邮件,一般和
  2. 如何通过一个数组山魈代码来执行电子邮件发送。

该代码是:

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

    function sendTheMail() {
        // Send the email!
        alert('scripting');
        var emails = [];
        var first = [];
        var last = [];
        $('tr > td:first-child > input:checked').each(function() {
            //collect email from checked checkboxes
            emails.push($(this).val());
            first.push($(this).parent().next().next().text());
            last.push($(this).parent().next().next().next().text());
        });
        var new_emails = JSON.stringify(emails);
        alert(new_emails);
        //create a new instance of the Mandrill class with your API key
        var m = new mandrill.Mandrill('my_key');

        // create a variable for the API call parameters
        var params = {
            "message": {
                "from_email": "rich@pachme.com",
                "to": [{"email": new_emails}],
                "subject": "Sending a text email from the Mandrill API",
                "text": "I'm learning the Mandrill API at Codecademy."
            }
        };
        m.messages.send(params, function(res) {
            log(res);
        },
                function(err) {
                    log(err);

                });
    }
</script>

电子邮件地址的阵列的警示是:

["richardwi@gmail.com","richard.illingworth@refined-group.com","mozartfm@gmail.com"]

将得到的错误信息是:

[{"email":"[\"richardwi@gmail.com\",\"richard.illingworth@refined-group.com\",\"mozartfm@gmail.com\"]","status":"invalid","_id":"0c063e8703d0408fb48c26c77bb08a87","reject_reason":null}]

在更广泛的笔记,只是一个电子邮件地址了以下工作:

“到”:[{ “电子邮件”: “user@gmail.com”}],

“到”:[{ “电子邮件”: “user@gmail.com”, “anotheruser@gmail.com”}],

没有,所以我甚至无法硬编码多送。

有任何想法吗? 所有帮助感激地接受。

Answer 1:

这是无效的javascript "to": [{"email": "email1", "email2"}]

将其更改为"to": [{"email": "email1"},{"email": "email2"}]

在JavaScript中[]是一个数组, {}是具有键/值对的对象。 因此,“以”应该是这样的对象数组{"email": "some@email.com"}

编辑

为了您的电子邮件的阵列映射到你能够例如使用这种对象的数组jQuery.map

// Try doing something like this
var emailObjects = $.map(emails, function(email) {
   return {"email": email};
});

然后更改您的params为以下

var params = {
    "message": {
        "from_email": "rich@pachme.com",
        "to": emailObjects,
        "subject": "Sending a text email from the Mandrill API",
        "text": "I'm learning the Mandrill API at Codecademy."
    }
};


文章来源: jquery sending email with mandrill from an array