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



69
70
71
72
# File 'app/providers/nuntius/smtp_mail_provider.rb', line 69

def block
  message.status = 'blocked'
  message
end

#deliverObject



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

def deliver
  return block unless MailAllowList.new(settings[:allow_list]).allowed?(message.to)
  return block if Nuntius::Message.where(status: %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

  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
  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?
      message.html = message.html.gsub('%7B%7Bmessage_url%7D%7D') { 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

  response = mail.deliver!

  message.provider_id = mail.message_id
  message.status = 'undelivered'
  message.status = 'sent' if Rails.env.test? ? true : response.success?
  message.last_sent_at = Time.zone.now if message.sent?

  message
end