Class: Mail::Body
- Inherits:
- 
      Object
      
        - Object
- Mail::Body
 
- Defined in:
- lib/mail/body.rb
Overview
Body
The body is where the text of the email is stored. Mail treats the body as a single object. The body itself has no information about boundaries used in the MIME standard, it just looks at its content as either a single block of text, or (if it is a multipart message) as an array of blocks of text.
A body has to be told to split itself up into a multipart message by calling #split with the correct boundary. This is because the body object has no way of knowing what the correct boundary is for itself (there could be many boundaries in a body in the case of a nested MIME text).
Once split is called, Mail::Body will slice itself up on this boundary, assigning anything that appears before the first part to the preamble, and anything that appears after the closing boundary to the epilogue, then each part gets initialized into a Mail::Part object.
The boundary that is used to split up the Body is also stored in the Body object for use on encoding itself back out to a string. You can overwrite this if it needs to be changed.
On encoding, the body will return the preamble, then each part joined by the boundary, followed by a closing boundary string and then the epilogue.
Instance Method Summary collapse
- #<<(val) ⇒ Object
- 
  
    
      #==(other)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Matches this body with another body. 
- 
  
    
      #=~(regexp)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Accepts a string and performs a regular expression against the decoded text. 
- #ascii_only? ⇒ Boolean
- 
  
    
      #boundary  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the boundary used by the body. 
- 
  
    
      #boundary=(val)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Allows you to change the boundary of this Body object. 
- #charset ⇒ Object
- #charset=(val) ⇒ Object
- #decoded ⇒ Object
- #default_encoding ⇒ Object
- #empty? ⇒ Boolean
- 
  
    
      #encoded(transfer_encoding = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns a body encoded using transfer_encoding. 
- #encoding(val = nil) ⇒ Object
- #encoding=(val) ⇒ Object
- 
  
    
      #epilogue  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the epilogue (any text that is after the last MIME boundary). 
- 
  
    
      #epilogue=(val)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the epilogue to a string (adds text after the last MIME boundary). 
- 
  
    
      #include?(other)  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Accepts anything that responds to #to_s and checks if it’s a substring of the decoded text. 
- 
  
    
      #initialize(string = '')  ⇒ Body 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Body. 
- 
  
    
      #match(regexp)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Accepts a string and performs a regular expression against the decoded text. 
- 
  
    
      #multipart?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    Returns true if there are parts defined in the body. 
- #negotiate_best_encoding(message_encoding, allowed_encodings = nil) ⇒ Object
- #parts ⇒ Object
- 
  
    
      #preamble  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the preamble (any text that is before the first MIME boundary). 
- 
  
    
      #preamble=(val)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Sets the preamble to a string (adds text before the first MIME boundary). 
- 
  
    
      #raw_source  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns the raw source that the body was initialized with, without any tampering. 
- 
  
    
      #set_sort_order(order)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Allows you to set the sort order of the parts, overriding the default sort order. 
- 
  
    
      #sort_parts!  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Allows you to sort the parts according to the default sort order, or the sort order you set with :set_sort_order. 
