Class: CommunicationMailer

Inherits:
ApplicationMailer show all
Defined in:
app/mailers/communication_mailer.rb

Overview

ActionMailer: communication.

Constant Summary collapse

MARKETING_CATEGORIES =

announcements, events, newsletters, promotions, webinars

Campaign::CATEGORIES

Instance Method Summary collapse

Methods inherited from ApplicationMailer

#null_mail

Instance Method Details

#email(email_from, email_to, subject, body, email_options = nil) ⇒ Object

template : An email template class
merge_options : parameters for liquid
options : email options (:to, :from, :cc, :bcc, :headers, :attachments)



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/mailers/communication_mailer.rb', line 14

def email(email_from, email_to, subject, body, email_options = nil)
  email_options ||= {}
  is_marketing_email = email_options[:category] && MARKETING_CATEGORIES.include?(email_options[:category])

  (email_options[:headers] || {}).each do |k, v|
    headers[k] = v
  end

  (email_options[:uploads] || []).each do |u|
    if u.try(:category) == 'paperless_qr_png'
      # Embed the USPS Label Broker QR inline (CID) so the RMA email body can
      # show it via <img src="cid:rma-qr-<id>"> (see Rma#paperless_return_html)
      # rather than as a separate attachment. Explicit content_id so the cid in
      # the pre-rendered body matches deterministically (Gmail/Outlook safe).
      file_name = "rma-qr-#{u.id}.png"
      attachments.inline[file_name] = u.attachment.data
      attachments[file_name].content_id = "<rma-qr-#{u.id}>"
    else
      attachments[u.attachment_name || "attachment_#{u.id}"] = u.attachment.data
    end
  end
  attachments["#{email_options[:appointment].events.first.summary.parameterize.underscore}.ics"] = { mime_type: 'application/ics', content: email_options[:appointment].to_ical } if email_options[:appointment].present?
  # Generate a unique message id based on the sender credential server hostname
  # We try here to match the authenticated sender SPF record or at least not to use
  # the rails generic which is the server name, e.g heatwave-app or heatwave-util,
  # supposedly this help with spam scoring
  server_fqdns = if is_marketing_email
                   Heatwave::Configuration.fetch(:smtp_credentials, :warmlyyours_marketing, :fqdns)
                 else
                   Heatwave::Configuration.fetch(:smtp_credentials, :warmlyyours_transaction, :fqdns)
                 end
  server_fqdns ||= 'warmlyyours.com'

  headers['Message-ID'] = "<#{SecureRandom.uuid}@#{server_fqdns}>"

  template_name = email_options[:template] || 'email'

  options = {
    from: email_from,
    to: email_to,
    subject: subject,
    template_path: 'communication_mailer',
    template_name: template_name
  }

  options[:cc] = email_options[:cc]
  options[:bcc] = email_options[:bcc]
  options[:reply_to] = email_options[:reply_to] if email_options[:reply_to].present?
  options[:skip_premailer] = true if email_options[:skip_premailer].to_b
  options[:references] = email_options[:references]
  @external_stylesheet = "emails/#{email_options[:stylesheet]}" if email_options[:stylesheet].present?
  @subject = subject
  @body = body
  @inline_stylesheet = email_options[:inline_css]
  @show_mailing_address = email_options[:show_mailing_address]
  list_unsubscribe_options = []
  list_unsubscribe_options << "<#{@list_unsubscribe_link}>" if (@list_unsubscribe_link = email_options[:list_unsubscribe_link])
  list_unsubscribe_options << "<mailto:#{@list_unsubscribe_email}>" if (@list_unsubscribe_email = email_options[:list_unsubscribe_email])
  @fax_unsubscribe_link = email_options[:fax_unsubscribe_link]

  options['List-Unsubscribe'] = list_unsubscribe_options.join(",") if list_unsubscribe_options.present?

  if Rails.env.production? && is_marketing_email
    options[:delivery_method_options] = {
      user_name: Heatwave::Configuration.fetch(:smtp_credentials, :warmlyyours_marketing, :user_name),
      password: Heatwave::Configuration.fetch(:smtp_credentials, :warmlyyours_marketing, :password)
    }
  end

  # Dev/staging campaign blasts are captured by mailpit (the env-level default
  # delivery target) like every other email — no per-message delivery_method
  # override needed. Browse them in the mailpit UI (config.x.mailpit_url).

  x_smtpapi = SendgridSmtpApi.build_x_smtpapi_header(
    unique_args: email_options[:unique_id].present? ? { unique_id: email_options[:unique_id] } : nil
  )
  Rails.logger.debug { "[CommunicationMailer] X-SMTPAPI: #{x_smtpapi}" } if Rails.env.development?
  headers['X-SMTPAPI'] = x_smtpapi

  mail(options)
end