Activity Forums Salesforce® Discussions What are the custom email handler in salesforce?

  • Saurabh

    Member
    March 23, 2017 at 2:38 pm

    Hi,

    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.

    Custom Email handler is a feature of salesforce.com where, the user can assign an Apex Class that implements the Messaging.InboundEmailHandler interface to a configuration which allows you to process the email contents, headers and attachments. Using this Information, you can cater a variety of requirements . Listing few, create a new contact if one does not exists with that email address, receive job applications and attached the person’s resume to their record.

    Email services are basically an option to a developer to develop a functionality , where an email can be received at a particular address and some operations can happen on the data retrieved from the email. These operations are done by the Apex Code, thus giving us a spectrum of options that fulfils requirements.

    # general template to create the apex class for the email services is:

    global class myHandler implements Messaging.InboundEmailHandler {
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
    return result;
    }
    }

    #Apex Code with test method:

    global class CreateContactFrmEmail implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {

    Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

    String subToCompare = 'Create Contact';

    if(email.subject.equalsIgnoreCase(subToCompare))
    {
    Contact c = new Contact();
    c.LastName = email.plainTextBody;
    insert c;

    // Save attachments, if any
    for (Messaging.Inboundemail.TextAttachment tAttachment : email.textAttachments) {
    Attachment attachment = new Attachment();

    attachment.Name = tAttachment.fileName;
    attachment.Body = Blob.valueOf(tAttachment.body);
    attachment.ParentId = c.Id;
    insert attachment;
    }

    //Save any Binary Attachment
    for (Messaging.Inboundemail.BinaryAttachment bAttachment : email.binaryAttachments) {
    Attachment attachment = new Attachment();

    attachment.Name = bAttachment.fileName;
    attachment.Body = bAttachment.body;
    attachment.ParentId = c.Id;
    insert attachment;
    }
    }

    result.success = true;
    return result;
    }

    static testMethod void testCreateContactFrmEmail() {
    Messaging.InboundEmail email = new Messaging.InboundEmail() ;
    Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();

    email.subject = 'Create Contact';
    email.plainTextBody = 'FromEmail';
    env.fromAddress = '[email protected]';

    CreateContactFrmEmail creatC = new CreateContactFrmEmail();
    creatC.handleInboundEmail(email, env );
    }
    }

    #After creating the above Apex class, click Your Name | Setup | Develop | Email Services.

    Click New Email Service to define a new email service.
    Select above apex class, add email address from where to accept the request and activate the service.

    Email-Service-Information (1)

     

    Email Address Information - Salesforce

    #After filling the form, click on “Save and New Email Address”

    Email-Address-Information-Salesforce (1)
    Note: The domain name in Email address must qualify the domain name entered at “email services” in first step. For example : we have entered “gmail.com” in first step that means it will accept the email address only from the gmail.com and that’s why in second step I have used email address displayed in screen shot.
    After all the above steps, one email address is generated by the salesforce. Send the email to that address with subject line “Create Contact” and contact name in email body.
    In the above tutorial, I have used very simple example just to demonstrate that how the email services works in the salesforce.

    Hope it may help:

    Thanks

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos