Class: Email::MessageBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/email/message_builder.rb

Constant Summary collapse

ALLOW_REPLY_BY_EMAIL_HEADER =
"X-Discourse-Allow-Reply-By-Email"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to, opts = nil) ⇒ MessageBuilder

Returns a new instance of MessageBuilder.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/email/message_builder.rb', line 11

def initialize(to, opts = nil)
  @to = to
  @opts = opts || {}
  @template_args = {
    site_name: SiteSetting.title,
    email_prefix: SiteSetting.email_prefix.presence || SiteSetting.title,
    base_url: Discourse.base_url,
    user_preferences_url: "#{Discourse.base_url}/my/preferences",
    hostname: Discourse.current_hostname,
  }.merge!(@opts)
  if @template_args[:url].present?
    @template_args[:header_instructions] ||= I18n.t(
      "user_notifications.header_instructions",
      @template_args,
    )
    @visit_link_to_respond_key =
      DiscoursePluginRegistry.apply_modifier(
        :message_builder_visit_link_to_respond,
        "user_notifications.visit_link_to_respond",
        @opts,
        @to,
      )
    @reply_by_email_key =
      DiscoursePluginRegistry.apply_modifier(
        :message_builder_reply_by_email,
        "user_notifications.reply_by_email",
        @opts,
        @to,
      )

    if @opts[:include_respond_instructions] == false
      @template_args[:respond_instructions] = ""
      @template_args[:respond_instructions] = I18n.t(
        "user_notifications.pm_participants",
        @template_args,
      ) if @opts[:private_reply]
    else
      if @opts[:only_reply_by_email]
        string = +"user_notifications.only_reply_by_email"
        string << "_pm" if @opts[:private_reply]
      else
        string =
          (
            if allow_reply_by_email?
              +@reply_by_email_key
            else
              +@visit_link_to_respond_key
            end
          )
        string << "_pm" if @opts[:private_reply]
      end
      @template_args[:respond_instructions] = "---\n" + I18n.t(string, @template_args)
    end

    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
  end
end

Instance Attribute Details

#reply_by_email_keyObject (readonly)

Returns the value of attribute reply_by_email_key.



7
8
9
# File 'lib/email/message_builder.rb', line 7

def reply_by_email_key
  @reply_by_email_key
end

#template_argsObject (readonly)

Returns the value of attribute template_args.



7
8
9
# File 'lib/email/message_builder.rb', line 7

def template_args
  @template_args
end

Class Method Details

.custom_headers(string) ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/email/message_builder.rb', line 271

def self.custom_headers(string)
  result = {}
  string
    .split("|")
    .each do |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
    end unless string.nil?
  result
end

Instance Method Details

#bodyObject



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/email/message_builder.rb', line 187

def body
  body = nil

  if @opts[:template]
    body = I18n.t("#{@opts[:template]}.text_body_template", template_args).dup
  else
    body = @opts[:body].dup
  end

  if @template_args[:unsubscribe_instructions].present?
    body << "\n"
    body << @template_args[:unsubscribe_instructions]
  end
  DiscoursePluginRegistry.apply_modifier(:message_builder_body, body, @opts, @to)
end

#build_argsObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/email/message_builder.rb', line 203

def build_args
  args = {
    to: @to,
    subject: subject,
    body: body,
    charset: "UTF-8",
    from: from_value,
    cc: @opts[:cc],
    bcc: @opts[:bcc],
  }

  args[:delivery_method_options] = @opts[:delivery_method_options] if @opts[
    :delivery_method_options
  ]
  args[:delivery_method_options] = (args[:delivery_method_options] || {}).merge(
    return_response: true,
  )

  args
end

#header_argsObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/email/message_builder.rb', line 224

