Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (5) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Retrieving Information from Incoming Email Messages

Retrieving Information from Incoming Email Messages

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

Problem

You want to retrieve the following information from a job applicant's incoming email:
  • Email address
  • First and last name
  • Phone number and street address

Solution

  1. Create an Apex class (ProcessApplicant) that implements the InboundEmailHandler interface. The class defines the handleInboundEmail method to work with the created:
    • InboundEmail, which contains the incoming email's content, and attachments
    • InboundEnvelope, which contains the envelope to and from addresses
    For example:
    global class processApplicant implements 
    		Messaging.InboundEmailHandler {
           
     global Messaging.InboundEmailResult handleInboundEmail(
      Messaging.InboundEmail email, 
      Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = 
    		new Messaging.InboundEmailresult();
    
    return result;
        }   
    }
    
  2. Parse through the incoming email message and look for the required information. For example:
    // Captures the sender's email address
    String emailAddress = envelope.fromAddress;
     
      // Retrieves the sender's first and last names
    String fName = email.fromname.substring(
    		0,email.fromname.indexOf(' '));
    String lName = email.fromname.substring(
    		email.fromname.indexOf(' '));
     
      // Retrieves content from the email. 
      // Splits each line by the terminating newline character
      // and looks for the position of the phone number and city
    String[] emailBody = email.plainTextBody.split('\n', 0);
    String phoneNumber = emailBody[0].substring(6);
    String city = emailBody[1].substring(5);
    
  3. Create and activate an email service to process the contents of the ProcessApplicantApex class.

    Salesforce.com generates an email address unique to this email service. Emails sent to this inbound email address run the ProcessApplicant class:

    1. Click Setup | Develop | Email Services.
    2. Click New Email Service.
    3. Enter jobApplication for the Email Service Name.
    4. Enter ProcessApplicant for the Apex Class.
    5. Click Save and New Email Address.
    6. Since the Email Address and Context User fields have been automatically populated, click Save.

Discussion

  • When creating email services, consider security and truncation settings. See “Defining Email Services” in the Salesforce.com online help.
  • Email content format varies—ensure that your code can handle different messages by parsing through the entire body of the email.
  • For a complete example containing code from all the email recipes, see Email Recipes—Complete Code Example.

Share

Recipe Activity - Please Log in to write a comment

good

voted as verified by sailappa nayagam  (2013-09-24)

I must be missing something very badly because every where I go in the cook book area has code snippets duplicated as above!!!!  The 1 and 2 points have duplicate code!!! What am I missing.  Please help.

by inacloud  (2010-10-29)

Hi, I would like to inquiry is it possible to retrieve message in the email and insert  to "Notes & Attachment" in the custom object. Thanks.

by MANLI YANG  (2010-10-28)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.