function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sascha DeinertSascha Deinert 

Unsubscribe InboundEmail

Hi,

I test the code from salesforce to unsubscribe via mail, below you will find the original code from salesforce. This works fine, but what is happen if the unsubscriber send an email to salesforce without the subject string "unsubscribe". Can I see this mails somewhere?
 
Global class unsubscribe implements Messaging.inboundEmailHandler{

    Global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                         Messaging.InboundEnvelope env ) {
    
        // Create an inboundEmailResult object for returning 
        // the result of the email service.
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
         
        // Create contact and lead lists to hold all the updated records.
        List<Contact> lc = new List <contact>();
        List<Lead> ll = new List <lead>();
         
        // Convert the subject line to lower case so the program can match on lower case.
        String mySubject = email.subject.toLowerCase();
        // The search string used in the subject line.
        String s = 'unsubscribe';
         
        // Check the variable to see if the word "unsubscribe" was found in the subject line. 
        Boolean unsubMe;
        // Look for the word "unsubcribe" in the subject line. 
        // If it is found, return true; otherwise, return false.
        unsubMe = mySubject.contains(s);
         
         // If unsubscribe is found in the subject line, enter the IF statement.
         
        if (unsubMe == true) {
            
            try {
               
            // Look up all contacts with a matching email address.
               
            for (Contact c : [SELECT Id, Name, Email, HasOptedOutOfEmail
                          FROM Contact
                          WHERE Email = :env.fromAddress
                          AND hasOptedOutOfEmail = false
                          LIMIT 100]) {
                          
                // Add all the matching contacts into the list.   
                c.hasOptedOutOfEmail = true;
                lc.add(c);
            }
            // Update all of the contact records.
            update lc;
        }
        catch (System.QueryException e) {
            System.debug('Contact Query Issue: ' + e);
        }   
        
        try {
            // Look up all leads matching the email address.
            for (Lead l : [SELECT Id, Name, Email, HasOptedOutOfEmail
                     FROM Lead
                     WHERE Email = :env.fromAddress
                     AND isConverted = false
                     AND hasOptedOutOfEmail = false
                     LIMIT 100]) {
                // Add all the leads to the list.       
                l.hasOptedOutOfEmail = true;
                ll.add(l);
                   
                System.debug('Lead Object: ' + l);   
            } 
            // Update all lead records in the query.
            update ll;
        }
        
        catch (System.QueryException e) {
            System.debug('Lead Query Issue: ' + e);
        }   
        
        System.debug('Found the unsubscribe word in the subject line.');
         } 
         else {
            System.debug('No Unsuscribe word found in the subject line.' );
         }
        // Return True and exit.
        // True confirms program is complete and no emails 
        // should be sent to the sender of the unsubscribe request. 
        result.success = true;
        return result;
    }   
}

thanks,
Sascha
Glyn Anderson 3Glyn Anderson 3
Sascha,

As written, this code does nothing in response to emails that don't contain "unsubscribe" in the subject line; and none of the inbound emails are retained.  You could add code at line 75 that could create a SingleEmailMessage from the fields in the InboundEmail, and send it to a different email address (i.e. forward it).  Or you could write code that creates an EmailMessage record and attaches it to a Case (or with Enhanced Email, you could attach it to a custom object).  Or you could convert it to an Attachment, a Chatter FeedItem, a Custom Object, etc.

Would any of these ideas work for you?  Do you need help implementing one of them?
Glyn Anderson 3Glyn Anderson 3
Sascha,  Have you found a solution to this?  Do you need any more help?
Sascha DeinertSascha Deinert
Not yet, do you have some more information for me?
Glyn Anderson 3Glyn Anderson 3
Hey Sascha,  I gave you three different ideas with variations and offered to help.  You have been silent.  And you expect me to have more information for you!  Are you expecting someone else to do all your work?  I can help you, but you have to be trying to solve the problem.  Do you want to implement any of the ideas I suggested?