Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I email out ApostropheCMS contact form submissions?

I created a contact-form module per this documentation: https://apostrophecms.org/docs/tutorials/intermediate/forms.html

Submissions work and submitted forms are showing under "Contact Forms" admin menu item.

I also want generate an email to a recipient email address for each submission. apostrophe-pieces-submit-widgets states that I can override the beforeInsert method to send email, if desired.

However, I'm not sure where I should override this method.

Should I add a beforeInsert method inside /lib/modules/contact-form/index.js self.submit method?

Or, should I create a project-level copy of apostrophe-pieces-submit-widgets, and override the beforeInsert method there (I feel like this is probably too global and therefore not ideal)?

Lastly, should I involve apostrophe-email?

like image 850
geochanto Avatar asked Dec 03 '18 02:12

geochanto


1 Answers

My answer here assumes you are in fact using apostrophe-pieces-submit-widgets. So you haven't had to write very much code so far. You have a pieces module already, let's call it products, and you have made a submit widgets module, products-submit-widgets, which extends apostrophe-pieces-submit-widgets. OK, great. Let's talk about how to take it the rest of the way to sending email.

The beforeInsert method intentionally starts out doing nothing at all, as it is your opportunity to do "something extra" in your project level code.

To override that method, create lib/modules/products-submit-widgets/index.js if you have not already done so. You probably already have that file with addFields and extend etc. per the documentation for apostrophe-pieces-submit-widgets.

Now your code can look like this:

module.exports = {

  // ALL OF YOUR EXISTING CONFIGURATION OF THE products-submit-widgets
  // MODULE STILL GOES HERE, then...

  construct: function(self, options) {
    self.beforeInsert = function(req, piece, callback) {
      return self.email(req, 'emailSubmission', {
        piece: piece
      }, {
        // can also specify from and other
        // valid properties for nodemailer messages here
        to: '[email protected]',
        subject: 'A new submission was received'
      }, callback);
    };
  }
}

Now, in lib/modules/my-pieces-module-name-submit-widgets/views at project level, create an emailSubmission.html file and populate it like this:

<h4>A new submission was received</h4>

A new submission was received from {{ piece.someFieldOrOther }}.

It contains this information:

{{ piece.someOtherField }}
{{ piece.yetAnotherField }}
{{ piece.etc }}

So far so good! But, in order for the self.email method to work, you do need to configure the apostrophe-email module. A bare-bones configuration might look like this, if you have an email delivery agent on your Linux server already:

// in app.js
modules: {
  'apostrophe-email': {
    // Default "from" address. Note your email can be dropped as spam
    // if this address is invalid or your server has no right to send
    // email for it
    from: '"Jane Doe" <[email protected]>',
    // See the nodemailer documentation, many
    // different transports are available, this one
    // matches how PHP does it on Linux servers
    nodemailer: {
      sendmail: true,
      newline: 'unix',
      path: '/usr/sbin/sendmail'
    }
  }
}

But, for more complete information on that please see sending email from your Apostrophe project. If you use a naive configuration your emails may be dropped as spam. If you don't plan to use a service like Amazon Simple Email Service or Postmark, and your volume is low, you can configure nodemailer to send messages through a valid gmail account without too much work.

Hope this is helpful! It would be a great PR to add this as an optional built-in feature of the apostrophe-pieces-submit-widgets module.

like image 75
Tom Boutell Avatar answered Nov 20 '22 22:11

Tom Boutell