Extended maintenance of Ruby versions 1.8.7 and 1.9.2 ended on July 31, 2014. Read more

In Files

  • mailread.rb


Class/Module Index [+]



The Mail class represents an internet mail message (as per RFC822, RFC2822) with headers and a body.

Public Class Methods

new(f) click to toggle source

Create a new Mail where f is either a stream which responds to gets(), or a path to a file. If f is a path it will be opened.

The whole message is read so it can be made available through the header, [] and body methods.

The “From ” line is ignored if the mail is in mbox format.

               # File mailread.rb, line 12
def initialize(f)
  unless defined? f.gets
    f = open(f, "r")
    opened = true

  @header = {}
  @body = []
    while line = f.gets()
      next if /^From /=~line # skip From-line
      break if /^$/=~line    # end of header

      if /^(\S+?):\s*(.*)/=~line
        (attr = $1).capitalize!
        @header[attr] = $2
      elsif attr
        line.sub!(/^\s*/, '')
        @header[attr] += "\n" + line

    return unless line

    while line = f.gets()
      break if /^From /=~line
    f.close if opened

Public Instance Methods

[](field) click to toggle source

Return the header corresponding to field.

Matching is case-insensitive.

               # File mailread.rb, line 59
def [](field)
body() click to toggle source

Return the message body as an Array of lines

               # File mailread.rb, line 52
def body
  return @body
header() click to toggle source

Return the headers as a Hash.

               # File mailread.rb, line 47
def header
  return @header

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.