Newer Version Available

This content describes an older version of this product. View Latest

Using the InboundEmail Object

For every email the Apex email service domain receives, Salesforce creates a separate InboundEmail object that contains the contents and attachments of that email. You can use Apex classes that implement the Messaging.InboundEmailHandler interface to handle an inbound email message. Using the handleInboundEmail method in that class, you can access an InboundEmail object to retrieve the contents, headers, and attachments of inbound email messages, as well as perform many functions.

Example 1: Create Tasks for Contacts

The following is an example of how you can look up a contact based on the inbound email address and create a new task.

1global class CreateTaskEmailExample implements Messaging.InboundEmailHandler {
2 
3  global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, 
4                                                       Messaging.InboundEnvelope env){
5 
6    // Create an InboundEmailResult object for returning the result of the 
7    // Apex Email Service
8    Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
9  
10    String myPlainText= '';
11    
12    // Add the email plain text into the local variable 
13    myPlainText = email.plainTextBody;
14   
15    // New Task object to be created
16    Task[] newTask = new Task[0];
17   
18    // Try to look up any contacts based on the email from address
19    // If there is more than one contact with the same email address,
20    // an exception will be thrown and the catch statement will be called.
21    try {
22      Contact vCon = [SELECT Id, Name, Email
23        FROM Contact
24        WHERE Email = :email.fromAddress
25        LIMIT 1];
26      
27      // Add a new Task to the contact record we just found above.
28      newTask.add(new Task(Description =  myPlainText,
29           Priority = 'Normal',
30           Status = 'Inbound Email',
31           Subject = email.subject,
32           IsReminderSet = true,
33           ReminderDateTime = System.now()+1,
34           WhoId =  vCon.Id));
35     
36     // Insert the new Task 
37     insert newTask;    
38     
39     System.debug('New Task Object: ' + newTask );   
40    }
41    // If an exception occurs when the query accesses 
42    // the contact record, a QueryException is called.
43    // The exception is written to the Apex debug log.
44   catch (QueryException e) {
45       System.debug('Query Issue: ' + e);
46   }
47   
48   // Set the result to true. No need to send an email back to the user 
49   // with an error message
50   result.success = true;
51   
52   // Return the result for the Apex Email Service
53   return result;
54  }
55}

Example 2: Handle Unsubscribe Email

Companies that send marketing email to their customers and prospects need to provide a way to let the recipients unsubscribe. The following is an example of how an email service can process unsubscribe requests. The code searches the subject line of inbound email for the word “unsubscribe.” If the word is found, the code finds all contacts and leads that match the From email address and sets the Email Opt Out field (HasOptedOutOfEmail) to True.

