class DefraRubyEmail::LastEmailCache
Constants
- EMAIL_ATTRIBUTES
Attributes
last_email[RW]
Public Instance Methods
email_body()
click to toggle source
If you've set multipart emails then you'll have both a text and a html version (determined by adding the relevant erb views). If you do so then `my_mail.parts.length` will at least equal 2. If you only have one of them e.g. just a text version then parts doesn't get populated.
However any attachments will cause ActionMailer to use parts. So for example if we have a text only email with an attached image, then parts will be of length 2; one being the content and the other being the attachment.
To cater for all possibilities we have this method to grab the body content guides.rubyonrails.org/action_mailer_basics.html#sending-multipart-emails stackoverflow.com/a/15818886
# File lib/defra_ruby_email/last_email_cache.rb, line 41 def email_body part_to_use = last_email.text_part || last_email.html_part || last_email # return the message body without the header information part_to_use.body.decoded end
last_email_json()
click to toggle source
# File lib/defra_ruby_email/last_email_cache.rb, line 16 def last_email_json return JSON.generate(error: "No emails sent.") unless last_email.present? message_hash = {} EMAIL_ATTRIBUTES.each do |attribute| message_hash[attribute] = last_email.public_send(attribute) end message_hash[:body] = email_body message_hash[:attachments] = last_email.attachments.map(&:filename) JSON.generate(last_email: message_hash) end
reset()
click to toggle source
This is necessary to properly test the service functionality
# File lib/defra_ruby_email/last_email_cache.rb, line 12 def reset @last_email = nil end