Processing Emails in Salesforce through Inbound Email Service

The most common condition met while working on an integration project is that the third party either does not have API’s or is unable to provide CSV file to transfer the data. At times like these you can simply ask a third party to send an email and then the email’s header, content and/or attachments can be in Salesforce through Email Service.

This email sent by a third party to Salesforce is termed as Inbound Email. Inbound email is received by Salesforce Email Service and email content and/or attachments are processed by apex class.

Now the question arises ‘What is Salesforce Email Service?’ and ‘How email is processed by apex class in Salesforce Email Service?’.

Email Services

Apex classes are invoked by self-regulating processes known as Email Services. They process the contents, headers, and attachments of inbound email. Moreover, the email addresses used by third parties for sending inbound email are salesforce-generated and there is no rule to associate each email service with only one salesforce generated email address.

Salesforce functionality to receive and parse emails and take actions through Apex

Salesforce provides an out of box interface which has various classes that help to process email and attachment and can return the result. And we will be implementing the Messaging. InboxEmailHandler interface in our example. With the help of classes within this interface we will capture the third party email and process it. We will be shortly explaining these things with the help of an example.

Points to Note while writing apex class

  • 1) Apex class should be declared global because email is sent by an outside third party.
  • 2) A global method is used inside the global apex class to process the incoming email.

Here are some basic classes used by apex class in Salesforce email services:

Messaging.InboundEmailResult

The return type of global handleInboundEmail method is InboundEmailResult. By default, InboundEmailResult object returns null if there is no error in the result of the email service and it is assumed to be successful. The properties for InboundEmailResult are: message and success. On the basis of message and success, two conditions arise:

  • 1) If result equals success, then InboundEmailResult class will return the result for the Apex Email Service.
  • 2) If result.success is false, then Salesforce rejects the inbound email and sends a reply to the original sender via email containing the message specified in the Message field.

Messaging.InboundEmail

Inbound Email class contains properties for incoming emails from third parties. The mapping processes that use apex classes to process email content, header and attachments, maps the processed email to standard fields. Some of the commonly used properties of Inbound Email are as follows:

  • 1) InboundEmail.binaryAttachments Image, audio, video and attachment files are supported in binary attachments. BLOB (short for Binary Large Objects) datatype is used to store the body of the attachment into a variable in Salesforce. We can use body, fileName, headers and mimeTypeSubType binary attachment properties to process and map the attachment.
  • 2) InboundEmail.textAttachment Files with extensions .vcf or .vcs and all MIME types of text and applications are supported in text attachments. The body of the attachment is stored as a string and input to the apex method can be re-encoded as UTF-8. The body, bodyIsTruncated, charset, fileName and mimeTypeSubType of text attachment properties can be used to process and map the attachment.

Messaging.InboundEnvelope

Email Envelope is communication between SMTP Client and Server which is not visible to the receiver of incoming emails. If the server at the end of communication does not respond with 250 OK, then actual email will never be sent. So, InboundEnvelope keeps a record of all the envelope information.

  • 1) InboundEnvelope.fromAddress- The ‘From’ field of the incoming email is stored as string in fromAddress of InboundEnvelope.
  • 2) InboundEnvelope.toAddress- The ‘To’ field of the incoming email is stored as string in toAddress of InboundEnvelope.

Now let us discuss the flow of execution in Email Services by Salesforce, as we have got the basic understanding of apex class and Email Service.

How email services work in salesforce?

  • Step: 1 An email is sent to salesforce-generated email address i.e. Salesforce email server.
  • Step: 2 Apex class is invoked by Salesforce email server for the processing of email.

Create email service in salesforce with simple Step by Step example

Finally, it’s time to demonstrate everything we have explained above with the help of an example. In this example, we will be defining our own Email Service that would process the incoming email and create leads in salesforce. The flow of creating email service is different from its flow of execution:

email-process

Step: 1 Create an apex class named ProcessLeadByEmailService.

We will implement the Messaging.InboundEmailHandler interface and create handleInboundEmail method with Messaging.InboundEmailResult return type in our apex class. Then, we will create an instance of Messaging.InboundEmailResult() to return the result to email service. After creating an instance of standard object Lead, map FirstName, LastName, Company, Email or any other standard or custom field of Lead with processed information received from incoming email.

Here, we are also inserting attachment in leads. For that after checking if the incoming email contains attachments, we will iterate through the attachments one by one to insert attachment into lead. Create an instance of standard salesforce attachment and map standard fields of attachment with filename and body of attachment. As we are inserting attachment in Lead, we will assign the Id of lead created as ParentId of attachment. At last we will return the result to the Email Service.

codes

Step: 2 Click Setup >> Search for ‘Email Services’ in Quick Find Box >> Click on ‘New Email Service’ Button.

Fill in all the fields as per your requirement and select the apex class you just created. Enable the Active checkbox.

email_service_img1

email_service_img2

Note: Accept Email From is blank in the given screenshot because this way emails can be received by email service from any email address or domain. We can list the email addresses and domains separated by commas from which we want this email service to receive emails. For example: demo@companyname.com, gmail.com

Click ‘Save and New Email Address’.

Step: 3 Fill Email Address Name and other details in Email Address Information as shown:

email_service_img3

Note: The Domain Name in ‘Accept Email from’ must qualify the Domain Name entered at Step: 2.

Click ‘Save’.

Step: 4 Copy the Salesforce generated Email Address as shown:

email_service_img4

Step: 5 Send Email to salesforce generated email address with an attachment as shown:

email_service_img5

Now you will see that the Lead is created along with an attachment.



email_service_img6

Note: If you are unable to see the attachment then edit Page Layout and add Notes and Attachment from related lists as shown:

email_service_img5

CONCLUSION

We just saw how easy and helpful it is to process the incoming email. The Salesforce Email Service is simple, easy to use and a powerful automation process that can be useful in several scenarios. Either it is integrating Salesforce with another platform or receiving emails and processing them to create data in one or multiple objects, one can always rely on Salesforce. But there are some governor limits on the processing of Email Service Messages, exceeding which will cause the message to be bounced, discarded, or queued for processing the next day. The formula for calculating Daily Limit =Number of user licenses * 1000, whereas Maximum Daily Limit is 1,000,000. So, if you have 10 licenses your daily limit will be 10000.

There are a number of resources available that can be referred to build a good understanding of Email Services. Some of them are listed below:

For any query on Email Services in Salesforce, contact support@astreait.com