ertemishakk
ertemishakk

Reputation: 547

Mailgun Batch sending doesn't work with personalised data

   var recipients = ['[email protected]']
    var recipientVars = {
        "[email protected]": {
            "first": "Ertem",
            "productImage": "https://ojeaustralia.s3.ap-southeast-2.amazonaws.com/IMG_4825.jpg1602122333765",
            "productName": "Black Marble",
            "productLink": "https://www.ojeaustralia.com.au/shop/Black-Marble/5f7af208ea0e37001763a350"
        }
    }


    var data = {
        sender: 'ojeaustralia.com.au',
        from: 'OJE AUSTRALIA <[email protected]>',
        to: recipients,
        subject: 'Did you forget something?',
        template: 'abandonedcart',
        "X-Mailgun-Recipient-Variables": JSON.stringify(recipientVars) 
    };

    mailgun.messages().send(data, function (error, body) {
        console.log(body);
    });

I cant send batch emails using variables. I don't need to create a mailing list as this is just a cron job to check abandoned carts at a eCommerce site. I need to send custom emails for people who've left items in their cart. However, sending batch email doesn't work and not well documented in the API.

Upvotes: 0

Views: 1016

Answers (2)

Olivier
Olivier

Reputation: 148

in short you cannot use X-Mailgun-Recipient-Variables with templates. Would be nice to see it mentionned somewhere in the docs

Upvotes: 1

ertemishakk
ertemishakk

Reputation: 547

Thank you for contacting Mailgun support.

To provide some additional clarity, you can do batch emails with custom variables using the X-Mailgun-Variables header with the supplied variables. The problem comes when you try to integrate the templates feature with batching. Templates (with handlebars support) and batching each has slightly different methods of supplying variables:

In order to use an array of objects with handlebars, the array must be passed with your initial POST request. Unfortunately, referencing the array from the Recipient Variables will not work in this case. This is due to how Recipient Variables change the data type from an array to a string when referencing Recipient Variables.

Handlebars are expecting an array of data, but the custom recipient variable data will be turned into a string on our side when the POST is made. So the issue is that batching and templating doesn't integrate all that well just yet. Batch messaging is pretty set feature-wise, but templating is only in the first iteration on our side. We are looking at making improvements to templating in the future that includes easier integration with batch messages, but there's no timeline on that as of yet.

You can set the subject in the handlebars using only "h:X-Mailgun-Variables" attribute, recipient-variables are not necessary.

Please let us know if you have any additional questions.

Upvotes: 1

Related Questions