Class: Nuntius::SmtpMailProvider

Inherits:
BaseProvider show all
Defined in:
app/providers/nuntius/smtp_mail_provider.rb

Instance Attribute Summary

Attributes inherited from BaseProvider

#message

Instance Method Summary collapse

Methods inherited from BaseProvider

all_settings, #callback, class_from_name, #initialize, #name, #refresh, setting_reader, states, transport

Constructor Details

This class inherits a constructor from Nuntius::BaseProvider

Instance Method Details

#blockObject



91
92
93
94
# File 'app/providers/nuntius/smtp_mail_provider.rb', line 91

def block
  message.blocked
  message
end

#deliverObject



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
# File 'app/providers/nuntius/smtp_mail_provider.rb', line 17

def deliver
  return no_recipient if message.to.blank?
  return block unless MailAllowList.new(settings[:allow_list]).allowed?(message.to)
  return block if Nuntius::Message.where(state: %w[complaint bounced], to: message.to).count >= 1

  mail = if message.from.present?
    Mail.new(sender: from_header, from: message.from)
  else
    Mail.new(from: from_header)
  end

  mail.header["X-Nuntius-Message-Id"] = message.id

  if message.campaign.present? && message.subscriber.present?
    mail.header["List-Unsubscribe"] = "<" + message.subscriber.unsubscribe_link(message.campaign, message) + ">"
    mail.header["List-Unsubscribe-Post"] = "List-Unsubscribe=One-Click"
  end

  if Rails.env.test?
    mail.delivery_method :test
  else
    mail.delivery_method :smtp,
      address: host,
      port: port,
      user_name: username,
      password: password,
      return_response: true,
      ssl: ssl
  end

  mail.to = message.to
  mail.subject = message.subject
  mail.part content_type: "multipart/alternative" do |p|
    p.text_part = Mail::Part.new(
      body: message.text,
      content_type: "text/plain",
      charset: "UTF-8"
    )
    if message.html.present?
      # Apply email tracking (tracking pixel and link wrapping)
      Nuntius::EmailTrackingService.perform(message: message)

      message.html = message.html.gsub("{{message_url}}") { message_url(message) }

      p.html_part = Mail::Part.new(
        body: message.html,
        content_type: "text/html",
        charset: "UTF-8"
      )
    end
  end

  message.attachments.each do |attachment|
    mail.attachments[attachment.filename.to_s] = {mime_type: attachment.content_type, content: attachment.download}
  end

  begin
    response = mail.deliver!
  rescue Net::SMTPFatalError
    message.rejected
    return message
  rescue Net::SMTPServerBusy, Net::ReadTimeout
    message.undelivered
    return message
  end

  message.provider_id = mail.message_id
  message.undelivered
  message.sent if Rails.env.test? || response.success?
  message.last_sent_at = Time.zone.now if message.sent?

  message
end

#no_recipientObject



96
97
98
99
# File 'app/providers/nuntius/smtp_mail_provider.rb', line 96

def no_recipient
  message.no_recipient
  message
end