Class: CommunicationMailer

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

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)



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

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|
    attachments[u.attachment_name || "attachment_#{u.id}"] = u.attachment.data
  end
  if email_options[:appointment].present?
    attachments["#{email_options[:appointment].events.first.summary.parameterize.underscore}.ics"] = { mime_type: 'application/ics', content: email_options[:appointment].to_ical }
  end
  # 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]
  if email_options[:stylesheet].present?
    @external_stylesheet = "emails/#{email_options[:stylesheet]}"
  end
  @subject = subject
  @body = body
  @inline_stylesheet = email_options[:inline_css]
  @show_mailing_address = email_options[:show_mailing_address]
  list_unsubscribe_options = []
  if (@list_unsubscribe_link = email_options[:list_unsubscribe_link])
    list_unsubscribe_options << "<#{@list_unsubscribe_link}>"
  end
  if (@list_unsubscribe_email = email_options[:list_unsubscribe_email])
    list_unsubscribe_options << "<mailto:#{@list_unsubscribe_email}>"
  end
  @fax_unsubscribe_link = email_options[:fax_unsubscribe_link]

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

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

  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