Class: RMail::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/rmail/message.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessage

Create a new, empty, RMail::Message.



44
45
46
47
48
49
# File 'lib/rmail/message.rb', line 44

def initialize
  @header = RMail::Header.new
  @body = nil
  @epilogue = nil
  @preamble = nil
end

Instance Attribute Details

#epilogueObject

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.



133
134
135
# File 'lib/rmail/message.rb', line 133

def epilogue
  @epilogue
end

#preambleObject

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.



140
141
142
# File 'lib/rmail/message.rb', line 140

def preamble
  @preamble
end

Instance Method Details

#==(other) ⇒ Object

Test if this message is structured exactly the same as the other message. This is useful mainly for testing.



53
54
55
56
57
58
# File 'lib/rmail/message.rb', line 53

def ==(other)
  @preamble == other.preamble &&
    @epilogue == other.epilogue &&
    @header == other.header &&
    @body == other.body
end

#add_part(part) ⇒ Object

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.



91
92
93
94
95
96
97
98
99
# File 'lib/rmail/message.rb', line 91

def add_part(part)
  if @body.nil?
	@body = [part]
  elsif @body.is_a?(Array)
    @body.push(part)
  else
	@body = [@body, part]
  end
end

#bodyObject

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.



66
67
68
# File 'lib/rmail/message.rb', line 66

def body
  return @body
end

#body=(s) ⇒ Object

Sets the body of the message to the given value. It should either be a String or an Array of RMail:Message objects.



72
73
74
# File 'lib/rmail/message.rb', line 72

def body=(s)
  @body = s
end

#decodeObject

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.

Raises:

  • (TypeError)


111
112
113
114
115
116
117
118
119
120
121
# File 'lib/rmail/message.rb', line 111

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

#eachObject

Call the supplied block for each line of the message. Each line will contain a trailing newline (\n).



161
162
163
164
165
166
167
# File 'lib/rmail/message.rb', line 161

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

#each_partObject

Return each part of this message

FIXME: not tested

Raises:

  • (TypeError)


152
153
154
155
156
157
# File 'lib/rmail/message.rb', line 152

def each_part
  raise TypeError, "not a multipart message" unless multipart?
  @body.each do |part|
    yield part
  end
end

#get_delimitersObject

This is used by the serializing functions to retrieve the MIME multipart delimiter strings found while parsing the message. These delimiters are then used when serializing the message again.

Normal uses of RMail::Message will never use this method, and so it is left undocumented.



190
191
192
193
194
195
196
197
198
# File 'lib/rmail/message.rb', line 190

def get_delimiters          # :nodoc:
  unless multipart? and @delimiters and @delimiters_boundary and
      @delimiters.length == @body.length + 1 and
      header.param('content-type', 'boundary') == @delimiters_boundary
    @delimiters = nil
    @delimiters_boundary = nil
  end
  [ @delimiters, @delimiters_boundary ]
end

#headerObject

Returns the RMail::Header object.

See also #body.



79
80
81
# File 'lib/rmail/message.rb', line 79

def header()
  return @header
end

#multipart?Boolean

Return true if the message consists of multiple parts.

Returns:

  • (Boolean)


84
85
86
# File 'lib/rmail/message.rb', line 84

def multipart?
  @body.is_a?(Array)
end

#part(i) ⇒ Object

Get the indicated part from a multipart message.

Raises:

  • (TypeError)


124
125
126
127
128
# File 'lib/rmail/message.rb', line 124

def part(i)
  raise TypeError,
    "Can not get part on a single part message." unless multipart?
  @body[i]
end

#set_delimiters(delimiters, boundary) ⇒ Object

This is used by the RMail::Parser to set the MIME multipart delimiter strings found in the message. These delimiters are then used when serializing the message again.

Normal uses of RMail::Message will never use this method, and so it is left undocumented.

Raises:

  • (TypeError)


175
176
177
178
179
180
181
# File 'lib/rmail/message.rb', line 175

def set_delimiters(delimiters, boundary) # :nodoc:
  raise TypeError, "not a multipart message" unless multipart?
  raise ArgumentError, "delimiter array wrong size" unless
    delimiters.length == @body.length + 1
  @delimiters = delimiters.to_ary
  @delimiters_boundary = boundary.to_str
end

#to_sObject

Returns the entire message in a single string. This uses the RMail::Serialize class.



144
145
146
147
# File 'lib/rmail/message.rb', line 144

def to_s()
  require 'rmail/serialize'
  RMail::Serialize.new('').serialize(self)
end