def header_args
  result = {}
  if @opts[:add_unsubscribe_link]
    unsubscribe_url =
      @template_args[:unsubscribe_url].presence || @template_args[:user_preferences_url]
    result["List-Unsubscribe"] = "<#{unsubscribe_url}>"
  end

  result["X-Discourse-Post-Id"] = @opts[:post_id].to_s if @opts[:post_id]
  result["X-Discourse-Post-Ids"] = @opts[:post_ids].join(",") if @opts[:post_ids].present?
  result["X-Discourse-Topic-Id"] = @opts[:topic_id].to_s if @opts[:topic_id]
  result["X-Discourse-Topic-Ids"] = @opts[:topic_ids].join(",") if @opts[:topic_ids].present?

  # at this point these have been filtered by the recipient's guardian for visibility,
  # see UserNotifications#send_notification_email
  result["X-Discourse-Tags"] = @template_args[:show_tags_in_subject] if @opts[
    :show_tags_in_subject
  ]
  result["X-Discourse-Category"] = @template_args[:show_category_in_subject] if @opts[
    :show_category_in_subject
  ]

  # please, don't send us automatic responses...
  result["X-Auto-Response-Suppress"] = "All"

  if !allow_reply_by_email?
    # This will end up being the notification_email, which is a
    # noreply address.
    result["Reply-To"] = from_value
  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
  end

  result.merge(MessageBuilder.custom_headers(SiteSetting.email_custom_headers))
end

#html_partObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/email/message_builder.rb', line 144

def html_part
  return unless html_override = @opts[:html_override]

  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)
  else
    html_override.gsub!("%{unsubscribe_instructions}", "")
  end

  if @template_args[:header_instructions].present?
    header_instructions =
      PrettyText.cook(@template_args[:header_instructions], sanitize: false).html_safe
    html_override.gsub!("%{header_instructions}", header_instructions)
  else
    html_override.gsub!("%{header_instructions}", "")
  end

  if @template_args[:respond_instructions].present?
    respond_instructions =
      PrettyText.cook(@template_args[:respond_instructions], sanitize: false).html_safe
    html_override.gsub!("%{respond_instructions}", respond_instructions)
  else
    html_override.gsub!("%{respond_instructions}", "")
  end

  html =
    UserNotificationRenderer.render(
      template: "layouts/email_template",
      format: :html,
      locals: {
        html_body: html_override.html_safe,
      },
    )
  html = DiscoursePluginRegistry.apply_modifier(:message_builder_html_part, html, @opts, @to)

  Mail::Part.new do
    content_type "text/html; charset=UTF-8"
    body html
  end
end

#subjectObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/email/message_builder.rb', line 79

def subject
  if @opts[:template] &&
       TranslationOverride.exists?(
         locale: I18n.locale,
         translation_key: "#{@opts[:template]}.subject_template",
       )
    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:
            (
              if @template_args[:show_category_in_subject]
                "[#{@template_args[:show_category_in_subject]}] "
              else
                ""
              end
            ),
          optional_tags:
            (
              if @template_args[:show_tags_in_subject]
                "#{@template_args[:show_tags_in_subject]} "
              else
                ""
              end
            ),
          topic_title: @template_args[:topic_title] ? @template_args[:topic_title] : "",
        },
      )
    subject = I18n.t("#{@opts[:template]}.subject_template", augmented_template_args)
  elsif @opts[:use_site_subject]
    subject = String.new(SiteSetting.email_subject)
    subject.gsub!("%{site_name}", @template_args[:email_prefix])
    subject.gsub!("%{optional_re}", @opts[:add_re_to_subject] ? I18n.t("subject_re") : "")
    subject.gsub!("%{optional_pm}", @opts[:private_reply] ? @template_args[:subject_pm] : "")
    subject.gsub!(
      "%{optional_cat}",
      (
        if @template_args[:show_category_in_subject]
          "[#{@template_args[:show_category_in_subject]}] "
        else
          ""
        end
      ),
    )
    subject.gsub!(
      "%{optional_tags}",
      @template_args[:show_tags_in_subject] ? "#{@template_args[:show_tags_in_subject]} " : "",
    )
    if @template_args[:topic_title]
      subject.gsub!("%{topic_title}", @template_args[:topic_title])
    end # must be last for safety
  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)
  else
    subject = @opts[:subject]
  end
  DiscoursePluginRegistry.apply_modifier(:message_builder_subject, subject, @opts, @to)
end