2013-06-11 04:46:08 +08:00
|
|
|
# Builds a Mail::Mesage we can use for sending. Optionally supports using a template
|
|
|
|
# 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?
|
|
|
|
mail(builder.build_args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MessageBuilder
|
|
|
|
|
|
|
|
def initialize(to, opts=nil)
|
|
|
|
@to = to
|
|
|
|
@opts = opts || {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
|
|
|
subject = @opts[:subject]
|
|
|
|
subject = I18n.t("#{@opts[:template]}.subject_template", template_args) if @opts[:template]
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
body = @opts[:body]
|
|
|
|
body = I18n.t("#{@opts[:template]}.text_body_template", template_args) if @opts[:template]
|
|
|
|
|
|
|
|
if @opts[:add_unsubscribe_link]
|
|
|
|
body << "\n"
|
|
|
|
body << I18n.t('unsubscribe_link', template_args)
|
|
|
|
end
|
|
|
|
|
|
|
|
body
|
|
|
|
end
|
|
|
|
|
|
|
|
def template_args
|
|
|
|
@template_args ||= { site_name: SiteSetting.title,
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
user_preferences_url: "#{Discourse.base_url}/user_preferences" }.merge!(@opts)
|
2013-06-25 23:27:17 +08:00
|
|
|
|
|
|
|
if @template_args[:url].present?
|
|
|
|
if allow_reply_by_email? and
|
|
|
|
@template_args[:respond_instructions] = I18n.t('user_notifications.reply_by_email', @template_args)
|
|
|
|
else
|
|
|
|
@template_args[:respond_instructions] = I18n.t('user_notifications.visit_link_to_respond', @template_args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@template_args
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_args
|
2013-06-14 05:00:00 +08:00
|
|
|
{ to: @to,
|
|
|
|
subject: subject,
|
|
|
|
body: body,
|
|
|
|
charset: 'UTF-8',
|
|
|
|
from: from_value }
|
2013-06-11 04:46:08 +08: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 22:56:16 +08:00
|
|
|
|
2013-06-19 03:54:02 +08: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-14 06:11:10 +08:00
|
|
|
|
2013-06-14 05:00:00 +08:00
|
|
|
if allow_reply_by_email?
|
2013-06-19 03:54:02 +08:00
|
|
|
result['X-Discourse-Reply-Key'] = reply_key
|
2013-06-14 05:00:00 +08:00
|
|
|
result['Reply-To'] = reply_by_email_address
|
|
|
|
else
|
|
|
|
result['Reply-To'] = from_value
|
|
|
|
end
|
2013-06-13 22:56:16 +08:00
|
|
|
|
2013-07-07 08:37:44 +08: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 21:47:38 +08:00
|
|
|
} unless string.nil?
|
2013-06-11 04:46:08 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-14 05:00:00 +08: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
|
|
|
|
|
|
|
|
def from_value
|
|
|
|
return @from_value if @from_value
|
|
|
|
@from_value = @opts[:from] || SiteSetting.notification_email
|
|
|
|
@from_value = alias_email(@from_value)
|
|
|
|
@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)
|
|
|
|
@reply_by_email_address = alias_email(@reply_by_email_address)
|
|
|
|
|
|
|
|
@reply_by_email_address
|
|
|
|
end
|
|
|
|
|
|
|
|
def alias_email(source)
|
|
|
|
return source if @opts[:from_alias].blank?
|
|
|
|
"#{@opts[:from_alias]} <#{source}>"
|
|
|
|
end
|
|
|
|
|
2013-06-11 04:46:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|