- #split!(boundary) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(string = '') ⇒ Body
Returns a new instance of Body.
| 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | # File 'lib/mail/body.rb', line 30 def initialize(string = '') @boundary = nil @preamble = nil @epilogue = nil @charset = nil @part_sort_order = [ "text/plain", "text/enriched", "text/html", "multipart/alternative" ] @parts = Mail::PartsList.new if Utilities.blank?(string) @raw_source = '' else # Do join first incase we have been given an Array in Ruby 1.9 if string.respond_to?(:join) @raw_source = ::Mail::Utilities.to_crlf(string.join('')) elsif string.respond_to?(:to_s) @raw_source = ::Mail::Utilities.to_crlf(string.to_s) else raise "You can only assign a string or an object that responds_to? :join or :to_s to a body." end end @encoding = default_encoding set_charset end | 
Instance Method Details
#<<(val) ⇒ Object
| 256 257 258 259 260 261 262 | # File 'lib/mail/body.rb', line 256 def <<( val ) if @parts @parts << val else @parts = Mail::PartsList.new[val] end end | 
#==(other) ⇒ Object
Matches this body with another body. Also matches the decoded value of this body with a string.
Examples:
body = Mail::Body.new('The body')
body == body #=> true
body = Mail::Body.new('The body')
body == 'The body' #=> true
body = Mail::Body.new("VGhlIGJvZHk=\n")
body.encoding = 'base64'
body == "The body" #=> true
| 67 68 69 70 71 72 73 | # File 'lib/mail/body.rb', line 67 def ==(other) if other.class == String self.decoded == other else super end end | 
#=~(regexp) ⇒ Object
| 85 86 87 | # File 'lib/mail/body.rb', line 85 def =~(regexp) self.decoded =~ regexp end | 
#ascii_only? ⇒ Boolean
| 276 277 278 279 280 281 | # File 'lib/mail/body.rb', line 276 def ascii_only? unless defined? @ascii_only @ascii_only = raw_source.ascii_only? end @ascii_only end | 
#boundary ⇒ Object
Returns the boundary used by the body
| 243 244 245 | # File 'lib/mail/body.rb', line 243 def boundary @boundary end | 
#boundary=(val) ⇒ Object
Allows you to change the boundary of this Body object
| 248 249 250 | # File 'lib/mail/body.rb', line 248 def boundary=( val ) @boundary = val end | 
#charset ⇒ Object
| 192 193 194 | # File 'lib/mail/body.rb', line 192 def charset @charset end | 
#charset=(val) ⇒ Object
| 196 197 198 | # File 'lib/mail/body.rb', line 196 def charset=( val ) @charset = val end | 
#decoded ⇒ Object
| 180 181 182 183 184 185 186 | # File 'lib/mail/body.rb', line 180 def decoded if !Encodings.defined?(encoding) raise UnknownEncodingType, "Don't know how to decode #{encoding}, please call #encoded and decode it yourself." else Encodings.get_encoding(encoding).decode(raw_source) end end | 
#default_encoding ⇒ Object
| 287 288 289 | # File 'lib/mail/body.rb', line 287 def default_encoding ascii_only? ? '7bit' : '8bit' end | 
#empty? ⇒ Boolean
| 283 284 285 | # File 'lib/mail/body.rb', line 283 def empty? !!raw_source.to_s.empty? end | 
#encoded(transfer_encoding = nil) ⇒ Object
Returns a body encoded using transfer_encoding. Multipart always uses an identiy encoding (i.e. no encoding). Calling this directly is not a good idea, but supported for compatibility TODO: Validate that preamble and epilogue are valid for requested encoding
| 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | # File 'lib/mail/body.rb', line 150 def encoded(transfer_encoding = nil) if multipart? self.sort_parts! encoded_parts = parts.map { |p| p.encoded } ([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s else dec = Mail::Encodings.get_encoding(encoding) enc = if Utilities.blank?(transfer_encoding) dec else negotiate_best_encoding(transfer_encoding) end if dec.nil? # Cannot decode, so skip normalization raw_source else # Decode then encode to normalize and allow transforming # from base64 to Q-P and vice versa decoded = dec.decode(raw_source) if defined?(Encoding) && charset && charset != "US-ASCII" decoded = decoded.encode(charset) decoded.force_encoding('BINARY') unless Encoding.find(charset).ascii_compatible? end enc.encode(decoded) end end end | 
#encoding(val = nil) ⇒ Object
| 200 201 202 203 204 205 206 | # File 'lib/mail/body.rb', line 200 def encoding(val = nil) if val self.encoding = val else @encoding end end | 
#encoding=(val) ⇒ Object
| 208 209 210 211 212 213 214 215 | # File 'lib/mail/body.rb', line 208 def encoding=( val ) @encoding = if val == "text" || Utilities.blank?(val) default_encoding else val end end | 
#epilogue ⇒ Object
Returns the epilogue (any text that is after the last MIME boundary)
| 228 229 230 | # File 'lib/mail/body.rb', line 228 def epilogue @epilogue end | 
#epilogue=(val) ⇒ Object
Sets the epilogue to a string (adds text after the last MIME boundary)
| 233 234 235 | # File 'lib/mail/body.rb', line 233 def epilogue=( val ) @epilogue = val end | 
#include?(other) ⇒ Boolean
| 113 114 115 | # File 'lib/mail/body.rb', line 113 def include?(other) self.decoded.include?(other.to_s) end | 
#match(regexp) ⇒ Object
| 99 100 101 | # File 'lib/mail/body.rb', line 99 def match(regexp) self.decoded.match(regexp) end | 
#multipart? ⇒ Boolean
Returns true if there are parts defined in the body
| 238 239 240 | # File 'lib/mail/body.rb', line 238 def multipart? true unless parts.empty? end | 
#negotiate_best_encoding(message_encoding, allowed_encodings = nil) ⇒ Object
| 142 143 144 | # File 'lib/mail/body.rb', line 142 def negotiate_best_encoding(, allowed_encodings = nil) Mail::Encodings::TransferEncoding.negotiate(, encoding, raw_source, allowed_encodings) end | 
#parts ⇒ Object
| 252 253 254 | # File 'lib/mail/body.rb', line 252 def parts @parts end | 
#preamble ⇒ Object
Returns the preamble (any text that is before the first MIME boundary)
| 218 219 220 | # File 'lib/mail/body.rb', line 218 def preamble @preamble end | 
#preamble=(val) ⇒ Object
Sets the preamble to a string (adds text before the first MIME boundary)
| 223 224 225 | # File 'lib/mail/body.rb', line 223 def preamble=( val ) @preamble = val end | 
#raw_source ⇒ Object
Returns the raw source that the body was initialized with, without any tampering
| 138 139 140 | # File 'lib/mail/body.rb', line 138 def raw_source @raw_source end | 
#set_sort_order(order) ⇒ Object
Allows you to set the sort order of the parts, overriding the default sort order. Defaults to ‘text/plain’, then ‘text/enriched’, then ‘text/html’, then ‘multipart/alternative’ with any other content type coming after.
| 120 121 122 | # File 'lib/mail/body.rb', line 120 def set_sort_order(order) @part_sort_order = order end | 
#sort_parts! ⇒ Object
Allows you to sort the parts according to the default sort order, or the sort order you set with :set_sort_order.
sort_parts! is also called from :encode, so there is no need for you to call this explicitly
| 128 129 130 131 132 133 134 | # File 'lib/mail/body.rb', line 128 def sort_parts! @parts.each do |p| p.body.set_sort_order(@part_sort_order) p.body.sort_parts! end @parts.sort!(@part_sort_order) end | 
#split!(boundary) ⇒ Object
| 264 265 266 267 268 269 270 271 272 273 274 | # File 'lib/mail/body.rb', line 264 def split!(boundary) self.boundary = boundary parts = extract_parts # Make the preamble equal to the preamble (if any) self.preamble = parts[0].to_s.strip # Make the epilogue equal to the epilogue (if any) self.epilogue = parts[-1].to_s.strip parts[1...-1].to_a.each { |part| @parts << Mail::Part.new(part) } self end | 
#to_s ⇒ Object
| 188 189 190 | # File 'lib/mail/body.rb', line 188 def to_s decoded end |