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.
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
Domain
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
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:
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:
We do not want to hard-code our Mailgun Secret API Key (it is a secret, after all!), so we are going to want to use an environment variable for that (and no default value). For an example of using environment variables, see how we set the PORT number in src/app.js. When we run the app, we will need to use something like MAILGUN_SECRET=mysupersecretapikey node src/app.js for sending emails to work. (Might also want to update the README.md file to indicate that.)
We also do not want to hard-code the email address that Mailgun sends to, since that will be different for you than it will be for me (because we both have our own Mailgun accounts). Consider using an environment variable for that as well, like MAILGUN_RECIPIENT.
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.
If err is defined, there was an error and we can return res.status(400).send(err.message);.
Otherwise, we can return res.sendStatus(204); for now.
Check List
[x] Set up Mailgun account
[x] Note your Secret API Key and Domain
[x] Add your personal email as an Authorized Recipient
[x] Verify your personal email as an Authorized Recipient
[x] Install mailgun-js using npm install --save
[x] Create src/mailer.js module that exports a sendText function
[x] Import the mailer module in src/app.js
[x] Call mailer.sendText from the contact form endpoint in src/app.js
[x] Use environment variables for the email recipient and API Secret Key
[x] Update README.md to let others know how to configure the mailer module
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
Domain
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
Step 2: Add mailgun-js Node Module
From the command line, run:
This will install a Mailgun SDK (software developers kit) into your
node_modules
directory and add the dependency topackage.json
so others can install it when they runnpm 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 calledsendText
that looks like this: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:
PORT
number insrc/app.js
. When we run the app, we will need to use something likeMAILGUN_SECRET=mysupersecretapikey node src/app.js
for sending emails to work. (Might also want to update the README.md file to indicate that.)MAILGUN_RECIPIENT
.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:Note the
./
in the require string. That indicates the module is in the current directory, rather than thenode_modules
directory. Ifmailer.js
were in a different directory, we would need to adjust that require string (ex:require('./lib/mailers.js')
if it were in thesrc/lib
directory).Next, in the
app.post('/api/forms/contact')
callback function, initialize the variablesto
,from
,subject
, andmessage
based on theMAILGUN_RECIPIENT
environment variable and the form data inreq.body
. Then callmailer.sendText()
with those arguments and a callback function that accepts two arguments:err
andbody
.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.err
is defined, there was an error and we canreturn res.status(400).send(err.message);
.return res.sendStatus(204);
for now.Check List
mailgun-js
usingnpm install --save
src/mailer.js
module that exports asendText
functionmailer
module insrc/app.js
mailer.sendText
from the contact form endpoint insrc/app.js
README.md
to let others know how to configure themailer
module