+ Start a Discussion
Wiz76Wiz76 

Unsubscribe Email Service - Need notification when From Address cannot be found

Hi all..I'm hoping someone can help me here. I have downloaded the 'Unsubscribe Opt Out' package from the AppExchange and it works great except for one part. I require an email notification to be sent to me if the email address the message is from is not found in Salesforce.

 

The clients my reps deal with have a high turnover rate so a mass mailing may be sent to an email of someone who has left that is just being forwarded to someone elses account. When they send the unsubscribe email back, that email address will not be in our system and we need to be notified of that.

 

Is there an easy way to implement this logic into the code pasted below? All I would require in the email message would be a simple message ' env.FromAddress can not be found within Salesforce.com'.

 

Thank you in advance!

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 Apex Email Service
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
 
// 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 I can match on lower case
String mySubject = email.subject.toLowerCase();
// String I am searching for in the subject line
String s = 'unsubscribe';
 
// Check variable to see if the word "unsubscribe" was found in the subject line 
Boolean unsubMe;
// Look for the unsubcribe word in the subject line, 
// if it is found return true, otherwise false is returned
unsubMe = mySubject.contains(s);
 
 // If unsubscribe is found in the subject line enter the if statement
 
 if (unsubMe == true) {
    
    try {
        
    // Lookup 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 contacts into the List   
                            c.hasOptedOutOfEmail = true;
                            lc.add(c);                                 
    }    
        // update all the Contact records
        
        update lc;
            }
    catch (System.QueryException e) {
        System.debug('Contact Query Issue: ' + e);
        }   

    try {
        // Lookup 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 will confirm it is complete and no bounced email 
// should be send the sender of the unsubscribe request. 
result.success = true;
return result;
    }   
    
    // Test method to ensure you have enough code coverage
    // Have created two methods, one that does the testing
    // with a valid "unsubcribe" in the subject line
    // and one the does not contain "unsubscribe" in the
    // subject line
    
static testMethod void testUnsubscribe() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail() ;
   Messaging.InboundEnvelope env    = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method        
   Lead l = new lead(firstName='Rasmus', 
            lastName='Mencke',
            Company='Salesforce', 
            Email='rmencke@salesforce.com', 
            HasOptedOutOfEmail=false);
   insert l;

// Create a new test Contact and insert it in the Test Method  
   Contact c = new Contact(firstName='Rasmus', 
                lastName='Mencke', 
                Email='rmencke@salesforce.com', 
                HasOptedOutOfEmail=false);
   insert c;
   
   // test with subject that matches the unsubscribe statement
   email.subject = 'test unsubscribe test';
   env.fromAddress = 'rmencke@salesforce.com';
   
   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );
                        
   }
 
static testMethod void testUnsubscribe2() {

// Create a new email and envelope object
   Messaging.InboundEmail email = new Messaging.InboundEmail();
   Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

// Create a new test Lead and insert it in the Test Method        
   Lead l = new lead(firstName='Rasmus', 
            lastName='Mencke',
            Company='Salesforce', 
            Email='rmencke@salesforce.com', 
            HasOptedOutOfEmail=false);
   insert l;

// Create a new test Contact and insert it in the Test Method    
   Contact c = new Contact(firstName='Rasmus', 
                lastName='Mencke', 
                Email='rmencke@salesforce.com', 
                HasOptedOutOfEmail=false);
   insert c;
   
   // Test with a subject that does Not contain unsubscribe
   email.subject = 'test';
   env.fromAddress = 'rmencke@salesforce.com';

   // call the class and test it with the data in the testMethod
   unsubscribe unsubscribeObj = new unsubscribe();
   unsubscribeObj.handleInboundEmail(email, env );                      
   }    
   
}

 

Best Answer chosen by Admin (Salesforce Developers) 
harsha__charsha__c

Yes, this can be achieved.

 

When you are able to get the from email address, you can check the same email with existing contacts or leads. if no record is found then you can set the body with this email address else you can skip this.

 

All Answers

harsha__charsha__c

Hi,

 

If you only need to get the fromAddress in the email service, 

 

it can be taken from email parameter : email.fromAddress

Wiz76Wiz76

Thanks Harsha, but what I am trying to get done is to have that from address put in the body of an email and sent to me if it is not found in any contact or lead record.

harsha__charsha__c

Yes, this can be achieved.

 

When you are able to get the from email address, you can check the same email with existing contacts or leads. if no record is found then you can set the body with this email address else you can skip this.

 

This was selected as the best answer
Wiz76Wiz76

Just in case anyone else every runs into this requirement. What I did was before the else statement I enterd these criteria and it works like a charm.

 If(lc.size()==0 && ll.size()==0){
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddress = new String[] {myemail@domain.com'};
        mail.setToAddresses(toAddress); 
        mail.setSubject('Email Could Not Be Found'); 
        mail.setPlainTextBody(x+' could not be found in the database. Please alert the appropriate users'); 
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); 

 Thanks Harsha!

harsha__charsha__c

You are welcome!!!