discourse/lib/email/renderer.rb
Neil Lalonde 9656a21fdb
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.
2019-07-30 15:05:08 -04:00

40 lines
927 B
Ruby

# frozen_string_literal: true
require_dependency 'email/styles'
module Email
class Renderer
def initialize(message, opts = nil)
@message = message
@opts = opts || {}
end
def text
return @text if @text
@text = (+(@message.text_part ? @message.text_part : @message).body.to_s).force_encoding('UTF-8')
@text = CGI.unescapeHTML(@text)
end
def html
style = if @message.html_part
Email::Styles.new(@message.html_part.body.to_s, @opts)
else
unstyled = UserNotificationRenderer.with_view_paths(
Rails.configuration.paths["app/views"]
).render(
template: 'layouts/email_template',
format: :html,
locals: { html_body: PrettyText.cook(text).html_safe }
)
Email::Styles.new(unstyled, @opts)
end
style.format_basic
style.format_html
style.to_html
end
end
end