تخصيص قالب البريد الإلكتروني الخارجي لنظام 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 إعجابًا

Thank @simon!

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

3 إعجابات

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 إعجابات

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

إعجاب واحد (1)

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 إعجابات

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

إعجابَين (2)

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)

ما هي أفضل طريقة لتغيير الخط في رسائل البريد الإلكتروني؟ شكراً.

إعجاب واحد (1)

أعتقد أن النهج الأكثر أمانًا هو استخدام خطوط الويب الآمنة. قائمة جيدة بالخطوط المتوفرة على أجهزة الكمبيوتر التي تعمل بنظام Windows و Mac موجودة هنا: https://www.cssfontstack.com/. يجب أن يكون كافيًا لإضافة قواعد CSS للخطوط إلى قالب البريد الإلكتروني الخاص بـ Discourse.

يمكنك محاولة سحب خطوط الويب من خدمة خارجية لرسائل البريد الإلكتروني الخاصة بك، ولكن يجب أن تكون على دراية بأن العديد من عملاء البريد الإلكتروني لا يدعمون خطوط الويب. بعض التفاصيل حول ذلك موجودة هنا: The Ultimate Guide to Web Safe Fonts for Email Marketing | Litmus.

3 إعجابات

شكرا لك. سأستخدم خطًا آمنًا للويب. في المثال أعلاه يوجد جزء [if mso] ولكني لا أرى أين تم تعيين الخط للعملاء غير Outlook.

إعجاب واحد (1)

في القالب المثال، يتم تعيين الخطوط لعملاء البريد الإلكتروني غير Outlook باستخدام قواعد CSS التي تتم إضافتها مباشرة إلى عناصر HTML. على سبيل المثال:

<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)

إذن هم كذلك. سألقي باللوم على شاشة الهاتف المحمول الضيقة وأكتفي بذلك :slight_smile:

إعجابَين (2)

هل من الممكن تخصيص خط النص بجوار الصورة الرمزية؟ أعني، النص الخاص بـ:

[الاسم الحقيقي] [اسم المستخدم]
[التاريخ]

لقد تمكنت من تغيير بقية نص البريد الإلكتروني ولكن النص أعلاه له النمط التالي:

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)

هل هناك أي متغيرات أخرى متاحة في قالب البريد الإلكتروني؟

على سبيل المثال، سيكون من المفيد الحصول على %{year} لتجنب الحاجة إلى تحديث تذييلات البريد الإلكتروني. هل هذا متاح؟

إعجاب واحد (1)