0

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.

There are 2 issues here:

  1. How to send multiple emails in general and
  2. How to pass an array to the mandrill code to execute email sending.

The code is:

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

and the resulting error message is:

[{"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:

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

but

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

user1903663
  • 1,713
  • 2
  • 22
  • 44

1 Answers1

1

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

Change it to "to": [{"email": "email1"},{"email": "email2"}]

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

Edit

To map your array of emails to an array of such objects you can for instance use jQuery.map

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

And then change your params to the following

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."
    }
};
fredrik
  • 13,282
  • 4
  • 35
  • 52
  • thanks, very helpful, that may answer the hard coding issues, how about passing the array from the checkbox table? Do you have a suggestion for that, please? Using the checkbox table is a more important issue for me. – user1903663 Jun 01 '13 at 10:02
  • I'm not sure I understand the issue, is that not what you already do? – fredrik Jun 01 '13 at 10:10
  • I should add that I have tried var emails = {} instead of [], doesn't work. – user1903663 Jun 01 '13 at 10:16
  • No, it's not. I need to pass the data collected form the checkboxes, in an acceptable format, to the mandrill script to send the emails. You have provided an answer to me "hard coding" it that is writing the email addresses myself. When using checkboxes there may be 100 unpredictable addresses, I cannot hard code them all. Pleas ejust focus on the main script above. – user1903663 Jun 01 '13 at 10:18
  • the question is what do I need to do to make this part of my code, emails.push($(this).val());, be in the right format to be acceptable to the mandrill script? I don't know how to do it. – user1903663 Jun 01 '13 at 10:21
  • I've added an example of what you could do. That should work, but I've never used mandrill, so you'll have to try it out. – fredrik Jun 01 '13 at 10:25
  • thanks, so in the bit "$.map(emails, function(email)" emails is the array created by mails.push($(this).val());? Point is that I do not know which checkboxes will be checked so it will be unpredictable. This looks very promising though, I will experiment. Grateful for your confirmation of above if you can ... – user1903663 Jun 01 '13 at 10:36
  • yes, `emails` is the array you've already built up. This code will only map that array into an array of objects – fredrik Jun 01 '13 at 10:58
  • You are a scholar, a gentleman and a star. It works! Thank you. The relevant bit of code now reads: $('tr > td:first-child > input:checked').each(function() { 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); var emailObjects = $.map(emails, function(email) { return {"email": email}; }); – user1903663 Jun 01 '13 at 11:01
  • 1
    I am so pleased, thank you. I have spent ages banging my head against the wall with this. – user1903663 Jun 01 '13 at 11:01