sscovil / something-awesome

This is going to be something totally awesome. Seriously. Awesome.
MIT License
0 stars 1 forks source link

Wire up the contact form to Mailgun #17

Closed sscovil closed 7 years ago

sscovil commented 7 years ago

To make our contact form more useful, we are going to wire it up to a service that will send us an email whenever someone submits a message in the form. In this case, we are going to use a free email service called Mailgun that has a REST API we can use.

Step 1: Create a Mailgun Account

Start by creating an account at: https://www.mailgun.com/

On the Mailgun Dashboard, you'll want to make note of the Secret API Key and the Domain that have been created.

Secret API Key

screen shot 2017-08-25 at 11 46 02 am

Domain

screen shot 2017-08-25 at 1 43 36 pm

You will also need to add your personal email address as an Authorized Recipient and verify by clicking the link you get in an email from Mailgun.

Account Settings

screen shot 2017-08-25 at 1 46 39 pm

Step 2: Add mailgun-js Node Module

From the command line, run:

$ npm install --save mailgun-js

This will install a Mailgun SDK (software developers kit) into your node_modules directory and add the dependency to package.json so others can install it when they run npm install.

Step 3: Create src/mailer.js Module

We want to keep our code clean and easy to maintain, so we will organize our code into separate files to group specific functionality. For sending emails, we will create a file called mailer.js.

The mailer.js file should export a function called sendText that looks like this:

exports.sendText = function(to, from, subject, message, callback) {
  // code goes here
};

That function should call mailgun.messages().send(data, callback); as described in the docs.

HINT: There is an error in the example in those docs! You'll have to try using mailgun-js and logging any errors in your console to figure out where that error is. Welcome to programming. :-)

A couple of important things to note:

Step 4: Send Email When Contact Form Is Submitted

Now that we have our mailer module wired up to Mailgun, we can use it to send an email with any form data that is submitted. In src/app.js, require the mailer module:

const mailer = require('./mailer');

Note the ./ in the require string. That indicates the module is in the current directory, rather than the node_modules directory. If mailer.js were in a different directory, we would need to adjust that require string (ex: require('./lib/mailers.js') if it were in the src/lib directory).

Next, in the app.post('/api/forms/contact') callback function, initialize the variables to, from, subject, and message based on the MAILGUN_RECIPIENT environment variable and the form data in req.body. Then call mailer.sendText() with those arguments and a callback function that accepts two arguments: err and body.

In the callback function we pass to mailer.sendText(), we need to handle the case where there is an error and also handle the case where sending the message succeeds.

Check List

sscovil commented 7 years ago

Resolved by #20