module EnMail::Helpers::MessageManipulation

A toolbox with common operations for manipulating and reading message properties, potentially useful for all adapters.

Protected Instance Methods

body_to_part(message) click to toggle source

Returns a new Mail::Part with the same content and MIME headers as the message passed as an argument.

Although Mail gem provides +Mail::Message#convert_to_multipart+ method, it works correctly for non-multipart text/plain messages only. This method is more robust, and handles messages containing any content type, be they multipart or not.

The message passed as an argument is not altered.

TODO Copy MIME headers (ones which start with “Content-”) TODO Preserve Content-Transfer-Encoding when possible TODO Set some safe Content-Transfer-Encoding, like quoted-printable

# File lib/enmail/helpers/message_manipulation.rb, line 24
def body_to_part(message)
  part = ::Mail::Part.new
  part.content_type = message.content_type
  if message.multipart?
    message.body.parts.each { |p| part.add_part duplicate_part(p) }
  else
    part.body = message.body.decoded
  end
  part
end
duplicate_part(part) click to toggle source

Duplicates a message part, preserving its Content-ID if present.

@param [Mail::Part] part message part @return [Mail::Part] duplicate of part

# File lib/enmail/helpers/message_manipulation.rb, line 39
def duplicate_part(part)
  duplicate = part.dup
  duplicate.add_content_id(part.content_id) unless part.content_id.nil?
  duplicate
end
find_recipients_for(message) click to toggle source

Detects a list of e-mails which should be used to define a list of recipients of encrypted message. All is simply taken from the message To: field.

@param [Mail::Message] message @return [Array] an array of e-mails

# File lib/enmail/helpers/message_manipulation.rb, line 51
def find_recipients_for(message)
  message.to_addrs
end
find_signer_for(message) click to toggle source

Detects e-mail which should be used to find a message signer key. Basically, it is taken from the message From: field, but may be overwritten by :signer adapter option.

@param [Mail::Message] message @return [String] an e-mail

# File lib/enmail/helpers/message_manipulation.rb, line 61
def find_signer_for(message)
  options[:signer] || message.from_addrs.first
end
rewrite_body(message, content_type:, parts: []) click to toggle source

Replaces a message body. Clears all the existing body, be it multipart or not, and then appends parts passed as an argument.

@param [Mail::Message] message

Message which body is expected to be replaced.

@param [String] content_type

A new content type for message, required, must be kinda multipart.

@param [Array] parts

List of parts which the new message body is expected to be composed
from.

@return undefined

# File lib/enmail/helpers/message_manipulation.rb, line 76
def rewrite_body(message, content_type:, parts: [])
  message.body = nil
  message.content_type = content_type
  parts.each { |p| message.add_part(p) }
end