2
我想將電子郵件功能添加到我的應用程序中。我已添加電子郵件包,並按照documentation提供的documentation提供使用meteor js成功註冊用戶後發送電子郵件
我希望當用戶註冊自己時,應該在成功註冊後發送電子郵件。
這裏是我的嘗試:
服務器/ 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;
});
這裏是我的服務器/ emp_details.js在那裏我有調用的方法。下面的代碼放在裏面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
});
}
最後,我呼籲在客戶端的方法如下所示:
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");
}
}
});
}
});
});
我不知道如何正確地使用這個電子郵件功能。
謝謝佈雷特,它的工作,只是一個user.profile.email遊戲 –
請建議一個備用解決方案也。我也想嘗試一下。 –