Thursday, February 25, 2016

Extract Signature image and text from received email


Example suppose you get received an Email From Some one
Hi cloudy,
                 Nice to meet you.

--

Thanks & Regards
Salesforce
9876543210
Salesforce Developer

Bold Letter text is signature

global class ProcessContactApplicantEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
     
        Contact contact = new Contact();
        contact.FirstName = email.fromname.substring(0,email.fromname.indexOf(' '));
        contact.LastName = email.fromname.substring(email.fromname.indexOf(' '));
        contact.Email = envelope.fromAddress;
        contact.from__c = email.subject;
 
        string bodystr = email.htmlBody;
        string bodymsg = bodystr.substringBeforeLast('--');
        contact.MainBodyEmail__c = bodymsg;
        string signature = bodystr.substringAfterLast('--');
        contact.MainBodyEmailArea__c = signature;
        //System.debug('++++ '+contact.Signature__c);
        insert contact;
   
        System.debug('====> Created contact==> '+contact.Id);
     
        if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
          for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
            Attachment attachment = new Attachment();
            // attach to the newly created contact record
            attachment.ParentId = contact.Id;
            attachment.Name = email.binaryAttachments[i].filename;
            attachment.Body = email.binaryAttachments[i].body;
            insert attachment;
            system.debug('---> '+attachment.name+' @@@@ '+attachment.body);
          }
    }
       return result;
    }
 
 
 
}

No comments:

Post a Comment