A Simple Email Integration Between Gravity Forms on WordPress and Salesforce

Gravity Forms is a well know form builder that runs on WordPress.  It is simple to spin up a form to collect data for whatever you need.  But what if that data needs to get to Salesforce? 

You could try to interact with the Salesforce API.  Salesforce has great APIs for interacting with its data, but doing that on the WordPress site can be pretty technical.  Let’s try to keep this simple.

Let’s just have Gravity Forms send an email to Salesforce with the results of what was entered on the form.  This is how Gravity Forms works already for most people.  The efficiency problem though is that the usually email ends up in somebody’s inbox and then the data needs to be copy/pasted into another system.

But Salesforce has a way to process this email for you.  It is called an Email Service.  Here is how this can be done.

1) Create an Apex Class that implements Messaging.InboundEmailHandler
Here is an example piece of code that will accept a Gravity Form email and then parse it into a list that will contain the questions and answers.  It would need to be extended for your specific questions and then what action to take.  Maybe you want to convert a lead or create an opportunity.

global class inboundSalesEmail implements Messaging.InboundEmailHandler {
	//Email for this service: .(JavaScript must be enabled to view this email address)
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env){    
  	    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
		system.debug('Subject: ' + email.Subject);
  	    //system.debug('PlainText Body: ' + email.plainTextBody);
  	    //system.debug('HTML Body: ' + email.htmlBody);
  	    //The email comes in broken by line feed
  	    list splitBody = new list();
  	    splitBody = email.plainTextBody.split('\n');

		//Now that we have a list of the lines spin through them
		// and remove the blank rows
  	    list splitItems = new list();
  	    for(string s : splitBody){
  	    	if(string.isBlank(s) == false){
  	    //Here is where you would do your logic.  
  	    //Both the questions and the answers from the Gravity Form 
  	    // are in this list of strings.
  	    for(string s : splitItems){
  	    	system.debug('Incoming Item: ' + s);
  	    result.success = true;
  	    return result;

This code is making use of the plainTextBody of the email.  You could get the htmlBody too from the email parameter for more detailed parsing, but that is going to contain all of the table tags like


and all of the formatting.  I really just wanted to get right at the data for this example.

2) Create an Email Service
a) Go to Setup, Build, Develop, Email Service
b) Click on New Email Service
c) Fill out the form and specify the Apex class that you created above
d) After the Email Service is saved click on the ‘New Email Address’ button to generate an email for this Email Service.
e) This last step is the piece that ties is all together.  It is saying that when Salesforce receives this email, that it will route the email to the Apex class that was created above.

That is really all there is to it!  Your Gravity Form and Salesforce are now linked!

You will have to be careful about how Gravity Forms sends over tables and other more complicated structures.  As you go through the loop you may need to build a set of questions so you know where they start and stop. 
I wrote a blog post last week about creating a task with an email service too if you want to look into how to write a test class for your email service.

An email integration is just as powerful as a REST/SOAP integration.  It gets the data where it needs to go and it stops the dreaded time-killer of copy/paste work. 

Let me know if Sundog can help you with your next integration!


Returning visitor? Please login or register.

Leave A Comment