Search code examples
javajavascriptandroidparse-platformmandrill

How to Use ParseObject in a Mandrill Template to send dynamic content emails?


Hello I am designing an app which needs to send CSS/HTML mail to various clients. What i have done so far is complete the app and send all the data to be stored in Parse Cloud as parse objects. Also made Mandrill profile , uploaded desired template via MailChimp and generated API key to be used in Cloud Code for Parse. My query is how to use these parse objects in my mail so that content of mail changes dynamically(I have few table entries in mail which should change according to data entered by user)? If possible could you provide me with sample JS code. I have Googled about this but couldn't find much documentation related to this topic. Or should i use some Mandrill API in some other languages such as Python? Thanks in advance.!!


Solution

  • currently I am trying out Mandrill Java 3rd party API and I think this is the complete answer for you:

    I am now going to describe you how to use the handlebars of the Mandrill Templates:

    Let's say this is my template at Mandrill Account named "my_test_template":

    <!DOCTYPE html PUBLIC>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>Test Template Email</title>
      </head>
      <body>
        <p> Hello my name is {{name_placeholder}} and this is the test template.</p>
      </body>
    </html>
    

    Normally, to set dynamic content for "name_placeholder", you must use this json object with javascript rest call:

    {
      "key" : "[api_key]",
      "message" : {
        "to" : [ {
          "email" : "someone@something.com",
          "name" : "Someone"
        } ],
        "merge" : true,
        "global_merge_vars" : [ {
          "name" : "name_placeholder",
          "content" : "Someone"
        } ],
        "merge_vars": [],
        "async" : false
      },
      "template_name" : "my_test_template",
      "template_content" : [],
      "merge_language": "handlebars"
    }
    

    What you must be careful about here is "merge_language". If you are using handlebars, it is just supported by "handlebars" language. If you do not set this data, it will fall to default language "mailchimp" and your dynamic content won't be replaced.

    To provide this, you can use the below java code which is using this Java 3rd party API :

    public void testTemplateMail() {
        try {
            MandrillMessage message = new MandrillMessage();
    
            //Set recipient
            ArrayList<Recipient> recipients = new ArrayList<Recipient>();
            Recipient recipient = new Recipient();
            recipient.setEmail("someone@something.com");
            recipient.setName("Someone");
            recipients.add(recipient);
            message.setTo(recipients);
            message.setPreserveRecipients(true);
    
            //Set global merge vars
            List<MergeVar> globalMergeVars = new ArrayList<>();
            MergeVar mergeVar = new MergeVar();
            mergeVar.setName("name_placeholder");
            mergeVar.setContent("Someone");
            globalMergeVars.add(mergeVar);
            message.setGlobalMergeVars(globalMergeVars);
    
            //Set merge language (*important)
            message.setMergeLanguage("handlebars");
    
            //You must provide at least an empty template content
            Map<String, String> template_content = new HashMap<>();
    
            //Send mail
            MandrillMessageStatus[] messageStatusReports = mandrillApi
                    .messages().sendTemplate("my_test_template", template_content, message, false);
            if (messageStatusReports != null && messageStatusReports.length > 0) {
                logger.info("Mail sent info: " + messageStatusReports[0].getStatus());
            }
        } catch (MandrillApiError e) {
            logger.error(e.getMessage());
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }
    

    Note that my subject and from email information is set to the mail template.

    To provide more detail info for the handlebars, you can visit this site.