Send Rich Email with the new SendHTMLEmail Action

Video introduction

Installation (See bottom of post)

Source code

Flow provides a built-in Send Email action but it’s something of an underachiever. However, using the power of Apex and Flow Actions, its possible to build a powerful SendHTMLEmail action that can be easily installed and used from within Flow Builder. MVP Jeremiah Dohn pioneered this space several years back with his well received HTML Email Flow action. The version here is similar and adds a couple of additional capabilities.

This improved email experience provides:

Rich, HTML email bodies that can use all of the capabilities of Flow’s Text Templates, including Bold, Italics, Underline, Bullets and Numbers, Left/Center/Right justification, URLs, images, fonts, and text size:

This action also allows you to use Email Templates (both Classic and Lightning), so long as you provide it with a recordId to a Contact, Lead, or User that can be used to fill in the merge fields of the template:

To make it easy to pass email addresses into the action, it supports any combination of the following, for To, CC, and BCC:

  • A single email address
  • A string collection of email addresses
  • A collection of Contacts
  • A collection of Users
  • A collection of Leads

Other supported features include:

  • Support for Organization-Wide Email Addresses
  • Support for Attachments
  • Ability to toggle on/off the use of the Salesforce per-user email signature
  • Ability to provide either a plain text body, an html body, or both
  • Ability to set the ReplyTo email address and the Sender Display Name

Instructions

This sample flow ships with the action and can be used to test and demonstrate the action:

Here’s how I have my inputs configured in the above sample Flow for an HTML body from a Text Template:

Here’s how it would be configured for use with an Email Template. Note the plainTextBody, HTMLbody, and Subject are all nulled out (when “Don’t Include” is selected, null values are used):

You can use any of these five combinations:

  • An HTML Body
  • A plain text body
  • Both an HTML Body or a plain text body
  • A Classic Email Template
  • A Lightning Email Template

However, you’ll get errors if you try to mix your own bodies and subjects with Email Templates.

Using Templates

If you want to use an Email Template, you must pass in the Id (not the name) of the template. In the above flow example, I use a Get Record to query for an email template by name, and then pass in the ID to the SendHTMLEmail flow action. Importantly, you need to also pass in the recordId of a Contact, Lead, or User that will serve as the context record for the template and populate the template’s merge fields. Do this with the templateTargetObjectID parameter

Setting the Sender/ReplyTo Address

You can either 1) use an OrganizationWideEmailAddress or 2) specify your own Reply-To Email Address and Sender Display Name. If you provide an OrganizationWideEmailAddress, that will take precedence and any Reply-To Email Address and/or Sender Display Name you’ve also provided will be ignored.

To use an OrganizationWideEmailAddress , pass the Id of one that you have defined into the action via the “Use This Organization-Wide Email Address Id”. (If you want to get the benefit of Verified sending, you can’t just paste the email address into the Reply-To Email Address field. You have to use the Use This Organization-Wide Email Address Id field.). to find the ID:

  1. In Lightning Setup, go to Organization-Wide Addresses and click Edit on the address you want to use:

2. Examine the URL of this web page. It should look something like this:

https://connect-dream-4759-dev-ed.lightning.force.com/lightning/setup/OrgWideEmailAddresses/page?address=%2F0D2560000008PGP%2Fe

3. The Id of the Organization-Wide Email Address is the character between the 2 “%2F” strings. So in the example above, you would extract “0D2560000008PGP” and insert it as an input:

Attachments

You can pass in a collection of Attachments. In many cases, though, you’ll be working with Content Document Links that you’ve retrieved with a Get Records. This package also includes therefore an action called ConvertFilesToAttachments that takes a collection of Content Document Links and returns a collection of Attachments. For more information on the relationship between these various document-related types, see this.

Considerations

This action makes use of the standard Salesforce email infrastructure, the same service that you use when you send a single email directly from a lightning experience page or a classic case feed. As such, any emails you send will be included in your usage, so be aware of the limits and guidelines. (Developer note: this action makes use of the Apex Messaging service API’s.)

