The Sitecore Content Hub allows the use of email templates. You can also use scripts to control your emails. Here is how to use existing email templates.
Confirm email address
Please verify the email address of the account you are using in order to verify the following If you have administrative privileges, you can verify it by following the steps below.
- Go to the Administration tool with the Manage icon
- Click on User Tools
- Open the target account
- Click Edit Profile
- Change the email address listed in Email
The above procedure confirms the following procedures
Email Templates
Email Templates is available as an item on the Administration page. This tool allows you to manage the email templates deployed at your site.The Forgot password template is opened as shown below. In this screen, the standard English template is selected.
To actually send a test email, click `Send Test Email` in the upper right corner of the template to send an email using the corresponding email template.
Sending email using scripts
Scripts are provided as a mechanism for easy processing within the Sitecore Content Hub system. Here we will register a C# script.
Register a new script
To register a new script, go to the Administration page, select Scripts, and then register the script. The procedure is as follows
- Open the administration screen
- Open Script
- Click on the + Script button in the upper right corner of the screen
- Save the script with a name, type and summary
The above procedure creates a new script.
Template creation code
In this case, we will create a script to send an email (to send the Forget Password email above). The page we will refer to is as follows
The following code will retrieve the template.
IMailTemplate template = await client.Notifications.GetMailTemplateAsync("ForgotPassword");
For this IMailTemplate, the namespace Stylelabs.M.Sdk.Contracts.Notifications is used, as in the Mail Template. The interfaces, etc. defined in this namespace are defined in Namespace Stylelabs.M.Sdk.Contracts.Notifications.
The aforementioned page also provides sample code for creating a template. In this case, we will create an email based on this.
CultureInfo enUs = CultureInfo.GetCultureInfo("en-US");
IMailTemplate template = await client.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = "Hello world template";
template.SetSubject(enUs, "Hello there!");
template.SetBody(enUs, "Hello {{Username}}!");
var templateVariable = new TemplateVariable
{
Name = "Username",
VariableType = TemplateVariableType.String
};
template.SetTemplateVariables(new[] { templateVariable });
await client.Entities.SaveAsync(template);
CultureInfo class is a standard library provided by C#, and its namespace is System.Globalization. Therefore, the code is as follows.
using System.Globalization;
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Models.Notifications;
using Stylelabs.M.Sdk.Contracts.Notifications;
CultureInfo enUs = CultureInfo.GetCultureInfo("en-US");
IMailTemplate template = await client.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = "Hello world template";
template.SetSubject(enUs, "Hello there!");
template.SetBody(enUs, "Hello {{Username}}!");
var templateVariable = new TemplateVariable
{
Name = "Username",
VariableType = TemplateVariableType.String
};
template.SetTemplateVariables(new[] { templateVariable });
await client.Entities.SaveAsync(template);
What happens if I register the above code as is? As shown in the following screen, an error is displayed in the client definition when the build is executed.
Two objects are provided when executing a Sitecore Content Hub script: the MClient and the Context. In this case, we will use the MClient since we will be sending out emails. The properties of each object are described in the following pages.
If you find that "client" is defined as "MClient," rewrite it as "MClient," save it, and execute the build. The following code will build successfully.
using System.Globalization;
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Models.Notifications;
using Stylelabs.M.Sdk.Contracts.Notifications;
CultureInfo enUs = CultureInfo.GetCultureInfo("en-US");
IMailTemplate template = await MClient.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = "Hello world template";
template.SetSubject(enUs, "Hello there!");
template.SetBody(enUs, "Hello {{Username}}!");
var templateVariable = new TemplateVariable
{
Name = "Username",
VariableType = TemplateVariableType.String
};
template.SetTemplateVariables(new[] { templateVariable });
await MClient.Entities.SaveAsync(template);
Email Transmission Code
Then use the template to check the code to send the email. Once again, check the following page.
A script for sending the email is included. The code is as follows
var request = new MailRequestBroadcast
{
MailTemplateName = "Hello world template"
};
request.Variables.Add("Username", "world");
await client.Notifications.SendEmailNotificationAsync(request);
This code uses the code already created to handle the following
- Template name: Hello world template
- World is assigned to Username defined as a variable
Please add this to the end of the template creation code. Of course, you will have to change the client.
using System.Globalization;
using Stylelabs.M.Sdk;
using Stylelabs.M.Sdk.Models.Notifications;
using Stylelabs.M.Sdk.Contracts.Notifications;
CultureInfo enUs = CultureInfo.GetCultureInfo("en-US");
IMailTemplate template = await MClient.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName) as IMailTemplate;
template.Name = "Hello world template";
template.SetSubject(enUs, "Hello there!");
template.SetBody(enUs, "Hello {{Username}}!");
var templateVariable = new TemplateVariable
{
Name = "Username",
VariableType = TemplateVariableType.String
};
template.SetTemplateVariables(new[] { templateVariable });
await MClient.Entities.SaveAsync(template);
var request = new MailRequestBroadcast
{
MailTemplateName = "Hello world template"
};
request.Variables.Add("Username", "world");
await MClient.Notifications.SendEmailNotificationAsync(request);
After a successful build, press the Publish button to make this script available.
Script Execution
This time, the trigger is triggered when the content of the asset is changed, and the script is executed using the specified action, so that the user receives a sample email.
Create Action
In order to use the script you have created, this time we will check its operation by tying it to a trigger and an action, in the form of an email that is sent when an asset is registered. Please refer to the page that has already introduced triggers and actions.
The first step is to activate the script you have just created so that it is available for use in actions. If you browse to the script you just created on the Scripts List page, you will see that it has not been activated.
First, activate the script so that it can be selected. The procedure is simple, one click.
Next, create a new action from the Actions menu on the Administration page, and specify the script you have just created.
The action is now complete.
Creating Triggers
As for triggers, we will use simple rules to handle them this time. For this reason, the trigger itself should be disabled at the end of the test.
First, as for the trigger items, set the basic items for a new trigger as shown in the following screen.
As for the condition, select M.Tag to process when a new taxonomy is added, and do not set any particular condition.
Then, in the Action field, specify the action you just created.
Now you are ready to go.
Testing in practice
This time, when you create a new M.Tag - Taxonomy, a trigger is triggered to run the specified action, the script set in the action. So, we will actually execute the new taxonomy. The audit of the action shows that it is executed. As a result, there is also an email in the mailbox.
A demonstration of the sequence of actions can be seen in the following image.
Summary
In this article, we have introduced a sample of creating a script, combining a trigger and an action, and sending out an e-mail when a change occurs. Actions can also be applied to buttons placed on the page, so you can get an idea of how scripts can be used as a mechanism for efficient operation.