FEATURE: customization of html emails (#7934)

This feature adds the ability to customize the HTML part of all emails using a custom HTML template and optionally some CSS to style it. The CSS will be parsed and converted into inline styles because CSS is poorly supported by email clients. When writing the custom HTML and CSS, be aware of what email clients support. Keep customizations very simple.

Customizations can be added and edited in Admin > Customize > Email Style.

Since the summary email is already heavily styled, there is a setting to disable custom styles for summary emails called "apply custom styles to digest" found in Admin > Settings > Email.

As part of this work, RTL locales are now rendered correctly for all emails.
This commit is contained in:
Neil Lalonde
2019-07-30 15:05:08 -04:00
committed by GitHub
parent 340173eb12
commit 9656a21fdb
39 changed files with 720 additions and 134 deletions

View File

@@ -12,6 +12,7 @@ class UserNotifications < ActionMailer::Base
include ApplicationHelper
helper :application, :email
default charset: 'UTF-8'
layout 'email_template'
include Email::BuildEmailHelper
@@ -362,11 +363,6 @@ class UserNotifications < ActionMailer::Base
result
end
class UserNotificationRenderer < ActionView::Base
include UserNotificationsHelper
include EmailHelper
end
def self.get_context_posts(post, topic_user, user)
if (user.user_option.email_previous_replies == UserOption.previous_replies_type[:never]) ||
SiteSetting.private_email?
@@ -580,15 +576,7 @@ class UserNotifications < ActionMailer::Base
site_description: SiteSetting.site_description
)
unless translation_override_exists
html = UserNotificationRenderer.with_view_paths(Rails.configuration.paths["app/views"]).render(
template: 'email/invite',
format: :html,
locals: { message: PrettyText.cook(message, sanitize: false).html_safe,
classes: Rtl.new(user).css_class
}
)
end
html = PrettyText.cook(message, sanitize: false).html_safe
else
reached_limit = SiteSetting.max_emails_per_day_per_user > 0
reached_limit &&= (EmailLog.where(user_id: user.id)
@@ -608,7 +596,6 @@ class UserNotifications < ActionMailer::Base
end
unless translation_override_exists
html = UserNotificationRenderer.with_view_paths(Rails.configuration.paths["app/views"]).render(
template: 'email/notification',
format: :html,
@@ -651,7 +638,6 @@ class UserNotifications < ActionMailer::Base
site_description: SiteSetting.site_description,
site_title: SiteSetting.title,
site_title_url_encoded: URI.encode(SiteSetting.title),
style: :notification,
locale: locale
}
@@ -689,13 +675,6 @@ class UserNotifications < ActionMailer::Base
@anchor_color = ColorScheme.hex_for_name('tertiary')
@markdown_linker = MarkdownLinker.new(@base_url)
@unsubscribe_key = UnsubscribeKey.create_key_for(@user, "digest")
end
def apply_notification_styles(email)
email.html_part.body = Email::Styles.new(email.html_part.body.to_s).tap do |styles|
styles.format_basic
styles.format_notification
end.to_html
email
@disable_email_custom_styles = !SiteSetting.apply_custom_styles_to_digest
end
end