Reputation: 633
var SITE_URL = Meteor.absoluteUrl();
function sendEmailNotification(type, sourceUser, recipients, notificationObject) {
var emailFrom = '[email protected]';
var emailSubject = 'MyApp: ';
var emailBody = '';
$.each(recipients, function (index, recipient) {
recipients[index] = recipient + '@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;
}
if (Meteor.isServer) {
// This function only runs on server
Email.send({
from: emailFrom,
bcc: recipients,
subject: emailSubject,
html: emailBody
});
}
}
The above function is in a JS file in the root (and so its code is available to both client and server). But when I call it in my client code, nothing happens. I have the email
package included in my app. On my local machine (Windows 7), I don't have the MAIL_URL
variable set. So calling the Email.send()
function should ideally produce an output in command prompt, but nothing actually gets outputted.
On our production server, SMTP is properly setup and other applications are able to send emails with same settings. I've configured the MAIL_URL
environment variable correctly there, but still no emails get sent.
Could somebody tell me if there's a problem with my code? Is there anything I'm not doing correctly?
P.S.: I even tried calling the Email.send() directly like in the code below, but still nothing happened.
if (Meteor.isServer) {
Email.send({
from: '[email protected]',
to: '[email protected]',
subject: 'This is a test email',
html: '<b>Congrats, it works!</b>'
});
}
}
});
Upvotes: 2
Views: 1401
Reputation: 633
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 = '[email protected]';
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);
Upvotes: 0
Reputation: 1491
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.
Upvotes: 3