Send Email Transactional Messages Through Direct Send API

Use Direct Send APIs to preview and send transactional email to one or more recipients. The email comes from the published Salesforce CMS content that’s linked to the send definition. Personalize it with values that you pass in the request, and override parts of it or add attachments for each recipient.

Prerequisites 

  • An access token that has the sfap_api scope. The token’s Run As user must have permission to create send definitions. See Connect to Direct Send APIs.
  • API version 68.0 or later for the send definition request.
  • A sender address on a domain that’s authorized for email sending in your org.
  • Published email content in Salesforce CMS, of type sfdc_cms__email, and its ContentKey. For content-variable personalization, supply values under attributes.$content that match the published content’s schema.

If you already have a send definition ID, skip to step 2.

Step 1: Create an Email Send Definition 

Create the send definition on your org’s My Domain host. The sender is the fromAddress.

1curl -X POST "https://YOUR_DOMAIN.my.salesforce.com/services/data/v68.0/connect/e360/email/sendDefinition" \
2  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
3  -H "Content-Type: application/json" \
4  -d '{
5    "name": "welcome-email@1.0.0",
6    "fromAddress": "noreply@YOUR_AUTHORIZED_DOMAIN",
7    "managedContentKey": "YOUR_EMAIL_CONTENT_KEY",
8    "communicationSubscription": "YOUR_SUBSCRIPTION_NAME"
9  }'

Replace the placeholders with your own values.

  • YOUR_DOMAIN: Your org’s My Domain name.
  • YOUR_ACCESS_TOKEN: Your access token.
  • YOUR_AUTHORIZED_DOMAIN: A domain that’s authorized for email sending in your org.
  • YOUR_EMAIL_CONTENT_KEY: The ContentKey of the published email content.
  • YOUR_SUBSCRIPTION_NAME: The name of the communication subscription. Whether Email requires one depends on your org’s consent settings. When a promotional subscription is required and you omit it, the API uses the Marketing subscription, which must be configured in your org.

Use role-based sender addresses, such as noreply@, support@, or orders@. Email send definition names must be unique in your org.

The response contains the sendDefinitionId. Save it for the next steps.

You can also pass these optional fields.

FieldDescription
fromNameThe sender’s display name.
replyToAddressThe reply-to email address.
replyToNameThe reply-to display name.
messagePurposePROMOTIONAL or TRANSACTIONAL. Required if you don’t pass managedContentKey, or if the content has no purpose. If the content has a purpose, this value must match it. See Message Purpose.
openTrackingEnabledIndicates whether email opens are tracked. Email is the only channel with open tracking. See Tracking.
clickTrackingEnabledIndicates whether link clicks are tracked.
dataSpaceNameThe developer name of a data space. If you omit it, the API uses the default data space.
campaignIdThe ID of a Salesforce campaign.
descriptionA description of up to 4,000 characters.

For every field, see Email - Send Definitions (Connect API). If the request fails, see Troubleshoot Send Definition Errors.

Step 2: Preview the Email 

Preview renders the email without sending it. Use it to test combinations of content and data. Preview requires individualId and attributes. It doesn’t require to, because no email is delivered. A complete email needs a subject and at least an HTML body or a plain-text body. These can come from the linked CMS content. If you supply the complete content inline, include subject and at least one of body or text.

Preview and send go to api.salesforce.com, not to your My Domain host. If you call the same path on your My Domain host, the request returns a 404 page that says URL No Longer Exists.

Important

1curl -X POST "https://api.salesforce.com/automation/actions360/messaging/email/v1/preview?sendDefinitionId=YOUR_SEND_DEFINITION_ID" \
2  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
3  -H "Content-Type: application/json" \
4  -d '{
5    "individualId": "Individual__dlm/test-user",
6    "attributes": {
7      "$content": {
8        "firstName": "Alice",
9        "company": "Acme Corp"
10      }
11    }
12  }'

Replace YOUR_SEND_DEFINITION_ID with the ID from step 1. For a test org, use https://test.api.salesforce.com/automation/actions360 as the base URL.

A 200 response contains the rendered content under response.

1{
2  "response": {
3    "subject": "Welcome to Acme Corp, Alice!",
4    "body": "<html>...rendered HTML...</html>",
5    "text": "Welcome to Acme Corp, Alice! ...",
6    "preheader": "Your account is ready"
7  }
8}

Check that the subject, HTML body, plain text, and preheader look the way you expect. If the body is empty, check that the CMS content is published and that the send definition links the email content that you intended.

Step 3: Send to One Recipient 

Use the same endpoint without /preview. This request delivers the email, so set to to an inbox that you control.

1curl -X POST "https://api.salesforce.com/automation/actions360/messaging/email/v1?sendDefinitionId=YOUR_SEND_DEFINITION_ID" \
2  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
3  -H "Content-Type: application/json" \
4  -d '{
5    "to": "alice@example.com",
6    "individualId": "Individual__dlm/customer-001",
7    "messageId": "email-welcome-001",
8    "locale": "en_US",
9    "attributes": {
10      "$content": {
11        "firstName": "Alice",
12        "message": "How are you?"
13      }
14    }
15  }'
FieldRequiredDescription
toYesThe destination email address.
individualIdYesThe recipient’s identity, used for processing and correlation.
attributesYesThe personalization values. See Personalize the Email.
messageIdNoA unique ID for the message, used for idempotency and tracing. If you omit it, the service generates one.
localeNoA locale, such as en_US, that overrides the template’s default locale for this recipient.

