lib/railgun/mailer.rb in mailgun-ruby-1.2.13 vs lib/railgun/mailer.rb in mailgun-ruby-1.2.14

- old
+ new

@@ -171,10 +171,11 @@ 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 @@ -230,10 +231,24 @@ rescue nil end end + # Returns the decoded AMP HTML from the Mail::Message object if it is available, + # otherwise nil. + # + # @param [Mail::Message] mail message to transform + # + # @return [String] + def extract_amp_html(mail) + begin + retrieve_amp_part(mail).body.decoded || nil + rescue + nil + end + end + # Returns the mail object from the Mail::Message object if text part exists, # (decomposing multipart into individual format if necessary) # otherwise nil. # # @param [Mail::Message] mail message to transform @@ -252,7 +267,21 @@ # # @return [Mail::Message] mail message with its content-type = text/html def retrieve_html_part(mail) return mail.html_part if mail.multipart? (mail.mime_type =~ /^text\/html$/i) && mail + end + + # Returns the mail object from the Mail::Message object if AMP part exists, + # (decomposing multipart into individual format if necessary) + # otherwise nil. + # + # @param [Mail::Message] mail message to transform + # + # @return [Mail::Message] mail message with its content-type = text/x-amp-html + 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 end