Configuring transactional mails via HTTP API

This topic is for administrators and developers with administration access rights in Magento 2.

This topic describes how to configure sending of transactional mails An email triggered by a recipient action (such as an order or purchase) or event (such as an anniversary). It is sent out subsequent to the event. via HTTP API with Magento 2. For transactional mails via the SMTP API, see Transactional mails via SMTP API.

How it works

Sending of transactional mails via HTTP API uses these templates:

  • A Sender template in Magento 2, which sends the variables of the transaction (for example customer name, ordered products) via the HTTP API.
  • A Recipient template in Optimizely Campaign, which uses field functions to insert sent variables into relevant locations. This creates the finished transactional mail with layout.

Sending and receiving the content of the variables takes place using the transaction recipient list as a buffer.

  1. The Magento 2 template sends the variables.
  2. The variables are written to the transaction recipient list: each variable into the relevant recipient list field.
  3. Using field functions, Optimizely Campaign copies the template with the individual variables from the transaction recipient list and places it in the desired location in the transactional mail.

    Image: Sending and receiving content of variables

Configuration steps

  1. Set up a transaction recipient list.
  2. Create the template in Optimizely Campaign.
  3. Create and activate the template in Magento 2.
  4. Configure transactional mails and transfer Magento 2 variables.

Step 1. Setting up a transaction recipient list

To set up the transaction recipient list. contact customer support. Plan in advance which variables you want to transfer from Magento 2 into Optimizely Campaign for your transactional mails.

List the Magento 2 variables for which you require a corresponding recipient list field in your transaction recipient list. Also, let Optimizely know the naming of the recipient list fields.

The scope of the transaction recipient list is limited. For this reason, you should request generic names for the recipient list fields, for example custom_string_1, custom_string_2, custom_date_1 and so on.

Step 2. Creating a template in Optimizely Campaign

Create the template and add the desired transactional mail texts (the unchangeable, static texts that are to be sent to recipients) and insert field functions in the places where the content of the Magento 2 variables should be.

Field functions

Field functions are placeholders that refer to a specific recipient list field in your transaction recipient list. The variables and content of the variables sent from Magento 2 are written into the relevant recipient list fields in your transaction recipient list. The field functions you place load relevant content from the referenced recipient list field in the next step.

Example

Set up a transactional mail text that greets each customer using the last name and the order date. For this you need these variables: salutation, lastname and orderdate.

The content of the variables is first written to a relevant recipient list field of the transaction recipient list (from Magento 2 via the HTTP API). To access these recipient list fields, insert the individual field functions into the Optimizely Campaign template and place the name of the recipient list field in curly brackets. To import the variable contents, the static text and field functions in the Optimizely Campaign template could look like this:

Hello, {salutation} {lastname}! 
Thank you for your online order from {orderdate}.

The example will appear in the sent transactional mail as follows:

Hello, Mr. Miller!
Thank you for your online order from August 3.

Adding static texts and field functions to a template

  1. Open the Optimizely Campaign menu and select Campaigns > Transactional Mails.
  2. Click Create.

    Images: Create transactional mail

  3. From the left action pane, drag the Recipients node Campaign: Item that determines when a recipient passes through the campaign, and the actions that are executed for the recipient. With the help of "connections", you can link nodes to specify the process of your campaign. The arrow at the end of each connection always points to the next node and the associated action. into the workspace.
  4. In the Recipient lists drop-down list in the context menu, select your transaction recipient list.

    Images: Recipient list options

    Customer support has set up a separate transaction recipient list for you. Make sure you use this transaction recipient list, instead of a regular recipient list. To see if a recipient list is a transactional recipient list, check the column Transaction API.

  5. From the left sidebar, drag the message node for the desired marketing channel Channel through which advertising messages or information are transmitted to customers and prospects. For example, email, SMS, print or push. (email, SMS) into the workspace.
  6. In the Name box in the context menu, enter a name for your transactional mail (for example Order confirmation).

    Images: Order confirmation email node

  7. In the context menu for the message node, click Edit content.
  8. In the Template list, select the desired template and confirm your selection by clicking Next.
  9. Configure the template for the transactional mail according to your requirements. Enter a subject and insert the static texts that recipients will receive, into the content paragraphs. Insert the relevant field function for the recipient list field of the transaction recipient list into places where variables from the Magento 2 template should appear (see example above).
  10. Click Apply.
  11. Click Close.
  12. Connect the Recipients node with the message node.
  13. Click Save and Close.
  14. Select the mailing in the overview and click Start to activate your transactional mail and to start the sending process.

Step 3. Creating and activating the template in Magento 2

Create a template in Magento 2 that sends the variables to Optimizely Campaign via HTTP API.

Prerequisites

  • Authorization code. For your transaction recipient list in to Optimizely Campaign.

    Open the Optimizely Campaign menu and select AdministrationAPI Overview > Recipient lists tab and select the desired transaction recipient list. Click Manage authorization codes and copy the authorization code from the list. If no authorization code is available for the selected recipient list, click Create authorization code.

  • Mailing ID. For the template in Optimizely Campaign.

    Open the Optimizely Campaign menu and select Campaigns > Transactional Mails. Copy the relevant mailing ID of the template in the list in column ID.

Configuration options

With Magento 2, you have two template configuration options for transferring variables:

  1. Use a template included in the standard Magento 2 installation and customize it to fit your needs. If you choose this option, the template's HTML code loads in the template content field. The code contains the available variables, for example {{trans "%name," name=$customer.name}} (for addressing customers by their names). Clean up the template around the HTML code and reduce the template to the variables. In the next step, map the corresponding recipient list fields of your transaction recipient list in Optimizely Campaign. See Mapping: Here’s how you do it.
  2. Create an empty template and manually insert the variables to be sent to Optimizely Campaign via the HTTP API.

