Search code examples
javascriptjsonmandrill

Unable to send email array in mandrill JSON response


Hi I have a form which the user inputs a message and sends it to multiple recipients. I am using Mandrill as my email client and the script is as follows

function sendMail() {

    var email = $("#email").val();
    var array = email.split(',');
    var name = $("#name").val();
    var msg = $("#msg").val();


$.ajax({

  type: 'POST',
  url: 'https://mandrillapp.com/api/1.0/messages/send.json',
  data: {
    'key': 'xxxxxxx',
    'message': {
      'from_email': 'xxx@xxx.com',
      'to': [
          {
            'email': email,

            'type': 'to'
          }
        ],
      'autotext': 'true',
      'subject': 'Hello world ' + name,
      'html': msg + '<br> <br> <img src="http://res.cloudinary.com/dzmi0gssmwn/image/upload/v1434831725/wpeqmuld5gzdplf7kzcw.jpg" width:320px;height:auto/>'
    }
  }
 }).done(function(response) {
        alert('Your message has been sent. Thank you!'); // show success message
        $("#name").val(''); // reset field after successful submission
        $("#email").val(''); // reset field after successful submission
        $("#msg").val(''); // reset field after successful submission
    })
    .fail(function(response) {
        alert('Error sending message.');
    });
  }

</script> 

When I input one email address the message gets successfully sent and received in the inbox. But when I submit with multiple addresses, I get a response that the message has been sent, but when I looked at the logs it states

[{"email":"xxxx@xxx.xxx, xxxx@xxx.xxx","status":"invalid","_id":"c045cbffbd0c4c5ab5581f5edaff2007","reject_reason":null}]

Does anybody know how to solve this issue or understand whats going wrong ? Thanks for taking the time to read this.


Solution

  • First just use mandrill API through js SDK

    <script type="text/javascript" src="mandrill.min.js"></script>
    

    It can be found here.

    var m = new mandrill.Mandrill('api key');
    
    function sendTheMail(){
        m.messages.send({
            "message": {
                "from_email": "xx@xx.com",
                "from_name": "test",
                "to":[{"email": "xx@x.com", "name": "someone's_name"}],
                "subject": "subj",
                "text": "msg" 
            }
        });
    }
    

    Shouldn't you be doing this on server, however, since this will basically show your API to public, and once it's public even for millisecond a bot will have it saved forever.