Everybody knows how to automatically turn customer emails into cases with Salesforce email to case. But doing this with a custom object would require the Salesforce email services.

What is Salesforce Email Services ?

Salesforce Email services are processes that let you take in inbound email and then process it using Apex class.

You could add custom logic inside this class to process inbound email headers, attachments, and its contents.

How to Setup Salesforce Email Services ?

Follow these easy steps to get Salesforce Email services up and running:

Step 1: Create an apex class

Before going ahead and creating an email service, we should first create an apex class. This Class should implement an interface called “Messaging.InboundEmailHandler“.

Example: How to create a Task and attach it to a contact whose email address is the same as the sender’s email address.

global class EmailServiceExample implements Messaging.InboundEmailHandler {
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope envelop){
		Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
		try {
			// Try to select a contact where email match from email address. (email.toAddress)
			// Create a new Task and attach it to the contact record found.
			// If an exception has occurs when selecting the contact, we will throw a QueryException to the Apex debug log.
		} catch (QueryException e) {
			System.debug('Query Issue: ' + e);
		}
		return results;
	}
}

Step 2: Create an Email Service

To create Salesforce email services, from Setup, go to: Develop > Email Services.

  • Click “New Email Service” Button to define a new email service.
  • Select the apex class that we have created in the first Step.
  • Activate it by Checking the Active checkbox.
  • Configure the Failure Response Settings.
  • Click Save.

Optionally, you can configure email service to accept emails from some senders. To do so, you should list their email addresses and domains in the “Accept Email From” textbox if you want to receive emails from anyone Leave this field blank.

Step 3: Create Email addresses

In this step, When saved the new email service, you will need to scroll down to the bottom of the page and create a new email address for this service.

You can add multiple email addresses. As a result, you process the same message differently for each email address.

When you create a new email service address, an email was automatically generated like the following. You should send your email to for processing: emailservice@1syegcr6ukrlnho24vjqahtg6ctxafxfxgjtx5cy190igmxn18b.0o-r3jtuay.eu29.apex.salesforce.com

Step 4: Test

Now, when you send a message to the above email, as a result, you should see a new Task created for the contact that has the same email address as sender.

Governor limit of Salesforce Email services

Salesforce limits the absolute number of messages that can process every day. Messages that surpass this limit are skipped, bounced, or queued to be handled the following day.

Salesforce calculates this limit as: “number of user licenses” x 1000, up to a daily total of 1,000,000.

Takeaway

Salesforce has all the standard services to easily handle inbound & outbound emails. So, to process the inbound ones, you should simply create a handler within apex class. Then, attach it to an email service and an email address, and you are good to go!