Skip to content Skip to sidebar Skip to footer

Meteorjs: Am I Using Email.send() Correctly?

var SITE_URL = Meteor.absoluteUrl(); function sendEmailNotification(type, sourceUser, recipients, notificationObject) { var emailFrom = 'server@example.com'; var emailSubj

Solution 1:

Pretty much a duplicate of Meteor's Email is undefined

See this pull request for sample code.

Just to clarify: Meteor does not execute client and server code in sequence like that. You have to be more explicit about what is running on the client vs the server. Instead of thinking in terms of linear execution along the JavaScript page, think that each piece of Meteor code runs as a result of an event. If some piece of code is not running, it is because there is no event that triggered it.

Solution 2:

I resolved it by creating a server-side method using Meteor.methods and placing the entire code above into it.

var SITE_URL = Meteor.absoluteUrl();

Meteor.methods({
    sendEmailNotification: function (type, sourceUser, recipients, notificationObject) {
        if (recipients.length > 0) {
            var emailFrom = 'app@example.com';
            var emailSubject = 'MyApp: ';
            var emailBody = '';
            for (var i = 0; i < recipients.length; i++) {
                recipients[i] = recipients[i] + '@example.com';
            }

            switch (type) {
                case'item_assigned':
                    emailSubject += notificationObject.item_title;
                    emailBody += '<div style="padding:10px;">';
                    emailBody += sourceUser;
                    emailBody += ' has assigned you an action item';
                    emailBody += '</div>'break;
                case'list_shared':
                    emailSubject += notificationObject.list_title;
                    emailBody += '<div style="padding:10px;">';
                    emailBody += sourceUser;
                    emailBody += ' has shared a list with you: ';
                    emailBody += '<a href="' + SITE_URL + '#' + notificationObject.list_id + '">' + notificationObject.list_title + '</a>';
                    emailBody += '</div>'break;
            }
            Email.send({
                from: emailFrom,
                bcc: recipients,
                subject: emailSubject,
                html: emailBody
            });
        }
    }
});

To call the above function in your client code, use:

Meteor.call('sendEmailNotification', 'list_shared', Meteor.user().username, [sharedUserName], listDetails);

Post a Comment for "Meteorjs: Am I Using Email.send() Correctly?"