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