2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-05-06 15:01:19 -04:00
|
|
|
# Builds a Mail::Message we can use for sending. Optionally supports using a template
|
2013-06-10 16:46:08 -04:00
|
|
|
# for the body and subject
|
|
|
|
module Email
|
|
|
|
class MessageBuilder
|
2013-07-22 14:20:41 -04:00
|
|
|
attr_reader :template_args
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2020-04-30 02:48:34 -04:00
|
|
|
ALLOW_REPLY_BY_EMAIL_HEADER = 'X-Discourse-Allow-Reply-By-Email'
|
2018-07-18 04:28:44 -04:00
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
def initialize(to, opts = nil)
|
|
|
|
@to = to
|
|
|
|
@opts = opts || {}
|
2013-07-22 14:20:41 -04:00
|
|
|
|
2015-08-12 17:00:16 -04:00
|
|
|
@template_args = {
|
2017-03-21 09:11:15 -04:00
|
|
|
site_name: SiteSetting.title,
|
|
|
|
email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
|
2015-08-12 17:00:16 -04:00
|
|
|
base_url: Discourse.base_url,
|
|
|
|
user_preferences_url: "#{Discourse.base_url}/my/preferences",
|
2016-06-03 09:48:54 -04:00
|
|
|
hostname: Discourse.current_hostname,
|
2015-08-12 17:00:16 -04:00
|
|
|
}.merge!(@opts)
|
2013-07-22 14:20:41 -04:00
|
|
|
|
|
|
|
if @template_args[:url].present?
|
2017-12-05 15:12:18 -05:00
|
|
|
@template_args[:header_instructions] ||= I18n.t('user_notifications.header_instructions', @template_args)
|
2015-12-07 16:52:10 -05:00
|
|
|
|
2014-05-06 15:01:19 -04:00
|
|
|
if @opts[:include_respond_instructions] == false
|
|
|
|
@template_args[:respond_instructions] = ''
|
2018-06-11 18:54:39 -04:00
|
|
|
@template_args[:respond_instructions] = I18n.t('user_notifications.pm_participants', @template_args) if @opts[:private_reply]
|
2014-05-06 15:01:19 -04:00
|
|
|
else
|
2016-02-26 17:56:56 -05:00
|
|
|
if @opts[:only_reply_by_email]
|
2019-05-02 18:17:27 -04:00
|
|
|
string = +"user_notifications.only_reply_by_email"
|
2018-06-11 18:54:39 -04:00
|
|
|
string << "_pm" if @opts[:private_reply]
|
2015-08-12 17:00:16 -04:00
|
|
|
else
|
2019-05-02 18:17:27 -04:00
|
|
|
string = allow_reply_by_email? ? +"user_notifications.reply_by_email" : +"user_notifications.visit_link_to_respond"
|
2016-02-26 17:56:56 -05:00
|
|
|
string << "_pm" if @opts[:private_reply]
|
2015-08-12 17:00:16 -04:00
|
|
|
end
|
2017-02-14 08:58:08 -05:00
|
|
|
@template_args[:respond_instructions] = "---\n" + I18n.t(string, @template_args)
|
2014-05-06 15:01:19 -04:00
|
|
|
end
|
2016-06-03 09:48:54 -04:00
|
|
|
|
|
|
|
if @opts[:add_unsubscribe_link]
|
|
|
|
unsubscribe_string = if @opts[:mailing_list_mode]
|
|
|
|
"unsubscribe_mailing_list"
|
|
|
|
elsif SiteSetting.unsubscribe_via_email_footer
|
|
|
|
"unsubscribe_link_and_mail"
|
|
|
|
else
|
|
|
|
"unsubscribe_link"
|
|
|
|
end
|
|
|
|
@template_args[:unsubscribe_instructions] = I18n.t(unsubscribe_string, @template_args)
|
|
|
|
end
|
2013-07-22 14:20:41 -04:00
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
2020-01-29 14:48:55 -05:00
|
|
|
if @opts[:template] &&
|
|
|
|
TranslationOverride.exists?(locale: I18n.locale, translation_key: "#{@opts[:template]}.subject_template")
|
2020-11-02 23:00:11 -05:00
|
|
|
augmented_template_args = @template_args.merge({
|
|
|
|
site_name: @template_args[:email_prefix],
|
|
|
|
optional_re: @opts[:add_re_to_subject] ? I18n.t('subject_re') : '',
|
|
|
|
optional_pm: @opts[:private_reply] ? @template_args[:subject_pm] : '',
|
|
|
|
optional_cat: @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '',
|
|
|
|
optional_tags: @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : '',
|
|
|
|
topic_title: @template_args[:topic_title] ? @template_args[:topic_title] : '',
|
|
|
|
})
|
|
|
|
subject = I18n.t("#{@opts[:template]}.subject_template", augmented_template_args)
|
2020-01-29 14:48:55 -05:00
|
|
|
elsif @opts[:use_site_subject]
|
2014-09-29 01:16:55 -04:00
|
|
|
subject = String.new(SiteSetting.email_subject)
|
2017-06-21 11:22:10 -04:00
|
|
|
subject.gsub!("%{site_name}", @template_args[:email_prefix])
|
2019-01-18 04:52:48 -05:00
|
|
|
subject.gsub!("%{optional_re}", @opts[:add_re_to_subject] ? I18n.t('subject_re') : '')
|
2018-02-19 04:20:17 -05:00
|
|
|
subject.gsub!("%{optional_pm}", @opts[:private_reply] ? @template_args[:subject_pm] : '')
|
2014-10-03 07:44:08 -04:00
|
|
|
subject.gsub!("%{optional_cat}", @template_args[:show_category_in_subject] ? "[#{@template_args[:show_category_in_subject]}] " : '')
|
2018-07-10 22:24:07 -04:00
|
|
|
subject.gsub!("%{optional_tags}", @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : '')
|
2014-09-29 01:16:55 -04:00
|
|
|
subject.gsub!("%{topic_title}", @template_args[:topic_title]) if @template_args[:topic_title] # must be last for safety
|
2019-01-18 04:52:48 -05:00
|
|
|
elsif @opts[:use_topic_title_subject]
|
|
|
|
subject = @opts[:add_re_to_subject] ? I18n.t('subject_re') : ''
|
|
|
|
subject = "#{subject}#{@template_args[:topic_title]}"
|
|
|
|
elsif @opts[:template]
|
|
|
|
subject = I18n.t("#{@opts[:template]}.subject_template", @template_args)
|
2014-09-29 01:16:55 -04:00
|
|
|
else
|
|
|
|
subject = @opts[:subject]
|
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
subject
|
|
|
|
end
|
|
|
|
|
2013-07-24 03:13:15 -04:00
|
|
|
def html_part
|
|
|
|
return unless html_override = @opts[:html_override]
|
|
|
|
|
2016-06-03 09:48:54 -04:00
|
|
|
if @template_args[:unsubscribe_instructions].present?
|
|
|
|
unsubscribe_instructions = PrettyText.cook(@template_args[:unsubscribe_instructions], sanitize: false).html_safe
|
|
|
|
html_override.gsub!("%{unsubscribe_instructions}", unsubscribe_instructions)
|
2015-11-24 10:58:26 -05:00
|
|
|
else
|
2016-06-03 09:48:54 -04:00
|
|
|
html_override.gsub!("%{unsubscribe_instructions}", "")
|
2015-11-24 10:58:26 -05:00
|
|
|
end
|
|
|
|
|
2016-06-03 09:48:54 -04:00
|
|
|
if @template_args[:header_instructions].present?
|
|
|
|
header_instructions = PrettyText.cook(@template_args[:header_instructions], sanitize: false).html_safe
|
2015-12-07 16:52:10 -05:00
|
|
|
html_override.gsub!("%{header_instructions}", header_instructions)
|
|
|
|
else
|
|
|
|
html_override.gsub!("%{header_instructions}", "")
|
|
|
|
end
|
|
|
|
|
2016-06-03 09:48:54 -04:00
|
|
|
if @template_args[:respond_instructions].present?
|
|
|
|
respond_instructions = PrettyText.cook(@template_args[:respond_instructions], sanitize: false).html_safe
|
2015-11-24 10:58:26 -05:00
|
|
|
html_override.gsub!("%{respond_instructions}", respond_instructions)
|
|
|
|
else
|
|
|
|
html_override.gsub!("%{respond_instructions}", "")
|
2013-07-24 03:13:15 -04:00
|
|
|
end
|
|
|
|
|
2019-10-09 17:50:48 -04:00
|
|
|
html = UserNotificationRenderer.render(
|
2019-07-30 15:05:08 -04:00
|
|
|
template: 'layouts/email_template',
|
|
|
|
format: :html,
|
|
|
|
locals: { html_body: html_override.html_safe }
|
|
|
|
)
|
2013-07-26 03:27:46 -04:00
|
|
|
|
2013-07-24 03:13:15 -04:00
|
|
|
Mail::Part.new do
|
|
|
|
content_type 'text/html; charset=UTF-8'
|
2019-07-30 15:05:08 -04:00
|
|
|
body html
|
2013-07-24 03:13:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
def body
|
2019-04-29 20:25:53 -04:00
|
|
|
body = nil
|
|
|
|
|
|
|
|
if @opts[:template]
|
|
|
|
body = I18n.t("#{@opts[:template]}.text_body_template", template_args).dup
|
|
|
|
else
|
|
|
|
body = @opts[:body].dup
|
|
|
|
end
|
2013-06-10 16:46:08 -04:00
|
|
|
|
2016-06-03 09:48:54 -04:00
|
|
|
if @template_args[:unsubscribe_instructions].present?
|
2013-06-10 16:46:08 -04:00
|
|
|
body << "\n"
|
2016-06-03 09:48:54 -04:00
|
|
|
body << @template_args[:unsubscribe_instructions]
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
body
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_args
|
2020-07-10 05:05:55 -04:00
|
|
|
args = {
|
2016-06-03 09:48:54 -04:00
|
|
|
to: @to,
|
2013-06-13 17:00:00 -04:00
|
|
|
subject: subject,
|
|
|
|
body: body,
|
|
|
|
charset: 'UTF-8',
|
2021-06-27 18:55:13 -04:00
|
|
|
from: from_value,
|
|
|
|
cc: @opts[:cc]
|
2016-06-03 09:48:54 -04:00
|
|
|
}
|
2020-07-10 05:05:55 -04:00
|
|
|
|
|
|
|
args[:delivery_method_options] = @opts[:delivery_method_options] if @opts[:delivery_method_options]
|
2022-06-14 20:28:30 -04:00
|
|
|
args[:delivery_method_options] = (args[:delivery_method_options] || {}).merge(return_response: true)
|
2020-07-10 05:05:55 -04:00
|
|
|
|
|
|
|
args
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def header_args
|
|
|
|
result = {}
|
|
|
|
if @opts[:add_unsubscribe_link]
|
2016-07-13 16:32:46 -04:00
|
|
|
unsubscribe_url = @template_args[:unsubscribe_url].presence || @template_args[:user_preferences_url]
|
|
|
|
result['List-Unsubscribe'] = "<#{unsubscribe_url}>"
|
2016-04-07 10:21:17 -04:00
|
|
|
end
|
|
|
|
|
2016-07-13 16:32:46 -04:00
|
|
|
result['X-Discourse-Post-Id'] = @opts[:post_id].to_s if @opts[:post_id]
|
2013-06-18 15:54:02 -04:00
|
|
|
result['X-Discourse-Topic-Id'] = @opts[:topic_id].to_s if @opts[:topic_id]
|
2013-06-13 18:11:10 -04:00
|
|
|
|
2016-08-03 05:02:07 -04:00
|
|
|
# please, don't send us automatic responses...
|
|
|
|
result['X-Auto-Response-Suppress'] = 'All'
|
|
|
|
|
2021-06-27 18:55:13 -04:00
|
|
|
if !allow_reply_by_email?
|
|
|
|
# This will end up being the notification_email, which is a
|
|
|
|
# noreply address.
|
2013-06-13 17:00:00 -04:00
|
|
|
result['Reply-To'] = from_value
|
2021-06-27 18:55:13 -04:00
|
|
|
else
|
|
|
|
|
|
|
|
# The only reason we use from address for reply to is for group
|
|
|
|
# SMTP emails, where the person will be replying to the group's
|
|
|
|
# email_username.
|
|
|
|
if !@opts[:use_from_address_for_reply_to]
|
|
|
|
result[ALLOW_REPLY_BY_EMAIL_HEADER] = true
|
|
|
|
result['Reply-To'] = reply_by_email_address
|
|
|
|
else
|
|
|
|
# No point in adding a reply-to header if it is going to be identical
|
|
|
|
# to the from address/alias. If the from option is not present, then
|
|
|
|
# the default reply-to address is used.
|
|
|
|
result['Reply-To'] = from_value if from_value != alias_email(@opts[:from])
|
|
|
|
end
|
2013-06-13 17:00:00 -04:00
|
|
|
end
|
2013-06-13 10:56:16 -04:00
|
|
|
|
2013-07-06 20:37:44 -04: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 09:47:38 -04:00
|
|
|
} unless string.nil?
|
2013-06-10 16:46:08 -04:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2013-06-13 17:00:00 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def allow_reply_by_email?
|
|
|
|
SiteSetting.reply_by_email_enabled? &&
|
|
|
|
reply_by_email_address.present? &&
|
|
|
|
@opts[:allow_reply_by_email]
|
|
|
|
end
|
|
|
|
|
2014-06-06 09:09:00 -04:00
|
|
|
def private_reply?
|
2015-02-06 06:08:37 -05:00
|
|
|
allow_reply_by_email? && @opts[:private_reply]
|
2014-06-06 09:09:00 -04:00
|
|
|
end
|
|
|
|
|
2013-06-13 17:00:00 -04: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
|
2017-07-27 21:20:09 -04:00
|
|
|
|
2014-06-06 09:09:00 -04: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 17:00:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def alias_email(source)
|
2017-02-27 14:22:56 -05:00
|
|
|
return source if @opts[:from_alias].blank? &&
|
|
|
|
SiteSetting.email_site_title.blank? &&
|
|
|
|
SiteSetting.title.blank?
|
|
|
|
|
2019-01-04 10:06:21 -05:00
|
|
|
if @opts[:from_alias].present?
|
|
|
|
%Q|"#{Email.cleanup_alias(@opts[:from_alias])}" <#{source}>|
|
2017-02-27 14:22:56 -05:00
|
|
|
elsif source == SiteSetting.notification_email || source == SiteSetting.reply_by_email_address
|
|
|
|
site_alias_email(source)
|
2014-07-22 15:52:14 -04:00
|
|
|
else
|
2017-02-27 14:22:56 -05:00
|
|
|
source
|
2014-07-22 15:52:14 -04:00
|
|
|
end
|
2013-06-13 17:00:00 -04:00
|
|
|
end
|
|
|
|
|
2014-06-06 09:09:00 -04:00
|
|
|
def site_alias_email(source)
|
2019-01-04 10:06:21 -05:00
|
|
|
from_alias = Email.site_title
|
|
|
|
%Q|"#{Email.cleanup_alias(from_alias)}" <#{source}>|
|
2014-06-06 09:09:00 -04:00
|
|
|
end
|
|
|
|
|
2013-06-10 16:46:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|