Module: Railgun
- Defined in:
- lib/railgun.rb,
lib/railgun/errors.rb,
lib/railgun/mailer.rb,
lib/railgun/railtie.rb,
lib/railgun/attachment.rb
Defined Under Namespace
Classes: Attachment, ConfigurationError, Error, InternalError, Mailer, Railtie
Class Method Summary collapse
-
.build_message_object(mail) ⇒ Object
Acts on a Rails/ActionMailer message object and uses Mailgun::MessageBuilder to construct a new message.
-
.extract_amp_html(mail) ⇒ String
Returns the decoded AMP HTML from the Mail::Message object if it is available, otherwise nil.
-
.extract_body_html(mail) ⇒ String
Returns the decoded HTML body from the Mail::Message object if available, otherwise nil.
-
.extract_body_text(mail) ⇒ String
Returns the decoded text body from the Mail::Message object if it is available, otherwise nil.
-
.retrieve_amp_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if AMP part exists, (decomposing multipart into individual format if necessary) otherwise nil.
-
.retrieve_html_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if html part exists, (decomposing multipart into individual format if necessary) otherwise nil.
-
.retrieve_text_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if text part exists, (decomposing multipart into individual format if necessary) otherwise nil.
-
.transform_for_mailgun(mail) ⇒ Hash
Performs a series of transformations on the ‘mailgun*` attributes.
Class Method Details
.build_message_object(mail) ⇒ Object
Acts on a Rails/ActionMailer message object and uses Mailgun::MessageBuilder to construct a new message.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/railgun/mailer.rb', line 161 def (mail) mb = Mailgun::MessageBuilder.new mb.from mail[:from] mb.reply_to(mail[:reply_to].to_s) if mail[:reply_to].present? mb.template(mail[:template].to_s) if mail[:template].present? mb.subject mail.subject mb.body_html extract_body_html(mail) mb.body_text extract_body_text(mail) mb.amp_html extract_amp_html(mail) [:to, :cc, :bcc].each do |rcpt_type| addrs = mail[rcpt_type] || nil case addrs when String # Likely a single recipient mb.add_recipient rcpt_type.to_s, addrs when Array addrs.each do |addr| mb.add_recipient rcpt_type.to_s, addr end when Mail::Field mb.add_recipient rcpt_type.to_s, addrs.to_s end end # v:* attributes (variables) mail.mailgun_variables.try(:each) do |name, value| mb.variable(name, value) end return mb. if mail..empty? mail..each do |attach| attach = Attachment.new(attach, encoding: 'ascii-8bit', inline: attach.inline?) attach. mb end return mb. end |
.extract_amp_html(mail) ⇒ String
Returns the decoded AMP HTML from the Mail::Message object if it is available, otherwise nil.
236 237 238 239 240 241 242 |
# File 'lib/railgun/mailer.rb', line 236 def extract_amp_html(mail) begin retrieve_amp_part(mail).body.decoded || nil rescue nil end end |
.extract_body_html(mail) ⇒ String
Returns the decoded HTML body from the Mail::Message object if available, otherwise nil.
208 209 210 211 212 213 214 |
# File 'lib/railgun/mailer.rb', line 208 def extract_body_html(mail) begin retrieve_html_part(mail).body.decoded || nil rescue nil end end |
.extract_body_text(mail) ⇒ String
Returns the decoded text body from the Mail::Message object if it is available, otherwise nil.
222 223 224 225 226 227 228 |
# File 'lib/railgun/mailer.rb', line 222 def extract_body_text(mail) begin retrieve_text_part(mail).body.decoded || nil rescue nil end end |
.retrieve_amp_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if AMP part exists, (decomposing multipart into individual format if necessary) otherwise nil.
275 276 277 278 279 280 |
# File 'lib/railgun/mailer.rb', line 275 def retrieve_amp_part(mail) # AMP emails should always be multipart, with an HTML fallback. return unless mail.multipart? mail.find_first_mime_type('text/x-amp-html') end |
.retrieve_html_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if html part exists, (decomposing multipart into individual format if necessary) otherwise nil.
263 264 265 266 |
# File 'lib/railgun/mailer.rb', line 263 def retrieve_html_part(mail) return mail.html_part if mail.multipart? (mail.mime_type =~ /^text\/html$/i) && mail end |
.retrieve_text_part(mail) ⇒ Mail::Message
Returns the mail object from the Mail::Message object if text part exists, (decomposing multipart into individual format if necessary) otherwise nil.
251 252 253 254 |
# File 'lib/railgun/mailer.rb', line 251 def retrieve_text_part(mail) return mail.text_part if mail.multipart? (mail.mime_type =~ /^text\/plain$/i) && mail end |
.transform_for_mailgun(mail) ⇒ Hash
Performs a series of transformations on the ‘mailgun*` attributes. After prefixing them with the proper option type, they are added to the message hash where they will then be sent to the API as JSON.
It is important to note that headers set in ‘mailgun_headers` on the message WILL overwrite headers set via `mail.headers()`.
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/railgun/mailer.rb', line 88 def transform_for_mailgun(mail) = (mail) # o:* attributes (options) mail..try(:each) do |k, v| ["o:#{k}"] = v.dup end # t:* attributes (options) mail.mailgun_template_variables.try(:each) do |k, v| ["t:#{k}"] = v.dup end # support for using ActionMailer's `headers()` inside of the mailer # note: this will filter out parameters such as `from`, `to`, and so forth # as they are accepted as POST parameters on the message endpoint. msg_headers = Hash.new # h:* attributes (headers) # Let's set all of these headers on the [Mail::Message] so that # the are created inside of a [Mail::Header] instance and processed there. mail.headers(mail.mailgun_headers || {}) mail.header_fields.each do |field| header = field.name.downcase if msg_headers.include? header msg_headers[header] = [msg_headers[header], field.value].flatten else msg_headers[header] = field.value end end msg_headers.each do |k, v| if Railgun::Mailer::IGNORED_HEADERS.include? k.downcase Rails.logger.debug("[railgun] ignoring header (using envelope instead): #{k}") next end # Cover cases like `cc`, `bcc` where parameters are valid # headers BUT they are submitted as separate POST params # and already exist on the message because of the call to # `build_message_object`. if .include? k.downcase Rails.logger.debug("[railgun] ignoring header (already set): #{k}") next end ["h:#{k}"] = v end # recipient variables ['recipient-variables'] = mail.mailgun_recipient_variables.to_json if mail.mailgun_recipient_variables # reject blank values .delete_if do |k, v| next true if v.nil? # if it's an array remove empty elements v.delete_if { |i| i.respond_to?(:empty?) && i.empty? } if v.is_a?(Array) v.respond_to?(:empty?) && v.empty? end return end |