Customizing the Discourse outer email template

:bookmark: This guide explains how to customize and style the outer HTML template for all emails sent from your Discourse site.

:person_raising_hand: Required user level: Administrator

All emails sent from your site are wrapped in an HTML template that can be customized.

This guide covers:

  • Accessing the email style customization section
  • Modifying the HTML template
  • Adding custom CSS
  • Testing email templates
  • Reverting changes
  • A custom template example
  • Interaction with digest emails
  • Changing email fonts

Accessing the email style customization

  1. Navigate to Admin / Customize / Email Style in your Discourse site.
  2. You’ll see an editor where you can modify the HTML and CSS for your email template.

Modifying the HTML template

The default markup in the editor contains an %{email_content} placeholder in the body section. This placeholder is where the content of each email will be inserted. You can make any changes to the template as long as you don’t remove this placeholder.

For information on customizing the content that replaces the %{email_content} placeholder, see how to customize specific email templates.

Adding custom CSS

You can use the CSS section of the Email Style editor to inject inline styles into your template. Here’s an example:

  1. Add a CSS class to an HTML element in the template:

    <td class="my-outer-td">
    </td>
    
  2. Create a rule for the class in the CSS editor:

    .my-outer-td {
      padding: 30px 0 30px 0;
    }
    

Discourse will replace the CSS class with the actual CSS rule before sending the email. You can add any CSS rules you like here.

Testing email templates

It’s crucial to test your email template changes across various email clients, as they may interpret HTML differently. Use an email testing service to preview how your emails will appear on different clients, especially for significant changes.

Reverting changes

To revert your template customization:

  1. Scroll to the bottom of the template editor.
  2. Click the “Reset to default” button.

This action will replace your changes with the default Discourse email template.

Custom template example

Here’s a basic email template that constrains the width of emails to a maximum of 600px and adds a logo to the top:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  </head>
  <body style="margin: 0; padding: 0;">
    <!--[if mso]>
    <style type="text/css">
      body, table, td, th, h1, h2, h3 { font-family: Helvetica, Arial, sans-serif !important; }
    </style>
    <![endif]-->
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
        <td style="padding: 10px 0 30px 0;">
          <!--[if gte mso 9]>
          <table width="600" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td>
          <![endif]-->
          <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;border-collapse: collapse;">
            <tr>
              <td style="padding: 20px 15px 20px 15px;">
                <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%">
                  <tr>
                    <td align="center">
                      <a href="https://forum.example.com">
                        <img src="https://your-site-logo-url" alt="Your site name" width="150" height="40" style="display: block;"/>
                      </a>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
            <tr>
              <td style="padding: 0 15px 0 15px;">
                <hr style="background-color: #ddd; height: 1px; border: 1px;">
              </td>
            </tr>
            <tr>
              <td style="padding: 20px 15px 30px 15px; color: #153643; font-family: Helvetica, Arial, sans-serif; font-size: 16px; line-height: 20px;">
                %{email_content}
              </td>
            </tr>
          </table>
          <!--[if gte mso 9]>
              </td>
            </tr>
          </table>
          <![endif]-->
        </td>
      </tr>
    </table>
  </body>
</html>

Remember to replace the image src, alt, and href attributes with your site’s specific information.

Here's how it looks on different screen sizes

iPad Pro (10.5 inch) (iOS 13.1):

Gmail (Chrome):

iPhone 11 Pro Max (iOS 13.1):

Outlook 2019 (Windows 10)

Interaction with digest emails

If your custom template adds a logo to all emails, you have two options for digest emails:

  1. To use the default digest email styling:

    • Disable the apply custom styles to digest site setting.
  2. To maintain consistent styling across all notification emails:

    • Remove any images set as the digest logo site setting.
    • Add <span></span> to the text customization at .../admin/customize/site_texts/user_notifications.digest.custom.html.header.

Changing email fonts

To change fonts in your emails:

  1. Use web-safe fonts for maximum compatibility. Refer to CSS Font Stack for a list of available fonts on Windows and Mac.

  2. Add CSS rules for the fonts to your Discourse email template.

  3. You can set fonts directly in HTML elements, for example:

    <td style="font-family: Helvetica, Arial, sans-serif; font-size: 16px; line-height: 20px;">
      %{email_content}
    </td>
    

Note that web fonts from external services are not supported by many email clients.

Additional resources

For more information on creating HTML emails, you can refer to this comprehensive guide on building an HTML email template from scratch.

Last edited by @hugh 2024-07-30T01:58:08Z

Last checked by @hugh 2024-07-30T01:58:15Z

Check documentPerform check on document:
18 Likes

Thank @simon!

Could you be so kind to provide a full text of template? I would be happy to try it.

3 Likes

Thanks for pointing that out. It must have been a copy/paste error when I created the topic. I’ve added the full template to the post now.

3 Likes

How can we get the logo_url from Discourse in this template? is this possible?

1 Like

The easiest way might be to open your browser’s web inspector on your DIscourse site and copy the value of the logo’s src attribute into the template. You could also get the logo URL from the rails console by looking at the value of the url property that is returned for SiteSetting.logo

4 Likes

Ok cool, this is how I’m doing it right now, thanks!

2 Likes

Very nice. I think this should be the default. Most sites use a centered column 600px wide.

Seems like there should be a separate wrapper to edit for digest emails.

2 Likes

What’s the best way to change the font in the emails? Thanks.

1 Like

I think the safest approach would be to use web safe fonts. A good listing of which fonts are available on Windows and Mac computers is here: https://www.cssfontstack.com/. It should be enough to add CSS rules for the fonts to your Discourse email template.

You could try pulling in web fonts from an external service for your emails, but you need to be aware that many email clients do not support web fonts. Some details about that are here: Web Fonts: How to Make Them Work Perfectly in Email - Litmus.

3 Likes

Thanks. I’d use a web-safe font. In the example above there’s a [if mso] part but I can’t see where the font for non-Outlook clients is set.

1 Like

In the example template, fonts for non-Outlook email clients are being set with CSS rules that are added directly to HTML elements. For example:

<td style="padding: 20px 15px 30px 15px; color: #153643; font-family: Helvetica, Arial, sans-serif; font-size: 16px; line-height: 20px;">
    %{email_content}
 </td>
2 Likes

So they are. I’ll blame the narrow mobile screen and leave it at that :slight_smile:

2 Likes

Is it possible to customise the font of the text beside the avatar? I mean, the text for:

[Real name] [Username]
[Date]

I’ve been able to change the rest of the email’s text but the text above has the following style:

style=“color:#006699;; font-size:13px;font-family:‘lucida grande’,tahoma,verdana,arial,sans-serif;text-decoration:none;font-weight:bold; text-decoration: none; font-weight: bold; color: #006699;”>

1 Like

Are there any other variables available in the email template?

For example, it would be useful to have %{year} for avoiding the need to update email footers. Is that available?

1 Like