0

I have an array of email addresses, like so:

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

I am passing it to a javscript script to send mail via the Mandrill api,as follows:

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);
    });
}

When to "to" variable is just one email address: "user1@gmail.com" there is no problem.

when I attempt to pass it as array,as above, the json response is:

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

and the mail is not sent.

How can I fix this and pass the array correctly? I have tried every combination, I can think of ....

user1903663
  • 1,713
  • 2
  • 22
  • 44

1 Answers1

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

Use JSON instead of array in 'to'.

Chetan Bhopal
  • 436
  • 4
  • 12
  • Thanks I know that is what I am supposed to do, so how do I pass multiple emails from my javascript array into that? That's my question. The email addresses I want to use are already in a javascript array, you see. Also, "multiple" is important not just one. – user1903663 May 31 '13 at 07:40
  • var to = '[{"email" : user1@gmail.com , "name" : user1}, {"email" : user2@gmail.com , "name" : user}]; – Chetan Bhopal May 31 '13 at 07:45
  • If you have an array then you have to convert it into JSON first same as above format. – Chetan Bhopal May 31 '13 at 07:48
  • Thanks I appreciate your assistance, how do I do that? I have tried JSON.stringify(). The emails are already in a javascript array, so I ma stuck at that point. I have tried emails = [] JSON.stringify(emails) but no joy. How do I convert the array, in my code above, into a suitable object to pass to the mandrill email sending bit. I know what the end result should be, how do I get there? My question is how, not what. I cannot hard code it because there may be hundreds of email addresses. – user1903663 May 31 '13 at 08:15
  • 1
    var to = '[user1@gmail.com,user2@gmail.com,user3@gmail.com]'; var obj = {}; for(i in to){ obj[i] = to[i]; } Now you will have a JSON in obj and you can pass it to the mandrill call now. – Chetan Bhopal May 31 '13 at 08:23
  • your javascript array is not correct. it should read as to = ['user1@gmail.com','user2@gmail.com','user3@gmail.com'] correct this first and then proceed with my code in last comment – Chetan Bhopal May 31 '13 at 08:28
  • Thank you, my question is still how! I don't know how to do this. I am afraid you are going to have to spell it out for me. With the code that you propose above => to = ['user1@gmail.com','user2@gmail.com','user3@gmail.com'] followed by var obj = {}; for(i in to){ obj[i] = to[i]; } (which does not work, probably I have done it wrongly) what is the object that is passed to mandrill? to or obj? So is it "to":[{"email":to}], or "to":[{"email":obj}]? I have tried both and neither works! I appreciate your kind assistance. – user1903663 May 31 '13 at 08:41
  • You have to send me the code for how you are creating a array of emails. you said that this array is made dynamically. If this is an array then why are you converting the array to string. – Chetan Bhopal May 31 '13 at 08:49
  • thanks that's very kind of you, how can I send it? My email address is richardwi@gmail.com if you want to ping me I can send by return. – user1903663 May 31 '13 at 08:51
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/30970/discussion-between-chetan-bhopal-and-user1903663) – Chetan Bhopal May 31 '13 at 08:52