我创建了一个电子邮件,在点击按钮时发送,但我不确定如何获取用户名并将其放入电子邮件中。有可变数据emailData
但是在服务器端。如何将用户的名字输入电子邮件?
路径:database schema
"profile": {
"firstName": "SomeGuy",
}
路径:server/email.js
// In your server code: define a method that the client can call
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();
SSR.compileTemplate( 'htmlEmail', Assets.getText( 'html-email.html' ) );
var emailData = {
name: "Doug Funny",
};
Email.send({
to: to,
from: from,
subject: subject,
html: SSR.render( 'htmlEmail', emailData )
});
}
});
路径:private/html-email.html
Hi {{name}},
This is a test email
路径:client / emailButton.js
Template.emailButton.events({
'click .send-email-button': function () {
Meteor.call('sendEmail',
'test@email.com',
'test@email.com',
'Hello from Meteor!',
'This is just some text. If removed this email send stops working');
}
});
更新
路径:client / emailButton.js
'submit #myForm': function () {
var otheruserId = FlowRouter.getParam('id');
Meteor.call('sendEmail',
'test@email.com',
'Hello from Meteor!',
otheruserId);
}
答案 0 :(得分:3)
如果您想要提出请求的用户的用户名,那么您可以使用此Meteor.user()
或Meteor.userId()
,
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();
SSR.compileTemplate( 'htmlEmail', Assets.getText( 'html-email.html' ) );
var user = Meteor.user();
// OR
// var userId = Meteor.userId();
// var user = Meteor.users.findOne({ _id: userId });
var emailData = {
name: (user && user.profile && user.profile.firstName) || ""
};
Email.send({
to: to,
from: from,
subject: subject,
html: SSR.render( 'htmlEmail', emailData )
});
}
});
更新:如果是针对不同的用户
由于您在客户端拥有其他用户的ID,因此您需要将其作为参数发送到Meteor.method。请参阅下面的方法,并附加参数userId
Meteor.methods({
sendEmail: function (to, from, subject, text, userId) {
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();
SSR.compileTemplate( 'htmlEmail', Assets.getText( 'html-email.html' ) );
var user = Meteor.users.findOne({ _id: userId });
var emailData = {
name: (user && user.profile && user.profile.firstName) || ""
};
Email.send({
to: to,
from: from,
subject: subject,
html: SSR.render( 'htmlEmail', emailData )
});
}
});
现在在客户端,你可以做到,
Meteor.call("sendEmail", to, from , subject, text, otheruserId);