この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

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

InboundEmail オブジェクトの使用

Apex メールサービスドメインが受信するすべてのメールについて、Salesforce は、そのメールの内容と添付ファイルを含む個別の InboundEmail オブジェクトを作成します。Messaging.InboundEmailHandler インターフェースを実装する Apex クラスを使用して、受信メールメッセージを処理できます。そのクラスで handleInboundEmail メソッドを使用して、InboundEmail オブジェクトにアクセスし、受信メールメッセージの内容、ヘッダー、および添付ファイルの取得と、その他多数の機能を実行することができます。

例 1: 取引先責任者の ToDo の作成

受信メールアドレスに基づいて取引先責任者を検索し、新規 ToDo を作成する方法の例は次のとおりです。

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}

例 2: メールの登録解除

顧客や見込み客にマーケティングメールを送信する会社は、受信者が登録解除するための方法を用意する必要があります。次の例で、メールサービスで登録解除要求を処理する方法を説明します。このコードは、受信メールの件名の「登録解除」という単語を検索します。単語が見つかると、差出人メールアドレスに一致するすべての取引先責任者とリードを検索し、[メール送信除外] 項目 (HasOptedOutOfEmail) を 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}