Wylio / meteor-mandrill

Meteor package for sending email via Mandrill
https://atmospherejs.com/wylio/mandrill
32 stars 8 forks source link

Setting From Name/Email/Subject Using Meteor's Accounts.emailTemplates #13

Closed mayvn10 closed 9 years ago

mayvn10 commented 9 years ago

Hi @dandv:

What's the right method to setting the From Name, From Email, and Subject via Meteor's Accounts.emailTemplates?

I tried to use the following but it wasn't working, so how exactly can you set them?

Accounts.emailTemplates.verifyEmail.html = function (user, url) {
    var result;
    try {
        result = Mandrill.templates.render({
            key: 'keyHere',
            subject: 'Verify your email address',
            from_email: 'contact@domain.com',
            from_name: 'Site Name',
            template_name: 'verify-email',
            template_content: [
                {
                    name: 'CONFIRMURL',
                    content: url
                }
            ],
            merge_vars: [
                {
                    name: 'CONFIRMURL',
                    content: url
                }
            ]
        });
    } catch (error) {
        console.error('Error while rendering Mandrill template', error);
    }
    return result.data.html;
}

Thanks in advance.

dandv commented 9 years ago

Meteor has special fields for the From name, Email and Subject, so try this:

// general from and siteName
Accounts.emailTemplates.from = 'YourApp <support@yourapp.com>';
Accounts.emailTemplates.siteName = 'YourApp';
// override for verification emails
Accounts.emailTemplates.verifyEmail.from = 'YourApp Verifications <contact@domain.com>';
Accounts.emailTemplates.verifyEmail.subject = 'Verify your email address';
mayvn10 commented 9 years ago

@dandv Adding this method resulted in an Exception:

I20150825-23:41:03.888(-4)? Exception while invoking method 'createUser' TypeError: Property 'from' of object #<Object> is not a function
mayvn10 commented 9 years ago

@dandv I used the following instead and it works. The issue now is that for some reason this one sends 2 emails - 1 is the exact call below and the other is the default Accounts verification email but with a different from name:

        Mandrill.messages.sendTemplate({
            key: 'XXX',
            template_name: 'verify-email',
            template_content: [
                {
                    name: 'CONFIRMURL',
                    content: url
                }
            ],
            message: {
                subject: 'Activate your account',
                from_email: 'contact@domain.com',
                from_name: 'Domain',
                to: [
                    {
                        email: user.emails[0].address,
                        type: 'to'
                    }
                ],
                merge_language: 'handlebars',
                global_merge_vars: [
                    {
                        name: 'NAME',
                        content: user.username
                    }
                ],
                merge_vars: [
                    {
                        name: 'CONFIRMURL',
                        content: url
                    }
                ]
            }
        });
dandv commented 9 years ago

The extra email comes from Meteor's Accounts package, which can't really know that you've already sent and email by other means. That's why the correct workflow is to assign to Accounts.emailTemplates.verifyEmail.html the result of Mandrill.templates.render() and let Meteor do the emailing.

Can you try calling render with the parameter you've passed to sendTemplate?

PS: I saw a key in your code and have redacted it just in case it wasn't a test key.

mayvn10 commented 9 years ago

I actually tried these same params with render and it only causes an Exception because Mandrill's API doesn't include those fields, as seen here: https://mandrillapp.com/api/docs/templates.JSON.html#method=render

The reason those fields work with sendTemplate is because of the following fields in this example: https://mandrillapp.com/api/docs/messages.JSON.html#method=send-template

If the best method is to use render, I wonder why calling template_name doesn't also carry in the default From Name, From Email, and Subject that I set on the Mandrill end.

PS: Thanks for redacting! Sometimes I lose track of what I'm copying and pasting because of all the testing.

mayvn10 commented 9 years ago

Correction - when you use the params from sendTemplate into render it sends the default email template from Meteor's Accounts package.

mayvn10 commented 9 years ago

Got it to work finally (after 100+ test message sends)!

Here's what I did:

Accounts.emailTemplates.verifyEmail.html = function (user, url) {
    var result;
    try {
        result = Mandrill.templates.render({
            template_name: 'verify-email',
            template_content: [
                {
                    name: 'CONFIRMURL',
                    content: url
                }
            ],
            merge_vars: [
                {
                    name: 'CONFIRMURL',
                    content: url
                }
            ]
        });
    } catch (error) {
        console.error('Error while rendering Mandrill template', error);
    }
    return result.data.html;
}

Basically, I overlooked the key.

You don't need the key on a templates.render if you've already declared it on the startup.js Mandrill.config, and only the template_name, template_content, and merge_vars parameters work for a templates.render.

All in all, pay close attention to the examples on Mandrill and only include parameters that they specifically outline in their docs.

Thanks for your help @dandv