jQuery从阵列发送带有mandrill的电子邮件 [英] jquery sending email with mandrill from an array

查看:108
本文介绍了jQuery从阵列发送带有mandrill的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此拼命挣扎.我想从一个复选框表中收集一组电子邮件地址(有些已选中但不是全部),并将它们传递给Mandrill JavaScript代码进行发送.

I am struggling with this desperately. I want to collect an array of email addresses from a checkboxed table (some checked but not all) and pass them to Mandrill JavaScript code to send.

这里有2个问题:

  1. 一般如何发送多封电子邮件和
  2. 如何将数组传递给山rill代码以执行电子邮件发送.

代码是:

<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>

电子邮件地址数组的警报为:

The alert of the array of email addresses is:

["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}]

一般来说,以下内容仅适用于一个电子邮件地址:

On a more general note, the following works for just one email address:

收件人":[{电子邮件":"user@gmail.com"}],

"to": [{"email" : "user@gmail.com"}],

但是

收件人":[{电子邮件":"user@gmail.com","anotheruser@gmail.com"}],

"to": [{"email" : "user@gmail.com", "anotheruser@gmail.com"}],

不能,所以我什至不能硬编码多次发送.

does not, so I am not even able to hard code a multiple send.

有什么想法吗?感谢所有的帮助.

Any ideas? All help gratefully received.

推荐答案

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

This is invalid javascript "to": [{"email": "email1", "email2"}]

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

在javascript中,[]是一个数组,而{}是一个具有键/值对的对象.因此,"to"应该是像{"email": "some@email.com"}

In javascript the [] is an array and {} is an object with key/value pairs. So "to" should be an array of objects like {"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从阵列发送带有mandrill的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