Templated/Formatted Emails

Hi Anvil Friends,

The attached screenshot shows an Order Review screen that I generate in our Anvil based ordering tool. I would like to recreate much of this layout and data in a formatted confirmation email to get sent to our customers. Are there any recommendations on how to generate the email template/format to accomplish this? Is there a way to export my existing form components to HTML? Sending the email itself shouldn’t be an issue.

Thank you in advance for your suggestions!

Jeff

Hi Jeff,

Good question. I’m not aware of any built-in ways of doing this directly (others may know a way). Would it be doable for you to use HTML in the body of the email?

anvil.email.send(
  to="customer@example.com",
  from_address="support",
  from_name="MyApp Support",
  subject="Welcome to MyApp",
  html="<h1>Welcome!</h1>"
)

Getting the body of the email to match the formatting on your page exactly seems like a bit of work, but that is the only method I can think of at the moment. Otherwise I suppose you could construct a media object (perhaps a PDF with PyPDF2) and send it along as an attachment.

I hope this helps.

I usually build the HTML for the email by adding each tag one by one to a list, then I join it into one string.

For more complex cases I noticed that jinja2 is available, so one could build the HTML starting from a jinja template.

Another thing you could try is to create a small HTML email that embeds an Anvil app in an iframe. The recipient will see the form being rendered by Anvil and will be able to interact with it as usual.
The form can use get_url_hash() to identify what order it is referring to (see here for details)

1 Like

The sad news about HTML-formatted emails is that most email client support a very limited subset of HTML, so it is pretty unlikely that the complicated HTML generated by an Anvil form would do what you want. (It also means that an <iframe> will not work in an email. If you wanted to load some “live” content, you would have to put a link into the email, and then have the user click the link to visit your app.)

As @stefano.menci suggests, using jinja2 to generate the HTML by hand is probably the most reliable option. (But once you’ve written the HTML, you’re going to have to test it separately with every mail client out there - GMail, Outlook, and Apple Mail are the big ones).

Oh, yes, I forgot about that!

In fact, even the joined list with simple HTML tags that I build contains links to custom URI protocols that don’t work from (some) email clients. So the first line has a normal click here for details link which opens an app that shows the same HTML in a custom HTML component.

Summary of my current process:

  • Build the HTML with one click here for details link that contains a link like https://show_email.anvil.app/#?email_id=123.
  • Store the HTML string in a table with id 123
  • Send the email with the HTML string
  • Create a show_email app that gets the HTML from the table and shows it on a form with a custom HTML component

An alternative that I have not tested could be to create an Anvil app with a form with a click here for details link that opens the app itself. The user will see the content embedded in the email, but some components will not work as expected. The click here for details link will always work, open the app and show a working form.