There are two parts to this question: Sitecore Forms and EXM.
In order to pass form fields from Sitecore Forms to EXM, you need to create a custom Submit Action that processes the fields on the page and then sends an email using the EXM Client API.
Sitecore Forms
When creating a custom Submit Action, you override the Execute method. Here's an example of that override.
protected override bool Execute(T data, FormSubmitContext formSubmitContext)
{
if (data.MessageId == Guid.Empty)
{
logger.LogWarn("Empty message id");
return false;
}
var toContacts = GetToContacts(data, formSubmitContext);
if (toContacts == null || toContacts.Count == 0)
{
return false;
}
try
{
var customTokens = BuildCustomTokens(data, formSubmitContext);
foreach (var to in toContacts)
{
SendMail(to, customTokens, data.MessageId);
}
}
catch (Exception ex)
{
logger.LogError(ex.Message, ex);
return false;
}
return true;
}
In this example, the developer is creating a Dictionary object called customTokens
through the BuildCustomTokens() method that will be passed into the Sitecore EXM Client API, which is called in the SendEmail()
method as shown below.
protected virtual Dictionary<string, object> BuildCustomTokens(T data, FormSubmitContext formSubmitContext)
{
var formFields = formFieldConverter.Convert(formSubmitContext.Fields);
var customTokens = new Dictionary<string, object>();
customTokens.Add(Constants.CustomTokensFormKey, formFields);
foreach (var formField in formFields)
{
customTokens.Add($"form_{formField.Name}", GetSingleStringValue(formField));
}
return customTokens;
}
protected virtual void SendMail(ContactIdentifier toContact, Dictionary<string, object> customTokens, Guid messageId)
{
var automatedMessage = new AutomatedMessage();
automatedMessage.ContactIdentifier = toContact;
automatedMessage.MessageId = messageId;
automatedMessage.CustomTokens = customTokens;
automatedMessage.TargetLanguage = Sitecore.Context.Language.Name;
clientApiService.SendAutomatedMessage(automatedMessage);
}
Email Experience Manager
Now that the submit action is created, you need to create an EXM Message that has a Rich Text area that can take in the $field_name$ token, which will be replaced, because of the custom token dictionary passed in through the Client API statement.
Credit to Bart Verdonck and this Sitecore Forms Extension module for the code snippets. This module provides the SendEmail submit action as an extension to Sitecore Forms.