This guide explains how to automate the creation of Secure EmailMessage records in Salesforce using declarative tools (Flow) or Apex, so secure emails can be triggered by business processes instead of manual sends.
How Automation Works
Inserting a Secure EmailMessage record — whether from a Flow, a trigger, or Apex — automatically triggers the Secure Email service to handle encryption, delivery, and logging. No separate "send" call is needed once the record is inserted.
Use automation when:
-
You want to integrate secure email sending into existing business processes
-
You want to send secure communication without manual user input (reminders, notifications, escalations)
Key Fields for Secure Email Automation
|
Label |
API Name |
Required |
Description |
|---|---|---|---|
|
From Address |
|
Required |
Sender email address; must be a valid Org-Wide Email Address |
|
Recipients |
|
Required |
One or more recipient addresses, comma-separated for multiple |
|
CC Recipients |
|
Optional |
CC addresses, comma-separated |
|
BCC Recipients |
|
Optional |
BCC addresses, comma-separated |
|
Reply-To Address |
|
Required |
Must be an Org-Wide Email Address |
|
Subject |
|
Required* |
Subject line — required unless a template is used |
|
HTML Body |
|
Required* |
Email body in HTML — required unless a template is used |
|
Template Name |
|
Optional |
When set, Subject and Body are pulled from the template instead — see Setup Lightning Email Templates |
|
Parent ID |
|
Required |
Salesforce record ID (Contact, Account, Case, etc.) to link the Secure Email to |
|
Record Type ID |
|
Required |
Defines whether the email is Outbound or another record type |
|
Scheduled Time |
|
Optional |
Time at which the message will be sent |
|
Process via Batch |
|
Required if bulk or scheduled |
When true, the batch process picks up the message and sends it at the scheduled time, or as part of a bulk run |
*Subject and HTML Body are required only if you're not using a template — pick one approach, not both (see examples below).
Sample Code: Sending via Apex
Option A — using a template (subject/body pulled from the template automatically):
cmsecureemail__Secure_EmailMessage__c oRecord = new cmsecureemail__Secure_EmailMessage__c();
oRecord.cmsecureemail__FromAddress__c = 'from@email.com';
oRecord.cmsecureemail__Recipients__c = 'to@email.com, another@email.com';
oRecord.cmsecureemail__Reply_to__c = 'orgwide@email.com';
// Assign Record Type (Outbound)
oRecord.RecordTypeId = Schema.SObjectType.Secure_EmailMessage__c
.getRecordTypeInfosByDeveloperName().get('Outbound').getRecordTypeId();
// Link to parent record
oRecord.cmsecureemail__Parent_Record_Id__c = '001XXXXXXXXXXXX'; // e.g. Account Id
// Content comes from the template — no need to set Subject/HtmlBody
oRecord.cmsecureemail__Template_Name__c = 'Contact Template';
insert oRecord;
Option B — hardcoded content (no template):
cmsecureemail__Secure_EmailMessage__c oRecord = new cmsecureemail__Secure_EmailMessage__c();
oRecord.cmsecureemail__FromAddress__c = 'from@email.com';
oRecord.cmsecureemail__Recipients__c = 'to@email.com, another@email.com';
oRecord.cmsecureemail__Reply_to__c = 'orgwide@email.com';
oRecord.RecordTypeId = Schema.SObjectType.Secure_EmailMessage__c
.getRecordTypeInfosByDeveloperName().get('Outbound').getRecordTypeId();
oRecord.cmsecureemail__Parent_Record_Id__c = '001XXXXXXXXXXXX';
oRecord.cmsecureemail__Subject__c = 'Test Subject';
oRecord.cmsecureemail__HtmlBody__c = 'Hello Cloud Maven';
insert oRecord;
Once the record is inserted, the Secure Email service automatically handles encryption, delivery, and logging.
Automation Options
1. Flow — recommended for admins
-
Use a Record-Triggered Flow on objects like Contact, Case, or Opportunity
-
Create a new Secure EmailMessage record in the Flow and populate the fields above
-
Use formula/resource variables to populate fields dynamically:
-
From Address → default Org-Wide Address
-
Recipients →
Contact.Email/Case.ContactEmail -
Parent ID →
{!$Record.Id} -
Template Name → the Lightning Email Template Id (recommended over hardcoding subject/body)
-
Subject/Body → driven by your business logic, if not using a template
-
2. Apex — for advanced use cases
Best suited to Batch or Queueable jobs for bulk sending, such as:
-
Automated campaign sends
-
Case escalations
-
System-driven notifications
Example — sending in bulk:
List<cmsecureemail__Secure_EmailMessage__c> emails = new List<cmsecureemail__Secure_EmailMessage__c>();
for (Contact con : [SELECT Id, Email FROM Contact WHERE Email != null LIMIT 100]) {
cmsecureemail__Secure_EmailMessage__c email = new cmsecureemail__Secure_EmailMessage__c();
email.cmsecureemail__FromAddress__c = 'orgwide@email.com';
email.cmsecureemail__Recipients__c = con.Email;
email.cmsecureemail__HtmlBody__c = 'Secure Email Test';
email.RecordTypeId = Schema.SObjectType.Secure_EmailMessage__c
.getRecordTypeInfosByDeveloperName().get('Outbound').getRecordTypeId();
email.cmsecureemail__Parent_Record_Id__c = con.Id;
email.cmsecureemail__Reply_to__c = 'orgwide@email.com';
email.cmsecureemail__Subject__c = 'Automated Test Secure Email';
emails.add(email);
}
insert emails;
Best Practices
-
Always use Org-Wide Email Addresses for From/Reply-To fields
-
For bulk operations, insert records in batches and monitor governor limits
-
Use error handling in both Flows and Apex to catch insert failures
-
Periodically review delivery status (Processing, Delivered, Bounced, etc.) to confirm compliance — see Data Model and ER Diagrams for the full list of status fields
Support
For setup assistance or troubleshooting, contact mailto:support@cloudmaveninc.com.
Related Pages
-
Data Model and ER Diagrams — full field reference for Secure EmailMessage and Email Recipient
-
Setup Lightning Email Templates — creating templates referenced by Template Name
-
Configuration Settings — batch job settings referenced by Process via Batch
-
Post-Installation Guide — org setup required before automation will work