Class Mail
In: lib/mailread.rb
Parent: Object

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

Methods

[]   body   header   new  

Public Class methods

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.

[Source]

    # File lib/mailread.rb, line 12
12:   def initialize(f)
13:     unless defined? f.gets
14:       f = open(f, "r")
15:       opened = true
16:     end
17: 
18:     @header = {}
19:     @body = []
20:     begin
21:       while line = f.gets()
22:         line.chop!
23:         next if /^From /=~line # skip From-line
24:         break if /^$/=~line    # end of header
25: 
26:         if /^(\S+?):\s*(.*)/=~line
27:           (attr = $1).capitalize!
28:           @header[attr] = $2
29:         elsif attr
30:           line.sub!(/^\s*/, '')
31:           @header[attr] += "\n" + line
32:         end
33:       end
34:   
35:       return unless line
36: 
37:       while line = f.gets()
38:         break if /^From /=~line
39:         @body.push(line)
40:       end
41:     ensure
42:       f.close if opened
43:     end
44:   end

Public Instance methods

Return the header corresponding to field.

Matching is case-insensitive.

[Source]

    # File lib/mailread.rb, line 59
59:   def [](field)
60:     @header[field.capitalize]
61:   end

Return the message body as an Array of lines

[Source]

    # File lib/mailread.rb, line 52
52:   def body
53:     return @body
54:   end

Return the headers as a Hash.

[Source]

    # File lib/mailread.rb, line 47
47:   def header
48:     return @header
49:   end

[Validate]