class RMail::Message
The RMail::Message
is an object representation of a standard Internet email message, including MIME multipart messages.
An RMail::Message
object represents a message header (held in the contained RMail::Header
object) and a message body. The message body may either be a single String for single part messages or an Array of RMail::Message
objects for MIME multipart messages.
Attributes
Access the epilogue string for this message. The epilogue string is relevant only for multipart messages. It is the text that occurs after all parts of the message and is generally nil.
Access the preamble string for this message. The preamble string is relevant only for multipart messages. It is the text that occurs just before the first part of the message, and is generally nil or simple English text describing the nature of the message.
Public Class Methods
Create a new, empty, RMail::Message
.
# File lib/rmail/message.rb, line 44 def initialize @header = RMail::Header.new @body = nil @epilogue = nil @preamble = nil @delimiters = nil end
Public Instance Methods
Test if this message is structured exactly the same as the other message. This is useful mainly for testing.
# File lib/rmail/message.rb, line 54 def ==(other) @preamble == other.preamble && @epilogue == other.epilogue && @header == other.header && @body == other.body end
Add a part to the message. After this message is called, the multipart?
method will return true and the body
method will return an array of parts.
# File lib/rmail/message.rb, line 92 def add_part(part) if @body.nil? @body = [part] elsif @body.is_a?(Array) @body.push(part) else @body = [@body, part] end end
Returns the body of the message as a String or Array.
If multipart?
returns true, it will be an array of RMail::Message
objects. Otherwise it will be a String.
See also header
.
# File lib/rmail/message.rb, line 67 def body return @body end
Sets the body of the message to the given value. It should either be a String or an Array of RMail:Message objects.
# File lib/rmail/message.rb, line 73 def body=(s) @body = s end
Decode the body of this message.
If the body of this message is encoded with quoted-printable
or base64
, this function will decode the data into its original form and return it as a String. If the body is not encoded, it is returned unaltered.
This only works when the message is not a multipart. The Content-Transfer-Encoding:
header field is consulted to determine the encoding of the body part.
# File lib/rmail/message.rb, line 112 def decode raise TypeError, "Can not decode a multipart message." if multipart? case header.fetch('content-transfer-encoding', '7bit').strip.downcase when 'quoted-printable' Utils.quoted_printable_decode(@body) when 'base64' Utils.base64_decode(@body) else @body end end
Call the supplied block for each line of the message. Each line will contain a trailing newline (\n
).
# File lib/rmail/message.rb, line 162 def each() # FIXME: this is incredibly inefficient! The only users of this # is RMail::Deliver -- get them to use a RMail::Serialize object. to_s.each("\n") { |line| yield line } end
Return each part of this message
FIXME: not tested
# File lib/rmail/message.rb, line 153 def each_part raise TypeError, "not a multipart message" unless multipart? @body.each do |part| yield part end end
Returns the RMail::Header
object.
See also body
.
# File lib/rmail/message.rb, line 80 def header() return @header end
Return true if the message consists of multiple parts.
# File lib/rmail/message.rb, line 85 def multipart? @body.is_a?(Array) end
Get the indicated part from a multipart message.
# File lib/rmail/message.rb, line 125 def part(i) raise TypeError, "Can not get part on a single part message." unless multipart? @body[i] end
Returns the entire message in a single string. This uses the RMail::Serialize
class.
# File lib/rmail/message.rb, line 145 def to_s() require 'rmail/serialize' RMail::Serialize.new('').serialize(self) end