meteormeteor-autoform

sendEmail after form submitted


I've been trying to send an email after a autoform has been successfully submitted. I've tried using the template.events 'submit' which didn't work and I've tried to use metermethod="sendEmail". Nothing I do seems to work. Can someone please tell me what I'm doing wrong.

Path: 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}}

Path: 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');
  }
});

UPDATE

Path: 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);       
            };          
        }
    }
};

Solution

  • You can use callbacks/hooks of autoform. If you want to send email after an insert following would be a solution:

    var hooksObject ={
        after: {
            insert: function(error, result){
                  //Send email here
            }
        }
    }
    

    UPDATE:

    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);
    

    Please refer to autoform documentation for more info.