表单提交后发送邮件

sendEmail after form submitted

我一直在尝试在成功提交自动表单后发送电子邮件。我试过使用 template.events 'submit' 但没有用,我试过使用 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);       
            };          
        }
    }
};

您可以使用 callbacks/hooks 的自动表格。如果您想在插入后发送电子邮件,以下是一个解决方案:

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