1Global class unsubscribe implements Messaging.inboundEmailHandler{
2
3    Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
4                         Messaging.InboundEnvelope env ) {
5    
6        // Create an inboundEmailResult object for returning 
7        // the result of the email service.
8        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
9         
10        // Create contact and lead lists to hold all the updated records.
11        List<Contact> lc = new List <contact>();
12        List<Lead> ll = new List <lead>();
13         
14        // Convert the subject line to lower case so the program can match on lower case.
15        String mySubject = email.subject.toLowerCase();
16        // The search string used in the subject line.
17        String s = 'unsubscribe';
18         
19        // Check the variable to see if the word "unsubscribe" was found in the subject line. 
20        Boolean unsubMe;
21        // Look for the word "unsubcribe" in the subject line. 
22        // If it is found, return true; otherwise, return false.
23        unsubMe = mySubject.contains(s);
24         
25         // If unsubscribe is found in the subject line, enter the IF statement.
26         
27        if (unsubMe == true) {
28            
29            try {
30               
31            // Look up all contacts with a matching email address.
32               
33            for (Contact c : [SELECT Id, Name, Email, HasOptedOutOfEmail
34                          FROM Contact
35                          WHERE Email = :env.fromAddress
36                          AND hasOptedOutOfEmail = false
37                          LIMIT 100]) {
38                          
39                // Add all the matching contacts into the list.   
40                c.hasOptedOutOfEmail = true;
41                lc.add(c);
42            }
43            // Update all of the contact records.
44            update lc;
45        }
46        catch (System.QueryException e) {
47            System.debug('Contact Query Issue: ' + e);
48        }   
49        
50        try {
51            // Look up all leads matching the email address.
52            for (Lead l : [SELECT Id, Name, Email, HasOptedOutOfEmail
53                     FROM Lead
54                     WHERE Email = :env.fromAddress
55                     AND isConverted = false
56                     AND hasOptedOutOfEmail = false
57                     LIMIT 100]) {
58                // Add all the leads to the list.       
59                l.hasOptedOutOfEmail = true;
60                ll.add(l);
61                   
62                System.debug('Lead Object: ' + l);   
63            } 
64            // Update all lead records in the query.
65            update ll;
66        }
67        
68        catch (System.QueryException e) {
69            System.debug('Lead Query Issue: ' + e);
70        }   
71        
72        System.debug('Found the unsubscribe word in the subject line.');
73         } 
74         else {
75            System.debug('No Unsuscribe word found in the subject line.' );
76         }
77        // Return True and exit.
78        // True confirms program is complete and no emails 
79        // should be sent to the sender of the unsubscribe request. 
80        result.success = true;
81        return result;
82    }   
83}
1@isTest
2private class unsubscribeTest {
3    // The following test methods provide adequate code coverage 
4    // for the unsubscribe email class.
5    // There are two methods, one that does the testing
6    // with a valid "unsubcribe" in the subject line
7    // and one the does not contain "unsubscribe" in the
8    // subject line.        
9    static testMethod void testUnsubscribe() {
10    
11       // Create a new email and envelope object.
12       Messaging.InboundEmail email = new Messaging.InboundEmail() ;
13       Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();
14    
15       // Create a new test lead and insert it in the test method.
16       Lead l = new lead(firstName='John', 
17                lastName='Smith',
18                Company='Salesforce', 
19                Email='user@acme.com', 
20                HasOptedOutOfEmail=false);
21       insert l;
22    
23       // Create a new test contact and insert it in the test method.
24       Contact c = new Contact(firstName='john', 
25                    lastName='smith', 
26                    Email='user@acme.com', 
27                    HasOptedOutOfEmail=false);
28       insert c;
29       
30       // Test with the subject that matches the unsubscribe statement.
31       email.subject = 'test unsubscribe test';
32       env.fromAddress = 'user@acme.com';
33       
34       // Call the class and test it with the data in the testMethod.
35       unsubscribe unsubscribeObj = new unsubscribe();
36       unsubscribeObj.handleInboundEmail(email, env );
37                            
38    }
39     
40    static testMethod void testUnsubscribe2() {
41    
42       // Create a new email and envelope object.
43       Messaging.InboundEmail email = new Messaging.InboundEmail();
44       Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
45    
46       // Create a new test lead and insert it in the test method.        
47       Lead l = new lead(firstName='john', 
48                lastName='smith',
49                Company='Salesforce', 
50                Email='user@acme.com', 
51                HasOptedOutOfEmail=false);
52       insert l;
53    
54       // Create a new test contact and insert it in the test method.    
55       Contact c = new Contact(firstName='john', 
56                    lastName='smith', 
57                    Email='user@acme.com', 
58                    HasOptedOutOfEmail=false);
59       insert c;
60       
61       // Test with a subject that does not contain "unsubscribe."
62       email.subject = 'test';
63       env.fromAddress = 'user@acme.com';
64    
65       // Call the class and test it with the data in the test method.
66       unsubscribe unsubscribeObj = new unsubscribe();
67       unsubscribeObj.handleInboundEmail(email, env );                      
68    }     
69}