jimrubenstein / node-mandrill

A node.js wrapper for MailChimp's Mandrill API.
MIT License
133 stars 19 forks source link

Is there a way to send X-MC-MergeVars? #4

Closed theonetheycallneo closed 7 years ago

theonetheycallneo commented 10 years ago

X-MC-MergeVars: {"_rcpt": "emailadress@domain.com", "fname": "John", "lname":"Smith"}

Reference:

http://help.mandrill.com/entries/21678522-how-do-i-use-merge-tags-to-add-dynamic-content

marcellodesales commented 9 years ago

????? Does this work with this module? I'm having a hard time to get the templates to work using SMTP headers.

akhoury commented 9 years ago

Don't use the headers, use the options parameter

var mandrill = require("node-mandrill");

mandrill(
    // you might be able to use a different endpoint here though
    // here's the Messages endpoint, https://mandrillapp.com/api/docs/messages.JSON.html
    "/messages/send", {
        message: {
            to: [{
                email: "you@you.you",
                name: "You"
            }],
            from_email: "me@me.me",
            from_name: "Me",
            subject: "You just inherited $180M",
            html: "Please send me your bank account # so I can transfer it asap",
            auto_text: true,            
            // the merge vars are just another option
            global_merge_vars: [{
                "name": "var1",
                "content": "Global Value 1"
            }],
            merge_vars: [{
                "rcpt": "emailadress@domain.com",
                "vars": [{
                    "name": "fname",
                    "content": "John"
                }, {
                    "name": "lname",
                    "content": "Smith"
                }]
            }]
        } // end of message{} 
    },
    function(error, response) {
        if (error) {
            return console.log(JSON.stringify(error));
        }
        console.log(response);
    });
marcellodesales commented 9 years ago

I was using the nodemailer module instead... But your example works like a charm!

thanks!

tikal commented 7 years ago

With nodemailer and custom SMTP headers, you have to JSON.stringify your object:

'X-MC-MergeVars':

JSON.stringify({"_rcpt": "emailadress@domain.com", "fname": "John", "lname":"Smith"})

For multiple recipients:

'X-MC-MergeVars': [ JSON.stringify({"_rcpt": "emailadress@domain.com", "fname": "John", "lname":"Smith"}), JSON.stringify({"_rcpt": "emailadress2@domain.com", "fname": "James", "lname":"Smith"}) ]

jimrubenstein commented 7 years ago

I feel like manipulating the headers is a feature that is meant for using the SMTP exposure of the API -- since this module uses the HTTP+REST API, I don't think you need to use the X-MC-MergeVars header, and should opt for setting the merge vars using the "merge_vars" parameter as @akhoury mentioned.