Skip to content

Advanced Send Email Flow Action – Send email from Salesforce flow with CC, BCC, and email attachments

Last updated on July 3, 2024

The Apex class is designed to send emails from Salesforce flow with additional options such as CC, BCC, and email attachments, etc.

Here’s a summary of input variables that can be passed from flow to this apex class.

Request: Represents the data needed to send an email.

  • WhoId: Recipient’s Salesforce ID (User, Contact, or Lead).
  • WhatId: Record which fields are used as merge fields and the email activity is related (Account, Case, Opportunity, etc.).
  • toAddresses: Comma-separated list of recipient email addresses.
  • ccAddresses: Comma-separated list of CC email addresses.
  • bccAddresses: Comma-separated list of BCC email addresses.
  • OWAddress: Org-Wide Email Address to use as the sender.
  • ReplyTo: Email address for reply-to.
  • Subject: Email subject.
  • TextBody: Plain text body of the email.
  • HtmlBody: HTML body of the email.
  • ContentVersionIds: List of attachment IDs.
public without sharing class SendEmailInvocable {

    @InvocableMethod(
        label = 'Send Email by APEX'
        description = 'Sends a email to recipients without need of Email Alert. If set the Who ID or What ID then Activity is also created.'
    )
    public static void execute( List<Request> requests ) {
    
        for(Request request : requests){
            SendEmailViaApex(request.WhoId, request.WhatId, request.toAddresses, request.ccAddresses, request.bccAddresses, request.OWAddress, request.ReplyTo, request.Subject, request.TextBody, request.HtmlBody, request.ContentVersionIds);
        }
        
    }
    
    public static void SendEmailViaApex(String WhoId, string WhatId, String toAddress, String ccAddress, String bccAddress, String OWAddress, String ReplyTo, String Subject, String TextBody, String HtmlBody, list<String> ContentVersionIds){
        // Note, this is bound by Apex governor limits of no more than 10 emails can be sent per transaction.
        
        String CONTACT_KEY_PREFIX = Contact.sObjectType.getDescribe().getKeyPrefix();
        String LEAD_KEY_PREFIX = Lead.sObjectType.getDescribe().getKeyPrefix();
        
            list<OrgWideEmailAddress> OrgWideAddress = new list<OrgWideEmailAddress>([Select Id, Address FROM OrgWideEmailAddress WHERE Address = :OWAddress limit 1]);
            system.debug('OrgWideAddress::'+OrgWideAddress);
            
            if(OrgWideAddress.size() > 0){
                
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); 
                
                if(String.isNotBlank( WhoId ) ) {
                    String whoKeyPrefix = Id.ValueOf(WhoId).getSObjectType().getDescribe().getKeyPrefix();        
                    mail.setTargetObjectId( WhoId );
                    mail.setSaveAsActivity( ( whoKeyPrefix == CONTACT_KEY_PREFIX || whoKeyPrefix == LEAD_KEY_PREFIX ) );
                }
                
                //Record ID for which letter will be generated
                if ( String.isNotBlank( whatId ) ) {
                    mail.setWhatId( whatId );
                    if ( String.isBlank( mail.getTargetObjectId() ) ) {
                        mail.setSaveAsActivity( true );
                    }
                }
                
                mail.setOrgWideEmailAddressId(OrgWideAddress[0].Id);                
                mail.setSubject(Subject);
                if(String.isNotBlank(TextBody)){
                    mail.SetPlainTextBody(TextBody);
                }else{
                    mail.SetHtmlBody(HtmlBody);
                }
                
                mail.setInReplyTo(replyTo);
                
                List<String> toAddresses= toAddress.split(',');
                mail.setToAddresses(toAddresses);
                
                if(String.isNotBlank(ccAddress)){
                    List<String> ccAddresses= ccAddress.split(',');
                    mail.setCcAddresses(ccAddresses);
                }
                
                if(String.isNotBlank(bccAddress)){
                    List<String> bccAddresses= bccAddress.split(',');
                    mail.setCcAddresses(bccAddresses);
                }               
                
                if(ContentVersionIds?.size() > 0){
                    mail.setEntityAttachments(ContentVersionIds);
                }
                Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            }
    }

    // -----------------------------------------------------------------

    public class Request {

        @InvocableVariable(
            label = 'Who ID'
            description = 'Email recipient and who the email Activity will be related to (User, Contact, or Lead). At least one of Who ID, To, Cc, Bcc Addresses must be set.'
        )
        public String whoId;

        @InvocableVariable(
            label = 'What ID'
            description = 'What the email Activity will be related to (Account, Case, Opportunity, etc.). Cannot be set if Who Id is a User record.'
        )
        public String whatId;
        

        @InvocableVariable(
            label = 'To Addresses'
            description = 'Comma-delimited list of email addresses. At least one of Who ID, To, Cc, Bcc Addresses must be set.'
        )
        public String toAddresses;

        @InvocableVariable(
            label = 'Cc Addresses'
            description = 'Comma-delimited list of email addresses. At least one of Who ID, To, Cc, Bcc Addresses must be set.'
        )
        public String ccAddresses;

        @InvocableVariable(
            label = 'Bcc Addresses'
            description = 'Comma-delimited list of email addresses. At least one of Who ID, To, Cc, Bcc Addresses must be set.'
        )
        public String bccAddresses;

        @InvocableVariable(
            label = 'Org Wide Address'
        )
        public String OWAddress;
        
        @InvocableVariable(
            label = 'Reply To'
            description = 'The email address that receives the message when a recipient replies.'
        )
        public String replyTo;
        
        @InvocableVariable(
            label = 'Subject'
            description = 'The email Subject.'
        )
        public String Subject;
        
        @InvocableVariable(
            label = 'Text Body'
            description = 'The text body of the email.'
        )
        public String TextBody;
        
        @InvocableVariable(
            label = 'HTML Body'
            description = 'The HTML body of the email.'
        )
        public String HtmlBody;
        
        @InvocableVariable(
            label = 'Content Version Ids'
        )
        public list<String> ContentVersionIds;

    }

}