Implementing Email To Lead Functionality In Simple Way

Greeting Reader smiley ,

Like mentioned in title, I would be posting about configuting Email to Lead service in Salesforce. 

There are number of tools in AppExchange which can do this, but why use apps when we can do this ourselfenlightened

For the people, who do not know what is Email To Lead Service, below is the quick description to get started.

Email To Lead :

Email To Lead is a standard email based service provided by Salesforce that allows users to create Leads by sending an email to a dedicated email address.

So What's Benefit ?

This benefits Sales people who mainly work on gathering Leads for their Organization. In normal cases, the user needs to log in to Salesforce and from there he/she needs to create a Lead. This could be little time consuming for busy people. This can be overcome with Email to Lead Functionality. All they have to do is send an email to dedicated address and Salesforce will take care of creating New Lead. (when I meant Salesforce in here, I actually mean people like Us, who coded for that logic cool).

In a simpler terms, send an email to create lead data in Salesforce without log in.

Now people who do not know what is a Lead, they probably need to refer some other sources. I am not covering that part, atleast not in this post.

How To Achieve :

I will break down the process in simpler steps:

  1. Creating an Apex Class (Pretty Simple One laugh)
  2. Configuring Email Services in Salesforce.

1. Creating an Apex Class:

Wondering why an Apex class is needed?!. This is where the actual logic of  inserting a new Lead data in the Salesforce will take place.

This Email to Lead Handler Class is different from other class in below mentioned terms:

  • Email to Lead Handler must be always in global context. This enables the class to be available globally.
  • Email to Lead Handler class must implement an interface called as Messaging.InboundEmailHandler.

          Messaging.InboundEmailHandler : This interface allows the programmer to process the incoming email service. When you implement this interface, you must define a interface method inside the class. This method is handleInboundEmail() method. The general Syntax is below:

 global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env) {}

Should have a Return Type of InboundEmailResult, and should take 2 arguments as described.

I have posted my sample class for your reference:

global class Email2Lead implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env) {
        //Do not worry about this. result returned only for Salesforce Email Service process

        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        //Instantiate New Lead Object
        Lead newLead = new Lead();
        //add details = email.fromAddress;
        newLead.LastName = 'Email2Lead'; = 'Email2Lead';
        //insert new lead
        try {
            //Insert New Lead
        catch(Exception e) {
            System.debug('Exception in new Email2Lead'+e);

        return result;
That's all buddy Coding part completed. Now let's configure.

2. Configuring Email Services in Salesforce:

  1. Go to Set up -> Develop -> Email Services.
  2. Click on New Email Service button.
  3. Give a Service Name.
  4. Select the Apex Class we created in above step.
  5. Check Active to TRUE and click on Save.

Once You are done It will look like below image:

Now, Click on New Email Address which will be acting as To Address where we need to send an email.

  1. Enter Email Address.
  2. Select a Context User preferably System admin and Click Save.

It will look like below:


There you go we are done !! The dedicated email address will be generated. Use this to send email as shown below:

This will create a lead in Salesforce like below:


Summary - What we did:

                              Email Address (lengthy one mentioned above ) -> Email Service -> Our Apex Class.

The incoming email will invoke the relevant email service which in turn executes our class. This class will insert the data.



Here I will highlight some points you can try:

1. User your Email To Lead Handler Apex class to process all business logic. In my example I just inserted some text string. you can process the incoming email to fetch actual information to be added to the lead record. (Give a try geek ).

2. Leave Accept Email From blank to accept email from all domain. If you want to restrict only from gmail or yahoo mention domains like etc; this will allow only gmail domain mails to create lead.

3. Like Lead we can create Cases also. So stay tuned for upcoming posts.

Thanks for reading. Hope it will be useful for you.

Any doubts or comments let me know in the below comments sectionmail. Thank you for all your support and heart.

Average: 3.8 (6 votes)