Reputation: 3190
I want to add the email functionality to my application. I have added the email package and followed steps according to the documentation provided
I want when the user registers itself, an email should be sent after successful registration.
Here is what I tried:
server/smtp.js:
Meteor.startup(function () {
smtp = {
username: '[email protected]', // eg: [email protected]
password: 'abc123', // eg: 3eeP1gtizk5eziohfervU
server: 'smtp.gmail.com', // eg: mail.gandi.net
port: 25
}
process.env.MAIL_URL = 'smtp://' + encodeURIComponent(smtp.username) + ':' + encodeURIComponent(smtp.password) + '@' + encodeURIComponent(smtp.server) + ':' + smtp.port;
});
here is my server/emp_details.js where i have called methods. the following code is placed inside Meteor.methods()
:
sendEmail: function (to, from, subject, text) {
check([to, from, subject, text], [String]);
// Let other method calls from the same client start running,
// without waiting for the email sending to complete.
this.unblock();
//actual email sending method
Email.send({
to: to,
from: from,
subject: subject,
text: text
});
}
And finally I called the method at client side as shown:
Template.register.onRendered(function()
{
var validator = $('.register').validate({
submitHandler: function(event)
{
var email = $('[name=email]').val();
var password = $('[name=password]').val();
var empProfile = Session.get('profileImage');
console.log(empProfile);
Accounts.createUser({
email: email,
password: password,
profile: {
name:'test',
image: empProfile
},
function(error)
{
if(error)
{
if(error.reason == "Email already exists.")
{
validator.showErrors({
email: "This email already belongs to a registered user"
});
}
}
else
{
Meteor.call('sendEmail',
'[email protected]',
'[email protected]',
'Hello from Meteor!',
'This is a test of Email.send.');
Router.go("home");
}
}
});
}
});
});
I don't know how to use this email functionality properly.
Upvotes: 2
Views: 1365
Reputation: 2010
May I suggest an alternate solution? Add a hook to Accounts.onCreateUser() on the server side to send the email:
Accounts.onCreateUser(function (options, user) {
Meteor.call(
'sendEmail',
'[email protected]',
user.profile.email, //use the path to the users email in their profile
'Hello from Meteor!',
'This is a test of Email.send.'
);
});
Are you actually using gmail? The SMTP port for google is 465 I believe, not 25. Confirm that your email sending works outside of meteor using that config, and then attempt it in Meteor. I also believe google limits the number of emails sent via SMTP to 99 per day so be careful with that. If you want to verify a users email address, use the built in email verification system in the accounts
package.
Upvotes: 4