Skip to main content

Send Email with Twilio SendGrid

You can implement a solution based on this recipe that uses Twilio SendGrid to send email messages.

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 (twilio-send-email) Orchestration Component that sends an email message.

The Twilio Send Email (twilio-send-email) Orchestration Component depends on a Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection.

The Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection depends on a Twilio SendGrid Token (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 (twilio-sendgrid-token) Secret

To create the Twilio SendGrid Token (twilio-sendgrid-token) Secret, in the Conscia DX Engine DX Studio UI:

  1. Click Settings in the top-nav, and then select Secrets.
  2. Click Add Secret. The Create Secret wizard appears.
  3. Set Secret Code to a value such as twilio-sendgrid-token.
  4. Set Secret Name to a value such as Twilio SendGrid Token.
  5. Set Secret Value to the Twilio API token.
  6. Optionally, enter a Description for the secret.
  7. Click Submit.

The Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection

To create the Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection, in DX Studio:

  1. Click Settings in the top-nav, and then select Connections. The Manage Connections page appears.
  2. Click Add Connection. The Create Connection wizard appears.
  3. Set Connection Code to a value such as twilio-sendgrid-connection.
  4. Set Connection Name to a value such as Twilio SendGrid Connection.
  5. Optionally, enter a Connection Description for the connection.
  6. Select the Universal API Connector.
  7. Click Submit.

To configure the Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection, in the Manage Connections page of DX Studio:

  1. Click the Edit button for the Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection. The Edit Connection wizard appears.
  2. Under Base URL, for Get value from, select Literal.
  3. Set the value of Base URL to the Twilio Sendgrid API base URL, such as https://api.sendgrid.com/v3.
  4. Under Base Headers, click Add another item.
  5. For the new header, set Header to Authorization.
  6. For the Authorization header, set Value to JS Expression.
  7. For the value of the Authorization header, enter the following expression:
`Bearer ${secret('twilio-sendgrid-token')}`
  1. Under Base Headers, click Add another item.
  2. For the new header, set Header to Content-Type.
  3. For the Content-Type header, set Value to Literal.
  4. For the value of the Content-Type header, enter the following:
application/json
  1. Click Submit.

The Twilio Send Email (twilio-send-email) Orchestration Component

To create the The Twilio Send Email (twilio-send-email) Orchestration Component, in DX Studio:

  1. In the top navigation, click Manage Experiences, and then click Components. The Manage Components page appears.
  2. Click Add Component. The Create Component wizard appears.
  3. For Component Code, enter an identifier for the component, such as twilio-send-email.
  4. For Name, enter a name for the component, such as Twilio Send Email**.
  5. Optionally, enter a Component Description.
  6. Select the No Rules checkbox. Experience Rules are not relevant to this Component.
  7. For Component Type, select Conscia - Universal API Connector. This Component Type can connect to almost any Webservice API.
  8. Click Submit.

To configure the Twilio Send Email (twilio-send-email) Orchestration Component, on the Manage Components page:

  1. Next to the Twilio Send Email (twilio-send-email) Orchestration Component, click the Edit button. The Edit Component wizard appears.
  2. For Connection, select the Twilio SendGrid Connection (twilio-sendgrid-connection) DX Engine Connection.
  3. 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 (twilio-sendgrid-connection) DX Engine Connection.
  4. For Method, select POST.
  5. 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 (twilio-send-email) Component as a Sub Component as described in Logging with Dynatrace, but using the emailMessage Context Field.

References