2013-06-03 16:12:24 -04:00
|
|
|
require_dependency 'email_styles'
|
|
|
|
|
|
|
|
class EmailRenderer
|
|
|
|
|
2013-06-06 15:08:56 -04:00
|
|
|
def initialize(message, opts=nil)
|
2013-06-03 16:12:24 -04:00
|
|
|
@message = message
|
2013-06-06 15:08:56 -04:00
|
|
|
@opts = opts || {}
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
|
|
|
@text ||= @message.body.to_s.force_encoding('UTF-8')
|
|
|
|
end
|
|
|
|
|
2013-06-06 15:08:56 -04:00
|
|
|
def logo_url
|
2013-06-05 18:23:43 -04:00
|
|
|
logo_url = SiteSetting.logo_url
|
|
|
|
if logo_url !~ /http(s)?\:\/\//
|
|
|
|
logo_url = "#{Discourse.base_url}#{logo_url}"
|
|
|
|
end
|
2013-06-06 15:08:56 -04:00
|
|
|
logo_url
|
|
|
|
end
|
2013-06-05 18:23:43 -04:00
|
|
|
|
2013-06-06 15:08:56 -04:00
|
|
|
def html
|
|
|
|
formatted_body = EmailStyles.new(PrettyText.cook(text, environment: 'email')).format
|
|
|
|
|
|
|
|
if @opts[:html_template]
|
|
|
|
ActionView::Base.new(Rails.configuration.paths["app/views"]).render(
|
|
|
|
template: 'email/template',
|
|
|
|
format: :html,
|
|
|
|
locals: { html_body: formatted_body, logo_url: logo_url }
|
|
|
|
)
|
|
|
|
else
|
|
|
|
formatted_body
|
|
|
|
end
|
2013-06-03 16:12:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|