Published: Oct 4, 2017 | Filed Under Google Cloud

Form Processing with Google Cloud Functions and MailGun

Processing forms is a very common task for dynamic web applications, and websites in general. Google cloud (compute engine) does not allow the sending of email, so this is a very logical use case for Google Cloud Functions. In this tutorial, we will be using Google Cloud Functions to process a contact form and send an email using MailGun’s API.

To begin, make sure that you have an active Google Cloud account with Google Cloud Functions enabled for your project, and a MailGun account.

Step 1: Sign up for MailGun Account & Get API Key

To get started, we need a MailGun account. If you sign up through this link you will get 30,000 free sends per month. Once signed in, click on your email on the top-right and click “Account Settings” then on the “API Keys” tab. MailGun API Key

Now go to the “Domains”. If you want to send from a custom domain, be sure to add and verify your domain. If you use the sandbox domain, the receiving email address from script must be the email address attached to the MailGun account. Authorized Sending Domains

Step 2 Require Packages & Configure API

var api_key = 'api-key-here',
var sending_domain = 'your-sending-domain'; // configure in Mailgun, use test domain if desired
var mailgun = require('mailgun-js')({apiKey: api_key, domain: sending_domain});

Step 3: Scaffold Project

Run npm init to start the project.

run npm install mailgun-js --save to install the Node module for Mailgin

Create an index.js file that exports the function processForm():

exports.processForm = function processForm(req, res) {
// future code here
};

Step 4: Parse Query Strings

Next, we need to parse the query strings from the request. This can be accessed from req.query and for this example we’ll be using fname, lname, email, and message, as variables. Note: we’re using decodeURIComponent to parse the URL encoded string for things like email addresses

exports.processForm = function processForm(req, res) {
var query = req.query,
fname = decodeURIComponent(query['fname']),
lname = decodeURIComponent(query['lname']),
email = decodeURIComponent(query['email']),
message = decodeURIComponent(query['message']),
};

Step 5: Send Email

Now we send the email. Right below the message statement we configure the data variable for the email:

data = {
from: fname + ' ' + lname + ' <' + email + '>',
to: '[email protected]',
subject: 'New Submission From ' + fname + ' ' + lname ,
text: message
}

Then we send the email and return the response from MailGun’s API:

mailgun.messages().send(data, function (error, body) {
if (error) {
return res.send(error);
}
res.send(body);
});

Bringing it all Together

Success! You now have a working email script. You can now post to the URL like passing in query strings like so https://[google-cloud-function-url]/processForm?fname=Zach&lname=Russell&email=zach%40protechig.com&message=Hello+can+you+help+me+with+this

Some other next steps would be to log messages to Google Cloud DataStore or configure email address routing based on form contents.

The full code is below:

var api_key = 'your-api-key-here',
sending_domain = 'your-sending-domain-here',
mailgun = require('mailgun-js')({apiKey: api_key, domain: sending_domain});

exports.processForm = function processForm(req, res) {
var query = req.query,
fname = decodeURIComponent(query['fname']),
lname = decodeURIComponent(query['lname']),
email = decodeURIComponent(query['email']),
message = decodeURIComponent(query['message']),
data = {
from: fname + ' ' + lname + ' <' + email + '>',
to: '[email protected]',
subject: 'New Submission From ' + fname + ' ' + lname ,
text: message
}

mailgun.messages().send(data, function (error, body) {
if (error) {
return res.send(error);
}
res.send(body);

});
};