This action does not use or involve Email Alerts, which are used in Workflow Rules and exist in Flow as separate actions you can drag into your flow. Be mindful of this so you don’t accidentally confuse the Email Alerts, the Email Templates, and the Flow Text Templates.

Currently unsupported features include:

  • Support for the MassEmailMessage API, enabling higher volumes
  • Character Set setting

If you’re interested in extending this class, please do so!


Installation

Old versions

1.0 Unmanaged

1.0 – Managed (requires Winter ’20)

20 replies
  1. Megan Moody
    Megan Moody says:

    I was able to get this work – awesome! – but the sender comes through as unverified. I’m populating the replyEmailAddress variable with one of our org-wide email addresses. Is it because I’m sending from a sandbox? Will the behavior be different in production? Also, is there any trick to adding images to the text template being used for the HTML body? I added one, but it doesn’t go with the email. Thanks for any tips you can provide!

    Reply
  2. George
    George says:

    Amazing to have Lightning Emails available in a flow – thanks. Am I right in saying that this currently doesn’t support custom object merge fields in the template email – can only be merge fields on the contact, user or lead object?

    Reply
    • Alex Edelstein
      Alex Edelstein says:

      Yes, those are current limitations of Lightning Email Templates.

      Here’s how I’d solve this. I’d build a flow that creates a Contact record just to hold the custom information from the custom object record that I’m about to send email for. I’d populate the contact’s fields with my custom data. For example, I might set Contact.Description (which can be used in a Lightning Email Template as a merge field) to a Text Template string that includes a part number, a part name, and a billing code. I’d then save my new contact, getting the ID back into my flow. Then the same flow can use this Send Email to invoke a Lightning Email Template that uses that contact Id that’s now full of my custom object’s key data. Then I’d have the same flow delete the pseudo contact once the email is sent, although I might need to insert a 1 hour pause to make sure the contact doesn’t get deleted before the email system has time to pull the custom data from it!

      Reply
  3. Richard
    Richard says:

    Hello Alex,

    thanks a lot for your hard work, I am looking for that kind of solution for a long time. Unfortunately the link for the updated version (1.1 unmanaged) is not working. I only see “v0.1 Beta” in the version details and the testing flow is not opening at all (loading loop, so I can’t open it) after package installation.

    Can you provide a new link to the actual update?

    Thanks a lot!

    Reply
  4. Nkosi
    Nkosi says:

    Hi, thanks a lot for your hard work . I am just having issues with a null pointer exception if I dont include an attachment and no email is being sent when attachments are included, no error but no email sent

    Reply
    • Alex Edelstein
      Alex Edelstein says:

      I’ll try to reproduce the no-attachment issue. The most common reason for email not being sent is that your org has hit its limits on email or isn’t enabled to send email. Are you using a scratch org?

      Reply
      • Alex Edelstein
        Alex Edelstein says:

        Can you confirm you’re getting a null pointer exception and not this error, which is similar but doesn’t involve pointers:

        Error element null (null).
        Flow encountered an error when processing and converting between data types. Please check the flow and ensure all data types are matched correctly.

        Reply
        • Johann moulin
          Johann moulin says:

          Hello Alex, thank you for this code. I have the same issue. I don’t think I have hit the limit and email are enabled in my org. Last message is a success but no email is sent. If I disable the attachment, it works fine. I use a collection of attachment (no need for cdl because my object uses attachments). Thank for your help

          Reply
          • Johann moulin
            Johann moulin says:

            Alex, I have tried with the conversion of cdl and it works fine. So when I create a collection of attachments, it doesn’t send the email, when I create a collection of cdl and I pass them through the convert action, it works fine. Hope it helps

  5. Moulin
    Moulin says:

    Well, I have solved this issue. When you get the attachment in your flow, you have to request all the fields (Id, Name, Contentype and Body). Once you have requested all these fields, it works fine.

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply