function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
UrvikUrvik 

Email Response Coming from Context User Email Address

I have created an email service to capture response from contacts/approvers for our custom approval process. When an approver responses to email, it is coming from my email address (Context User). I want the response to come from Approver's email address. Below is my code..Could someone please help?
----------------------------------------------------------------------------------------------------------------------------------------------
global class Capture_Approval_From_Email implements Messaging.InboundEmailHandler {

    global Messaging.InboundEmailResult handleInboundEmail(Messaging.inboundEmail email, Messaging.InboundEnvelope env)
    {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        Set<String> bccEmails = new Set<String>();
        String roleName = '';
      
       
        Approval_Process_Setting__c appSetting = Approval_Process_Setting__c.getInstance();
        if(appSetting != null)
            roleName = appSetting.Leadership_Role_Name__c;
           
        String recID = '';
       
        for(User usr : [SELECT Email FROM User WHERE UserRole.DeveloperName = :roleName and Get_Approval_Mail_To_Leadership__c= true ])
        {
            bccEmails.add(usr.Email);
        }       

        try
        {
            recID = email.subject.substring(email.subject.indexOf('<') + 1, email.subject.indexOf('>'));
           
        }
        catch (System.StringException e)
        {
             System.debug('No <> found in email: ' + e);
        }
       
        if(recID != null && recID != '')
        {
            try
            {
                Project_Governance_Form_Approval_Request__c govFormReq = [SELECT id,Project_Governance_Form__r.Project__r.ERDI_S_E_Lead__r.Email,Project_Governance_Form__r.Project__r.Transaction_Lead__r.Email FROM Project_Governance_Form_Approval_Request__c WHERE id = :recID LIMIT 1];
               
                if(govFormReq.Project_Governance_Form__r.Project__r.ERDI_S_E_Lead__r.Email != null)
                    bccEmails.add(govFormReq.Project_Governance_Form__r.Project__r.ERDI_S_E_Lead__r.Email);
               
                if(govFormReq.Project_Governance_Form__r.Project__r.Transaction_Lead__r.Email != null)
                    bccEmails.add(govFormReq.Project_Governance_Form__r.Project__r.Transaction_Lead__r.Email);               
               
                String emailText = '';
                String reply = '';
               
                Project_Governance_Form_Approval_Request__c req = new Project_Governance_Form_Approval_Request__c(id = recID);
                if(email.plainTextBody.length() >=32768)
                    emailText = email.plainTextBody.subString(0,32766);
                else emailText = email.plainTextBody;
               
                try
                {
                    reply = emailText.split('\n')[0].toLowerCase();
                }
                catch(Exception e)
                {
                   system.debug('***********Exception encountered***********'+e.getMessage());
                }
               
                if(reply.contains('approve') || reply.contains('approved') || reply.contains('yes')||
                reply.contains('Approve') || reply.contains('Approved') || reply.contains('Yes'))
                    req.Status__c = 'Approved';
               
                if(reply.contains('reject') || reply.contains('rejected') || reply.contains('no')||
                reply.contains('Reject') || reply.contains('Rejected') || reply.contains('No'))
                    req.Status__c = 'Rejected';
                    req.email__c = env.fromAddress;
                   
                req.Approver_Email_Text__c = emailText;
               
                update req;
            }
            catch(Exception e)
            {
                system.debug('***********Exception encountered***********'+e.getMessage());
            }
            if(!bccEmails.isEmpty())
            {   
                mail.setSaveAsActivity(false);
                mail.setHTMLBody(email.htmlBody); 
                mail.setToAddresses(new List<String>(bccEmails));
                mail.setSubject(email.subject);
               
                try
                {
                    Messaging.sendEmail(new Messaging.Email[] { mail });
                }
                catch(Exception e)
                {
                    system.debug('***********Exception encountered***********'+e.getMessage());
                }               
            }
        }
        return result;
    }
}

ShashForceShashForce

Hi Urvik,

I'm afraid there isn't an option to setup a "From" email address dynamically. However, we can setup an org-wide email address instead as the "From" address. Please see if these links help:
http://salesforce.stackexchange.com/questions/1243/setting-a-from-address-in-singleemailmessage
https://developer.salesforce.com/blogs/developer-relations/2009/08/practical-org-wide-address-usage.html

If this answers your question, please mark this as the Best Answer for this post, so that others can benefit from this post.

Thanks,
Shashank