2014-05-06 14:01:19 -05:00
|
|
|
# Builds a Mail::Message we can use for sending. Optionally supports using a template
|
2013-06-10 15:46:08 -05:00
|
|
|
# for the body and subject
|
|
|
|
module Email
|
|
|
|
|
|
|
|
module BuildEmailHelper
|
|
|
|
def build_email(*builder_args)
|
|
|
|
builder = Email::MessageBuilder.new(*builder_args)
|
|
|
|
headers(builder.header_args) if builder.header_args.present?
|
2013-07-24 02:13:15 -05:00
|
|
|
mail(builder.build_args).tap { |message|
|
2013-07-24 14:07:43 -05:00
|
|
|
if message and h = builder.html_part
|
|
|
|
message.html_part = h
|
|
|
|
end
|
2013-07-24 02:13:15 -05:00
|
|
|
}
|
2013-06-10 15:46:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MessageBuilder
|
2013-07-22 13:20:41 -05:00
|
|
|
attr_reader :template_args
|
2013-06-10 15:46:08 -05:00
|
|
|
|
|
|
|
def initialize(to, opts=nil)
|
|
|
|
@to = to
|
|
|
|
@opts = opts || {}
|
2013-07-22 13:20:41 -05:00
|
|
|
|
2014-03-26 17:06:00 -05:00
|
|
|
@template_args = {site_name: SiteSetting.email_prefix.presence || SiteSetting.title,
|
2013-07-22 13:20:41 -05:00
|
|
|
base_url: Discourse.base_url,
|
2014-04-29 15:34:07 -05:00
|
|
|
user_preferences_url: "#{Discourse.base_url}/my/preferences" }.merge!(@opts)
|
2013-07-22 13:20:41 -05:00
|
|
|
|
|
|
|
if @template_args[:url].present?
|
2014-05-06 14:01:19 -05:00
|
|
|
if @opts[:include_respond_instructions] == false
|
|
|
|
@template_args[:respond_instructions] = ''
|
|
|
|
else
|
|
|
|
@template_args[:respond_instructions] =
|
|
|
|
if allow_reply_by_email?
|
|
|
|
I18n.t('user_notifications.reply_by_email', @template_args)
|
|
|
|
else
|
|
|
|
I18n.t('user_notifications.visit_link_to_respond', @template_args)
|
|
|
|
end
|
|
|
|
end
|
2013-07-22 13:20:41 -05:00
|
|
|
end
|
2013-06-10 15:46:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2014-09-29 00:16:55 -05:00
|
|
|
if @opts[:use_site_subject]
|
|
|
|
subject = String.new(SiteSetting.email_subject)
|
|
|
|
subject.gsub!("%{site_name}", @template_args[:site_name])
|
|
|
|
subject.gsub!("%{optional_re}", @opts[:add_re_to_subject] ? I18n.t('subject_re', template_args) : '')
|
|
|
|
subject.gsub!("%{optional_pm}", @opts[:private_reply] ? I18n.t('subject_pm', template_args) : '')
|
2014-10-03 06:44:08 -05:00
|
|
|
subject.gsub!("%{optional_cat}", @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '')
|
2014-09-29 00:16:55 -05:00
|
|
|
subject.gsub!("%{topic_title}", @template_args[:topic_title]) if @template_args[:topic_title] # must be last for safety
|
|
|
|
else
|
|
|
|
subject = @opts[:subject]
|
|
|
|
subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template]
|
|
|
|
end
|
2013-06-10 15:46:08 -05:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2013-07-24 02:13:15 -05:00
|
|
|
def html_part
|
|
|
|
return unless html_override = @opts[:html_override]
|
|
|
|
if @opts[:add_unsubscribe_link]
|
|
|
|
|
|
|
|
if response_instructions = @template_args[:respond_instructions]
|
2013-07-26 02:27:46 -05:00
|
|
|
respond_instructions = PrettyText.cook(response_instructions).html_safe
|
|
|
|
html_override.gsub!("%{respond_instructions}", respond_instructions)
|
2013-07-24 02:13:15 -05:00
|
|
|
end
|
|
|
|
|
2013-07-26 02:27:46 -05:00
|
|
|
unsubscribe_link = PrettyText.cook(I18n.t('unsubscribe_link', template_args)).html_safe
|
|
|
|
html_override.gsub!("%{unsubscribe_link}",unsubscribe_link)
|
2013-07-24 02:13:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
styled = Email::Styles.new(html_override)
|
|
|
|
styled.format_basic
|
|
|
|
|
2013-07-26 02:27:46 -05:00
|
|
|
if style = @opts[:style]
|
|
|
|
styled.send "format_#{style}"
|
|
|
|
end
|
|
|
|
|
2013-07-24 02:13:15 -05:00
|
|
|
Mail::Part.new do
|
|
|
|
content_type 'text/html; charset=UTF-8'
|
|
|
|
body styled.to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
def body
|
|
|
|
body = @opts[:body]
|
2014-02-17 18:14:35 -06:00
|
|
|
body = I18n.t("#{@opts[:template]}.text_body_template", template_args).dup if @opts[:template]
|
2013-06-10 15:46:08 -05:00
|
|
|
|
|
|
|
if @opts[:add_unsubscribe_link]
|
|
|
|
body << "\n"
|
|
|
|
body << I18n.t('unsubscribe_link', template_args)
|
|
|
|
end
|
|
|
|
|
|
|
|
body
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_args
|
2013-06-13 16:00:00 -05:00
|
|
|
{ to: @to,
|
|
|
|
subject: subject,
|
|
|
|
body: body,
|
|
|
|
charset: 'UTF-8',
|
|
|
|
from: from_value }
|
2013-06-10 15:46:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def header_args
|
|
|
|
result = {}
|
|
|
|
if @opts[:add_unsubscribe_link]
|
|
|
|
result['List-Unsubscribe'] = "<#{template_args[:user_preferences_url]}>" if @opts[:add_unsubscribe_link]
|
|
|
|
end
|
2013-06-13 09:56:16 -05:00
|
|
|
|
2013-06-18 14:54:02 -05:00
|
|
|
result['X-Discourse-Post-Id'] = @opts[:post_id].to_s if @opts[:post_id]
|
|
|
|
result['X-Discourse-Topic-Id'] = @opts[:topic_id].to_s if @opts[:topic_id]
|
2013-06-13 17:11:10 -05:00
|
|
|
|
2013-06-13 16:00:00 -05:00
|
|
|
if allow_reply_by_email?
|
2013-06-18 14:54:02 -05:00
|
|
|
result['X-Discourse-Reply-Key'] = reply_key
|
2013-06-13 16:00:00 -05:00
|
|
|
result['Reply-To'] = reply_by_email_address
|
|
|
|
else
|
|
|
|
result['Reply-To'] = from_value
|
|
|
|
end
|
2013-06-13 09:56:16 -05:00
|
|
|
|
2013-07-06 19:37:44 -05:00
|
|
|
result.merge(MessageBuilder.custom_headers(SiteSetting.email_custom_headers))
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.custom_headers(string)
|
|
|
|
result = {}
|
|
|
|
string.split('|').each { |item|
|
|
|
|
header = item.split(':', 2)
|
|
|
|
if header.length == 2
|
|
|
|
name = header[0].strip
|
|
|
|
value = header[1].strip
|
|
|
|
result[name] = value if name.length > 0 && value.length > 0
|
|
|
|
end
|
2013-07-10 08:47:38 -05:00
|
|
|
} unless string.nil?
|
2013-06-10 15:46:08 -05:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-13 16:00:00 -05:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def reply_key
|
|
|
|
@reply_key ||= SecureRandom.hex(16)
|
|
|
|
end
|
|
|
|
|
|
|
|
def allow_reply_by_email?
|
|
|
|
SiteSetting.reply_by_email_enabled? &&
|
|
|
|
reply_by_email_address.present? &&
|
|
|
|
@opts[:allow_reply_by_email]
|
|
|
|
end
|
|
|
|
|
2014-06-06 08:09:00 -05:00
|
|
|
def private_reply?
|
|
|
|
SiteSetting.reply_by_email_enabled? &&
|
|
|
|
reply_by_email_address.present? &&
|
|
|
|
@opts[:allow_reply_by_email] &&
|
|
|
|
@opts[:private_reply]
|
|
|
|
end
|
|
|
|
|
2013-06-13 16:00:00 -05:00
|
|
|
def from_value
|
|
|
|
return @from_value if @from_value
|
|
|
|
@from_value = @opts[:from] || SiteSetting.notification_email
|
|
|
|
@from_value = alias_email(@from_value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_by_email_address
|
|
|
|
return @reply_by_email_address if @reply_by_email_address
|
|
|
|
return nil unless SiteSetting.reply_by_email_address.present?
|
|
|
|
|
|
|
|
@reply_by_email_address = SiteSetting.reply_by_email_address.dup
|
|
|
|
@reply_by_email_address.gsub!("%{reply_key}", reply_key)
|
2014-06-06 08:09:00 -05:00
|
|
|
@reply_by_email_address = if private_reply?
|
|
|
|
alias_email(@reply_by_email_address)
|
|
|
|
else
|
|
|
|
site_alias_email(@reply_by_email_address)
|
|
|
|
end
|
2013-06-13 16:00:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def alias_email(source)
|
2014-07-22 14:52:14 -05:00
|
|
|
return source if @opts[:from_alias].blank? && SiteSetting.email_site_title.blank?
|
|
|
|
if !@opts[:from_alias].blank?
|
2014-08-08 12:35:25 -05:00
|
|
|
"#{Email.cleanup_alias(@opts[:from_alias])} <#{source}>"
|
2014-07-22 14:52:14 -05:00
|
|
|
else
|
2014-08-08 12:35:25 -05:00
|
|
|
"#{Email.cleanup_alias(SiteSetting.email_site_title)} <#{source}>"
|
2014-07-22 14:52:14 -05:00
|
|
|
end
|
2013-06-13 16:00:00 -05:00
|
|
|
end
|
|
|
|
|
2014-06-06 08:09:00 -05:00
|
|
|
def site_alias_email(source)
|
2014-08-08 12:35:25 -05:00
|
|
|
"#{Email.cleanup_alias(SiteSetting.email_site_title.presence || SiteSetting.title)} <#{source}>"
|
2014-06-06 08:09:00 -05:00
|
|
|
end
|
|
|
|
|
2013-06-10 15:46:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|