我一直在尝试在成功提交autoform后发送电子邮件。我已经尝试使用template.events'report',它不起作用,我试图使用metermethod =“sendEmail”。我所做的一切似乎都没有用。有人可以告诉我我做错了什么。
路径:form.html
{{#autoForm collection="JobOffers" id="jobOfferForm" type="insert" meteormethod="sendEmail"}}
<fieldset>
{{> afQuickField name='firstName'}}
<button type="submit" data-meteor-method="sendEmail" class="btn btn-primary">Submit</button>
</fieldset>
{{/autoForm}}
路径:server/email.js
sendEmail: function (from, subject, userId) {
check([from, subject, userId], [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' ) );
// to find the users info for the logged in users
// var user = Meteor.user();
var user = Meteor.users.findOne({ _id: userId });
var email = (user && user.emails[0].address);
var emailData = {
// name: (candidate && candidate.profile && candidate.profile.firstName),
name: (user && user.profile && user.profile.firstName),
// favoriteRestaurant: "Honker Burger",
// bestFriend: "Skeeter Valentine"
};
Email.send({
to: email,
from: from,
subject: subject,
html: SSR.render( 'htmlEmail', emailData )
});
console.log('sendEmail sent');
}
});
更新
路径:form.js
AutoForm.hooks({
jobOfferForm: hooksObject
});
var hooksObject = {
after: {
insert: function(error, result){
Email.send({
var otheruserId = FlowRouter.getParam('id');
Meteor.call('sendEmail',
'test@email.com',
'Hello from Meteor!',
otheruserId);
};
}
}
};
答案 0 :(得分:2)
您可以使用autoform的回调/挂钩。如果您想在插入以下内容后发送电子邮件,那将是一个解决方案:
var hooksObject ={
after: {
insert: function(error, result){
//Send email here
}
}
}
<强>更新强>
var hooksObject = {
after: {
insert: function(error, result){
var otheruserId = FlowRouter.getParam('id');
Meteor.call('sendEmail',
'test@email.com',
'Hello from Meteor!',
otheruserId);
}
}
};
AutoForm.addHooks('jobOfferForm', hooksObject);
有关详细信息,请参阅autoform documentation。