Do the following to create a template, add the variables and map these to the recipient list fields of your transaction recipient list.

  1. Log in to Magento 2 with administrator rights.
  2. Select Marketing > Email Templates.

    Images: Email templates

    If you already created templates that are sent to Optimizely Campaign, these appear as the template type Episerver.

    Images: Select email template

  3. Click Add New Template.

    Images: Add new template

  4. In the Template Information area, fill in the fields:
    • Template Name. Enter a name for the new template.
    • Template Subject. Leave empty.

      The subject is automatically entered later using the subject from the template created in Optimizely Campaign.

    • Episerver Campaign. Select Enabled.
    • Authcorisation Code. Enter the authorization code for the transaction recipient list in Optimizely Campaign.
    • bmMailingId. Enter the mailing ID of the template in Optimizely Campaign.

    Images: Template information

  5. In the Template Information box, enter which variables from Magento 2 should be transferred to which recipient list field of the transaction recipient list in Optimizely Campaign. To add a variable, click Insert Variable...

    Images: Insert variable

  6. Select the desired variable by clicking it.

    Images: Select variable

    Images: Inserted variable

  7. Map the inserted variable with the corresponding recipient list field in Optimizely Campaign. If required, click Insert Variable... to add further variables.
  8. Click Save Template.
  9. After you create the template, activate it and map an event (for example an order process, or newsletter registration). At which point in the admin interface you map and activate the template to an event depends on the type of the event.

    Example: To map and activate the template to the event type Order, do the following:

    1. Select Stores > Configuration.
    2. Select Sales > Sales Emails.
    3. Activate the template by selecting it in the Order area in the New Order Confirmation Template drop-down list.
    4. Save your changes.

Step 4. Configuring transactional mails and transferring Magento 2 variables

To transfer customer orders and billing and delivery addresses to the template in Optimizely Campaign, using the template in Magento 2, you need additional configuration, and create a special field function (for order access) in your Optimizely Campaign client.

Magento 2 provides the customer orders and billing and delivery addresses as HTML code. This needs to be changed to simple text so that Optimizely Campaign can process the information.

Converting HTML code to simple text

The following functions in the Magento 2 integration are for converting the HTML code into simple text:

getEpiserverBillingData( )

With getEpiserverBillingData( ), you get access to the individual parameters of the billing address. This feature is available via the following instruction, used in an example for the salutation of the customer:

{{var order.getEpiserverBillingData('salutation')}}

You transfer the return value of the function call (parameter salutation) into the recipient list field salutation as simple text with the following mapping:

salutation={{var order.getEpiserverBillingData('salutation')}}

getEpiserverShippingData()

With getEpiserverShippingData( ), you receive access to the individual parameters of the customer's delivery address. The feature is available via the following mapping, in this example for the customer's last name:

{{var order.getEpiserverShippingData('lastname')}}

You transfer the return value of the function call (parameter lastname) into the recipient list field lastname as simple text with the following mapping:

lastname={{var order.getEpiserverShippingData('lastname')}}

getEpiserverOrderData()

With getEpiserverOrderData( ), you access the customer's orders.

Accessing orders differs from accessing the billing and the delivery addresses, as you transfer a list with several parameters into a single recipient list field, instead of transferring individual parameters.

The function is available via the following instruction (in this example, product ID, storage unit, product name, quantity ordered and price):

{{var order.getEpiserverOrderData('product_id','sku','name','qty_ordered',
'price')}}

You transfer a string with the order information specified in the order_positions_csv into the recipient list field with the following mapping:

order_positions_csv={{var order.getEpiserverOrderData('product_id',
'sku','name','qty_ordered','price')}}

After the transfer to Optimizely Campaign, the individual parameters in the recipient list field of your transaction recipient list are separated by a semicolon. If multiple products are transferred, the individual products are separated by a line break. The string written in the recipient list field corresponds to the structure of a table that is saved in the CSV format.

Creating a special field function for order data

If you want to use order data in a transactional mail, you must create a new special field function in your Optimizely Campaign client. This must use the template engine of Optimizely Campaign to transfer the string to HTML, as described in the following.

  1. Open the Optimizely Campaign menu and select Administration > Field Functions.

    If the Field functions menu item is not available in your client, contact customer support.

  2. Click Add.

    Images: Add field function

  3. Under General, in the Name box, add a concise name for the new field function, for example CSVOrderObjects.

    Images: Enter name

  4. In the Type drop-down list, select Velocity.
  5. In the Content area > Default replacement tab > Default replacement box, enter the following code:
    <table>
    <tr>
      <td>Product ID</td><td>SKU</td><td>Name</td><td>Quantity</td>
      <td>Price</td>
    </tr>
    #set ("csvContent = "csv.readFromString("user.data.order_positions_csv))
    #foreach("row in "csvContent)
    <tr>
    #foreach("item in "row) <td> "item </td>
    #end </tr> #end </table>

    Images: Default replacement code

  6. Click Save.

    Insert the field function at the desired location in the Optimizely Campaign template. When the transactional mail An email triggered by a recipient action (such as an order or purchase) or event (such as an anniversary). It is sent out subsequent to the event. is sent and the recipient list field order_positions_csv contains a CSV context object, a simple HTML table is displayed in the sent transactional mail.

    See Field functions how to customize the layout of the table as desired (HTML knowledge is required).