Class Mail::Body
In: lib/mail/body.rb
Parent: Object

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.

Methods

Public Class methods

Public Instance methods

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

Accepts a string and performs a regular expression against the decoded text

Examples:

  body = Mail::Body.new('The body')
  body =~ /The/ #=> 0

  body = Mail::Body.new("VGhlIGJvZHk=\n")
  body.encoding = 'base64'
  body =~ /The/ #=> 0

Returns the boundary used by the body

Allows you to change the boundary of this Body 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

Returns the epilogue (any text that is after the last MIME boundary)

Sets the epilogue to a string (adds text after the last MIME boundary)

Accepts anything that responds to to_s and checks if it‘s a substring of the decoded text

Examples:

  body = Mail::Body.new('The body')
  body.include?('The') #=> true

  body = Mail::Body.new("VGhlIGJvZHk=\n")
  body.encoding = 'base64'
  body.include?('The') #=> true

Accepts a string and performs a regular expression against the decoded text

Examples:

  body = Mail::Body.new('The body')
  body.match(/The/) #=> #<MatchData "The">

  body = Mail::Body.new("VGhlIGJvZHk=\n")
  body.encoding = 'base64'
  body.match(/The/) #=> #<MatchData "The">

Returns true if there are parts defined in the body

Returns the preamble (any text that is before the first MIME boundary)

Sets the preamble to a string (adds text before the first MIME boundary)

Returns the raw source that the body was initialized with, without any tampering

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’ with any other content type coming after.

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

[Validate]