Send Email with Twilio SendGrid
This recipe shows how to use Twilio SendGrid to send email messages, pulling subject, content, and recipient information from various sources. The Orchestration Component that sends the email is designed as a generic template that can be reused for many purposes.
Overall Orchestration Flow
This example uses Context to pass the email subject, content, and recipient to a DX Engine orchestration flow that sends an email message using Twilio.
Mapping Out DX Engine Elements
This example includes a single Twilio Send Email Orchestration Component that sends an email message.
The Twilio Send Email Orchestration Component depends on a Twilio SendGrid Connection DX Engine Connection.
The Twilio SendGrid Connection DX Engine Connection depends on a Twilio SendGrid Token Secret.
DX Engine Configuration Details
The topics in this section explain how to configure the DX Engine elements used in this recipe.
The Twilio SendGrid Token Secret
To create the Twilio SendGrid Token Secret, in the DX Engine UI:
- Click Settings in the top-nav, and then select Secrets.
- Click Add Secret. The Create Secret wizard appears.
- Set Secret Code to a value such as
twilio-sendgrid-token
. - Set Secret Name to a value such as Twilio SendGrid Token.
- Set Secret Value to the Twilio API token.
- Optionally, enter a Description for the secret.
- Click Submit.
The Twilio SendGrid Connection DX Engine Connection
To create the Twilio SendGrid Connection DX Engine Connection, in the DX Engine UI:
- Click Settings in the top-nav, and then select Connections. The Manage Connections page appears.
- Click Add Connection. The Create Connection wizard appears.
- Set Connection Code to a value such as
twilio-sendgrid-connection
. - Set Connection Name to a value such as Twilio SendGrid Connection.
- Optionally, enter a Connection Description for the connection.
- Select the Universal API Connector.
- Click Submit.
To configure the Twilio SendGrid Connection DX Engine Connection, in the Manage Connections page of the DX Engine UI:
- Click the Edit button for the Twilio SendGrid Connection DX Engine Connection. The Edit Connection wizard appears.
- Under Base URL, for Get value from, select Literal.
- Set the value of Base URL to the Twilio Sendgrid API base URL, such as
https://api.sendgrid.com/v3
. - Under Base Headers, click Add another item.
- For the new header, set Header to Authorization.
- For the Authorization header, set Value to JS Expression.
- For the value of the Authorization header, enter the following expression:
`Bearer ${secret('twilio-sendgrid-token')}`
- Under Base Headers, click Add another item.
- For the new header, set Header to Content-Type.
- For the Content-Type header, set Value to Literal.
- For the value of the Content-Type header, enter the following:
application/json
- Click Submit.
The Twilio Send Email Orchestration Component
To create the The Twilio Send Email Orchestration Component, in the DX Engine UI:
- In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
- Click Add Component. The Create Component wizard appears.
- For Component Code, enter an identifier for the component, such as
twilio-send-email
. - For Name, enter a name for the component, such as Twilio Send Email**.
- Optionally, enter a Component Description.
- Select the No Rules checkbox. Experience Rules are not relevant to this Component.
- For Component Type, select Conscia - Universal API Connector. This Component Type can connect to almost any Webservice API.
- Click Submit.
To configure the Twilio Send Email Orchestration Component, on the Manage Components page:
- Next to the Twilio Send Email Orchestration Component, click the Edit button. The Edit Component wizard appears.
- For Connection, select the Twilio SendGrid Connection DX Engine Connection.
- For Webservice Path, select Literal, and enter
/mail/send
for the value. This is the URL path of the Twilio SendGrid API relative to the basea URL defined by the Twilio SendGrid Connection DX Engine Connection. - For Method, select POST.
- Under Body, select JS Expression and enter:
_.assign({},
{
"personalizations": [
{
"to":[
{
"email": contextField("emailMessage").to.email,
"name": contextField("emailMessage").to.name
}]
}],
"from":
{
"email": contextField("emailMessage").from.email,
"name": contextField("emailMessage").from.name
},
"subject": contextField("emailMessage").subject,
"content": [
{
"type": contextField("emailMessage").content.type,
"value": contextField("emailMessage").content.value
}]
})
To test this component, you can pass Context such as the following, replacing values as appropriate:
{
"emailMessage": {
"to": {
"email": "{recipient-email}",
"name": "{recipient-name}"
},
"from": {
"name": "{sender-name}",
"email": "{sender-email}"
},
"subject": "{email-subject}",
"content": {
"value": "{html-body}",
"type": "text/html"
}
}
}
Components can invoke the Twilio Send Email Component as a Sub Component as described in Logging with Dynatrace, but using the emailMessage
Context Field.