5 Replies Latest reply on Jan 22, 2020 8:22 AM by Matt Coles

    Tip: Don't hand-code your HTML emails!

    Matt Coles

      With VizAlerts, it's easy enough to create simple HTML emails with custom links and formatting. But what if you want something that involves more than a simple hyperlink and some plain text? It's not that much fun to hand-code in HTML.


      Here's a method for building rich emails in VizAlerts that will hopefully be a little easier to manage. In this post, I'm assuming you know how to build VizAlerts already, so I'll focus just on the HTML bits.


      First, write the email you want to send in your email client (I'm using Outlook for this demonstration), then send it to yourself. Wherever you want information from your data to be inserted, use a placeholder text string. I like using all caps so it's easy to spot later.




      Once you've got it in your inbox, right-click it and pick "View Source"


      Dump it into your favorite text editor (I'm using the free Notepad++ here), and do a Find/Replace to replace all single quotes with double quotes (this step is necessary to prevent Tableau from tripping over this as a big ol' text string):



      Copy the entire thing and paste it into the Email Body * field, surrounded by the single quotes, so Tableau knows that it's a literal string:




      Once you've done that, find the placeholders that you typed into the email, and replace them with string representations of the fields you want to replace them with from your data. In our Superstore Sales data set, we've got the Item and Order Quantity fields we want to use. Note that because one is a measure and the other is a dimension, we'll have to aggregate the dimension:


      (the product quantity is replaced, and we need to aggregate "Item"):


      Set up the other required fields, making sure you're testing the alert first by ensuring Email To * is your own email address, then publish it to Tableau Server. Add the "test_alert" comment, and observe the glorious results. Our formatting is in there, with the HTML table layout we wanted, and we didn't really have to hand-code anything. DO NOTE, however, that there is a 3,000 character limit per field when it comes to VizAlerts exporting this info from Tableau Server, so for longer strings, you'll need to split it across the Email Header ~, Email Body *, and Email Footer ~ fields (yielding 9k total characters).










           BONUS ROUND!!

      Image result for sf2 bonus round



      We don't want one email per item. For a given customer, we want a single email with all their products listed with the respective quantities over our threshold. Thankfully, we can do that pretty simply using the Consolidate Lines ~ field.


      First, edit the Email Body * field again, and add a table row tag right in front of your field section (we already have a table in our code, so I just backed the cursor up until I found the start tag, then hit Enter to move it down, and made it it's own string).



      Do the same for the end of the row:



      Now that we know where the beginning and end of our table row is, everything that comes before it can be moved to the Email Header ~ field, and everything after can go into the Email Footer ~ field.



      Don't forget to add the header, footer, and Consolidate Lines ~ fields to the viz!



      And...shablam! Note that I added the email address for the customer to the Email Subject * field, because Consolidate Lines uses the recipient list and subject to determine when to break up the consolidation of data. Doing that allows me to see who each email would be sent to, and breaks up the consolidation appropriately, all without me actually sending the data to people who aren't me.