To change the sender, add CC or BCC addresses, override the subject or body, or attach files for this recipient, see Override Parts of the Email and Add Attachments.

A 200 response means that the request was processed. Check the recipient’s status, SUCCESS or FAIL, to find out whether the email was accepted for delivery. Acceptance doesn’t confirm delivery. See Handle Responses, Errors, and Retries in Direct Send API.

Step 4: Send to Multiple Recipients 

To send one email to many addresses, post to the /bulk endpoint. Pass a recipients array and an optional batch-level requestId. Each recipient can have its own locale, overrides, and attachments.

1curl -X POST "https://api.salesforce.com/automation/actions360/messaging/email/v1/bulk?sendDefinitionId=YOUR_SEND_DEFINITION_ID" \
2  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
3  -H "Content-Type: application/json" \
4  -d '{
5    "requestId": "email-welcome-batch-001",
6    "recipients": [
7      {
8        "to": "john@example.com",
9        "individualId": "Individual__dlm/user-001",
10        "messageId": "email-welcome-user-001",
11        "locale": "en_US",
12        "attributes": {"$content": {"firstName": "John"}}
13      },
14      {
15        "to": "jane@example.com",
16        "individualId": "Individual__dlm/user-002",
17        "messageId": "email-welcome-user-002",
18        "locale": "fr_FR",
19        "attributes": {"$content": {"firstName": "Jane"}}
20      }
21    ]
22  }'

The response contains a result for each recipient, in request order. Some recipients can fail even when others succeed, so check every recipients[].status. See Handle Responses, Errors, and Retries in Direct Send API.

Personalize the Email 

Pass the personalization values that the published content expects. Email supports three personalization models. Use the model that your content was built for, and keep the namespaces separate.

ModelWhen to Use ItHow to Pass Values
Content variables (recommended)The content declares its variables in contentBody["sfdc_cms:schema"] and reads them from $content.Under attributes.$content, as in step 3.
Apex-backed contentThe content uses an Apex data provider.Under the expression key that the provider configures, such as "attributes": {"ApexClassName": {"field1": "value1"}}.
Free-form variablesThe content uses free-form variables, such as $firstName.Directly under attributes, such as "attributes": {"firstName": "Alice"}.

For content variables, the $content key is case-sensitive, and the property names and casing must match the schema. Supply every property that the schema requires. In raw HTML email content, the Handlebars lookup looks like {{fallback (get (get @root "$content") "firstName") "-"}}.

The content schema doesn’t describe or validate free-form values, and a free-form value doesn’t fill a $content variable.

Other system or channel attributes, such as $link, can sit next to $content in attributes.

Override Parts of the Email 

These optional fields go on the recipient object, next to attributes, not inside it. In a bulk send, they can differ for each recipient.

FieldDescription
subjectOverrides the subject template.
bodyOverrides the HTML body template.
textOverrides the plain-text template.
fromOverrides the from address.
replyToOverrides the reply-to address.
ccAn array of CC email addresses.
bccAn array of BCC email addresses, such as ["audit@example.com"]. Other recipients don’t see BCC addresses.
ttlSecondsThe maximum lifetime of the email, from 1 through 86,400 seconds (24 hours). If you omit it, the service’s default expiration applies.
attachmentsAn array of Base64-encoded files. See Add Attachments.

Add Attachments 

You supply attachments when you send. The send definition doesn’t need any attachment setup, and each recipient can have different files. Each attachment has four fields.

  • fileLocationType: Set to "base64".
  • fileLocation: The file’s bytes as a Base64 string, with no line breaks and no data:...;base64, prefix.
  • fileName: The file name, such as invoice.pdf. The extension must match the file format.
  • contentType: The file’s MIME type. The API doesn’t infer it from fileName, so always set it.
File FormatcontentType
PDF (.pdf)application/pdf
Calendar (.ics)text/calendar
JPEG (.jpg)image/jpeg
PNG (.png)image/png
GIF (.gif)image/gif
WebP (.webp)image/webp
Word (.docx)application/vnd.openxmlformats-officedocument.wordprocessingml.document
Word (.doc)application/msword
Excel (.xlsx)application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Excel (.xls)application/vnd.ms-excel
PowerPoint (.pptx)application/vnd.openxmlformats-officedocument.presentationml.presentation
PowerPoint (.ppt)application/vnd.ms-powerpoint
CSV (.csv)text/csv
Plain text (.txt)text/plain
Rich text (.rtf)application/rtf
MP4 (.mp4)video/mp4

Other MIME types, including ZIP archives, aren’t supported.

This example uses base64, tr, and jq to encode a local invoice.pdf file, build the request, and send it.

1base64 < invoice.pdf | tr -d '\r\n' |
2  jq -Rs '{
3    "to": "alice@example.com",
4    "individualId": "Individual__dlm/customer-001",
5    "messageId": "email-invoice-001",
6    "attributes": { "$content": { "firstName": "Alice" } },
7    "attachments": [
8      {
9        "fileLocationType": "base64",
10        "fileLocation": .,
11        "fileName": "invoice.pdf",
12        "contentType": "application/pdf"
13      }
14    ]
15  }' |
16  curl -X POST "https://api.salesforce.com/automation/actions360/messaging/email/v1?sendDefinitionId=YOUR_SEND_DEFINITION_ID" \
17    -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
18    -H "Content-Type: application/json" \
19    --data-binary @-

See Also