Personalización de la plantilla de correo electrónico externo de Discourse

: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 Me gusta

Thank @simon!

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

3 Me gusta

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 Me gusta

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

1 me gusta

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 Me gusta

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

2 Me gusta

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 Me gusta

¿Cuál es la mejor manera de cambiar la fuente en los correos electrónicos? Gracias.

1 me gusta

Creo que el enfoque más seguro sería usar fuentes seguras para la web. Aquí hay una buena lista de las fuentes disponibles en computadoras Windows y Mac: https://www.cssfontstack.com/. Debería ser suficiente para agregar reglas CSS para las fuentes a su plantilla de correo electrónico de Discourse.

Podría intentar incorporar fuentes web de un servicio externo para sus correos electrónicos, pero debe tener en cuenta que muchos clientes de correo electrónico no admiten fuentes web. Aquí hay algunos detalles al respecto: The Ultimate Guide to Web Safe Fonts for Email Marketing | Litmus.

3 Me gusta

Gracias. Usaría una fuente segura para la web. En el ejemplo anterior hay una parte [if mso] pero no veo dónde se establece la fuente para los clientes que no son de Outlook.

1 me gusta

En la plantilla de ejemplo, las fuentes para clientes de correo electrónico que no son de Outlook se establecen con reglas CSS que se añaden directamente a los elementos HTML. Por ejemplo:

<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 Me gusta

Así es. Culparé a la estrecha pantalla del móvil y lo dejaré ahí :slight_smile:

2 Me gusta

¿Es posible personalizar la fuente del texto junto al avatar? Me refiero al texto de:

[Nombre real] [Nombre de usuario]
[Fecha]

He podido cambiar el resto del texto del correo electrónico, pero el texto de arriba tiene el siguiente estilo:

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 me gusta

¿Hay otras variables disponibles en la plantilla de correo electrónico?

Por ejemplo, sería útil tener %{year} para evitar la necesidad de actualizar los pies de página de los correos electrónicos. ¿Está disponible?

1 me gusta