Meteor 将用户信息传递到SSR html电子邮件
我已经创建了一封在点击按钮时发送的电子邮件,但是我不确定如何将用户名输入到电子邮件中。在服务器端有可变数据Meteor 将用户信息传递到SSR html电子邮件,meteor,Meteor,我已经创建了一封在点击按钮时发送的电子邮件,但是我不确定如何将用户名输入到电子邮件中。在服务器端有可变数据emailData。如何在电子邮件中输入用户的名字 路径:数据库架构 "profile": { "firstName": "SomeGuy", } 路径:server/email.js // In your server code: define a method that the client can call Meteor.methods({ sendEmail:
emailData
。如何在电子邮件中输入用户的名字
路径:数据库架构
"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');
}
});
'submit #myForm': function () {
var otheruserId = FlowRouter.getParam('id');
Meteor.call('sendEmail',
'test@email.com',
'Hello from Meteor!',
otheruserId);
}
更新
路径: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');
}
});
'submit #myForm': function () {
var otheruserId = FlowRouter.getParam('id');
Meteor.call('sendEmail',
'test@email.com',
'Hello from Meteor!',
otheruserId);
}
如果您想要发出请求的用户的用户名,那么可以像这样使用
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);
如果您想要发出请求的用户的用户名,那么可以像这样使用
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);
这很有效。如果用户正在查看另一个用户帐户,该怎么办。如何获取该用户的名称。@bp123如果您需要其他用户的名称,则需要有一个唯一字段,如该用户的
id
、username
或email
,以便您可以执行var user=Meteor.users.findOne({u id:id})
(或类似的查询用户名
或电子邮件
)而不是var user=Meteor.user()
。Yeh在客户端我使用self.autorun(function(){var id=FlowRouter.getParam('id');self.subscribe('candidateProfile',id);})代码>如何在服务器端使用它?@bp123更新了答案。请让我知道,如果这是你要找的。好的。接下来是如何将其从单击事件更改为提交事件。提交表单时,如果表单提交正确,则会发送电子邮件。这很有效。如果用户正在查看另一个用户帐户,该怎么办。如何获取该用户的名称。@bp123如果您需要其他用户的名称,则需要有一个唯一字段,如该用户的id
、username
或email
,以便您可以执行var user=Meteor.users.findOne({u id:id})
(或类似的查询用户名
或电子邮件
)而不是var user=Meteor.user()
。Yeh在客户端我使用self.autorun(function(){var id=FlowRouter.getParam('id');self.subscribe('candidateProfile',id);})代码>如何在服务器端使用它?@bp123更新了答案。请让我知道,如果这是你要找的。好的。接下来是如何将其从单击事件更改为提交事件。提交表单时,如果表单提交正确,则发送电子邮件。