Class: Webhookdb::Message::EmailTransport

Inherits:
Transport
  • Object
show all
Includes:
Appydays::Configurable
Defined in:
lib/webhookdb/message/email_transport.rb

Instance Method Summary collapse

Methods inherited from Transport

for, for!, #recipient, register_transport

Methods included from Webhookdb::MethodUtilities

#attr_predicate, #attr_predicate_accessor, #singleton_attr_accessor, #singleton_attr_reader, #singleton_attr_writer, #singleton_method_alias, #singleton_predicate_accessor, #singleton_predicate_reader

Instance Method Details

#_handle_postmark(delivery, mail, message_id) ⇒ Object



129
130
131
132
# File 'lib/webhookdb/message/email_transport.rb', line 129

def _handle_postmark(delivery, mail, message_id)
  mail.header["X-PM-Metadata-messageid"] = message_id
  mail.header["X-PM-Tag"] = delivery.template
end

#add_bodies(delivery, content) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/webhookdb/message/email_transport.rb', line 53

def add_bodies(delivery, content)
  pm = Premailer.new(
    content.to_s,
    with_html_string: true,
    warn_level: Premailer::Warnings::SAFE,
  )

  begin
    subject = content[:subject]
  rescue TypeError, NoMethodError
    subject = nil
  end

  raise "content %p is missing a subject" % content unless subject

  bodies = []
  bodies << delivery.add_body(content: content[:subject], mediatype: "subject")
  bodies << delivery.add_body(content: pm.to_plain_text, mediatype: "text/plain")
  bodies << delivery.add_body(content: pm.to_inline_css, mediatype: "text/html")
  return bodies
end

#allowlisted?(address) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/webhookdb/message/email_transport.rb', line 75

def allowlisted?(address)
  return self.class.allowlist.any? { |pattern| File.fnmatch(pattern, address) }
end

#format_email_name(email, name) ⇒ Object



124
125
126
127
# File 'lib/webhookdb/message/email_transport.rb', line 124

def format_email_name(email, name)
  return email if name.blank?
  return "#{name} <#{email}>"
end

#send!(delivery) ⇒ Object



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
# File 'lib/webhookdb/message/email_transport.rb', line 79

def send!(delivery)
  unless allowlisted?(delivery.to)
    raise Webhookdb::Message::Transport::UndeliverableRecipient,
          "#{delivery.to} is not allowlisted"
  end

  from = delivery.extra_fields["from"].present? ? delivery.extra_fields["from"] : self.class.from
  to = self.format_email_name(delivery.to, delivery.recipient&.name)
  message_id = SecureRandom.uuid.to_s
  this = self
  Mail.deliver do
    delivery_method(
      :smtp,
      address: this.class.smtp_host,
      port: this.class.smtp_port,
      user_name: this.class.smtp_user,
      password: this.class.smtp_password,
      enable_starttls_auto: this.class.smtp_starttls,
    )
    this.class.smtp_headers.each do |k, v|
      header[k] = v
    end
    custom_method = :"_handle_#{this.class.smtp_provider}"
    this.send(custom_method, delivery, self, message_id) if this.respond_to?(custom_method)
    from from
    to to
    reply_to(delivery.extra_fields["reply_to"]) if delivery.extra_fields["reply_to"].present?
    subject delivery.body_with_mediatype("subject")&.content
    text_part do
      content_type "text/plain; charset=UTF-8"
      body delivery.body_with_mediatype!("text/plain")&.content
    end
    html_part do
      content_type "text/html; charset=UTF-8"
      body delivery.body_with_mediatype!("text/html")&.content
    end
  end
  return message_id
end

#serviceObject



45
46
47
# File 'lib/webhookdb/message/email_transport.rb', line 45

def service
  return "smtp"
end

#supports_layout?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/webhookdb/message/email_transport.rb', line 49

def supports_layout?
  return true
end

#typeObject



41
42
43
# File 'lib/webhookdb/message/email_transport.rb', line 41

def type
  return :email
end