[SalesForce] How to leverage org-wide email address within an email service

I'm leveraging 'Send an Email' button for a custom object(comm__c) which has a lookup to the case object. While sending the email the user sets the 'From email address' to a specific Organization-Wide Email Address.

I'm creating a comm__c record every time an outbound email is sent by leveraging EmailMessage object, now my requirement is to create a comm__c record for each inbound email replied to the Organization-Wide Email Address.

If I write a class implementing InboundEmailHandler interface and setup an Email Service the setup process doesn't ask me to specify the Organization-Wide Email Address in any of the steps. Please advise.

Best Answer

2 options:

Option 1

1) Create a class which implements the inboundHandler.

global class VerifyOrgWideEmailAddress implements Messaging.InboundEmailHandler {

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

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


        //This is one time thing that you will have to perfrom everytime you setup the org wide email address.
        //Use this html body to fetch the verification link and open it in a browser to verify it. 
        System.debug('The email Body ==> '+ email.plainTextBody);

        result.success = true;
        return result;
    }
}

2) Create a email service and link the above class that you just created. 3) Create a new email address under the Email Services.

4) Copy the email address that you just created and use it to create the Org Wide Email address.

5) Open the developer console and wait for a log with the Operation , 'EmailToApexHandler'. Open the log and search for the verification Link keyword.

enter image description here

6) Copy-paste the verification link in browser and that should be it.

7) Now, you would need to update the email services class to handle your custom logic.

Option 2.

1) Say you have setup an org email address which is pointing to support@gmail.com.

2) Create an email service.

3) You will have to setup an email forwarding in the email server (for this example the email forwarding will be setup in gmail) to forward all the email to the email services email that you have created.

Related Topic