Salesforce | Trap incoming email and extract email content with attachments in salesforce

Author - Kiran Preet

Problem: Trap incoming email and extract email content with attachments in Salesforce
Solution: Salesforce provides us a mechanism to trap incoming emails on a specific email id and extra email contents including attachments out of it. To achieve this we need to follow these steps:

1. Create an Apex class that will implement Messaging.InboundEmailHandler for capturing the email content.

In the following apex code, we have created an account and used email subject content as an Account name. Here we also save the attachments sent in the email:

global class CaptureEmailInbound implements Messaging.InboundEmailHandler {
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope env) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        String myPlainText = email.plainTextBody; //this is  email body content that you can save where needed
        String defaultAccount = 'Default Account';
        String accountId;
        try {
        List acclist = [Select id, name from Account where name like: email.subject];
        //find an account id first that has account name matching the subject of email	
        if (acclist.size() & gt; 0) {
        for (Account a: acclist) {
        accountId = a.Id;
        break;}
        } 
        else {
        Account account = new Account();
        account.name = email.subject;
        insert account;
        accountId = account.Id;
            }
        // Save attachments, if any (both text and binary)
        for (Messaging.Inboundemail.TextAttachment tAttachment: email.textAttachments) {
        Attachment attachment = new Attachment();
        attachment.Name = tAttachment.fileName;
        attachment.Body = Blob.valueOf(tAttachment.body);
        attachment.ParentId = accountId;
        insert attachment;
         }
        for (Messaging.Inboundemail.BinaryAttachment bAttachment: email.binaryAttachments) {
        Attachment attachment = new Attachment();
        attachment.Name = bAttachment.fileName;
        attachment.Body = bAttachment.body;
        attachment.ParentId = accountId;
        insert attachment;
           }
        } catch (Exception e) {
          result.success = false;
          result.message = 'Oops, I failed.' + e + myPlainText;
        }
        return result;
    }
}

2. Go to setup.

3. Write “Email Services” in the quick find box and open it.

4. Click on “New Email Service” to create new service:

1301

4.1. Write a name in Email Service name.
4.2. Select the CaptureEmailInbound apex class field which we built previously in Apex class.
4.3. As we are also capturing attachments from email so select All option in Accept Attachment field.
4.4. Check Active checkbox.

5. Click save and new Email address:

1302

5.1. Email address contains the local-part of the email address. Salesforce.com assigns the domain name part of the address. Context user will take the value of user logged in value by default.
5.2. Write email addresses that are authorized to send email in Accept Email from Field.

6. Click Save.

7. Salesforce provides you an email address. Take that email address and send email to this email address and the class will run.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *