Encapsulates an email message.
Methods
Attributes
| [RW] | bcc | The list of the recipients, can be arrays. |
| [RW] | body | The body of the message. |
| [RW] | cc | The list of the recipients, can be arrays. |
| [RW] | charset | The charset used to encode the message. |
| [RW] | content_type | The content type of the message. |
| [RW] | encode_subject | Encode the subject? |
| [RW] | from | Sender, can be an array. |
| [RW] | headers | Additional headers |
| [RW] | reply_to | Reply to. |
| [RW] | sent_on | Sent on |
| [RW] | subject | The subject |
| [RW] | to | The list of the recipients, can be arrays. |
Public Class methods
[ show source ]
# File lib/nitro/mailer/mail.rb, line 61
61: def initialize(from = nil, to = nil, subject = nil, body = nil)
62: @from, @to, @subject, @body = from, to, subject, body
63: @headers = {}
64: @content_type = self.class.default_content_type
65: @charset = self.class.default_charset
66: end
Accept string or IO.
[ show source ]
# File lib/nitro/mailer/mail.rb, line 78
78: def self.new_from_encoded(encoded)
79: if encoded.is_a? String
80: require "stringio"
81: encoded = StringIO.new(encoded)
82: end
83:
84: f = encoded
85:
86: # the following code is copied from mailread.rb
87:
88: unless defined? f.gets
89: f = open(f, "r")
90: opened = true
91: end
92:
93: _headers = {}
94: _body = []
95: begin
96: while line = f.gets()
97: line.chop!
98: next if /^From /=~line # skip From-line
99: break if /^$/=~line # end of header
100:
101: if /^(\S+?):\s*(.*)/=~line
102: (attr = $1).capitalize!
103: _headers[attr] = $2
104: elsif attr
105: line.sub!(/^\s*/, '')
106: _headers[attr] += "\n" + line
107: end
108: end
109:
110: return unless line
111:
112: while line = f.gets()
113: break if /^From /=~line
114: _body.push(line)
115: end
116: ensure
117: f.close if opened
118: end
119:
120: mail = Mail.new
121: mail.headers = _headers
122: mail.body = _body.join("\n")
123: mail.parse_headers
124:
125: return mail
126: end
Public Instance methods
[ show source ]
# File lib/nitro/mailer/mail.rb, line 128
128: def [](key)
129: @headers[key]
130: end
[ show source ]
# File lib/nitro/mailer/mail.rb, line 132
132: def []=(key, value)
133: @headers[key] = value
134: end
Returns the Mail message in encoded format.
[ show source ]
# File lib/nitro/mailer/mail.rb, line 138
138: def encoded
139: raise "No body defined" unless @body
140: raise "No sender defined" unless @from
141: raise "No recipients defined" unless @to
142:
143: # gmosx: From is typically NOT an array.
144:
145: from = @from.is_a?(Array) ? @from.join(", ") : @from
146: buf = "From: #{from}\n"
147:
148: to = @to.is_a?(Array) ? @to.join(", ") : @to
149: buf << "To: #{to}\n"
150:
151: if @cc
152: cc = @cc.is_a?(Array) ? @cc.join(", ") : @cc
153: buf << "Cc: #{cc}\n"
154: end
155:
156: if @bcc
157: bcc = @bcc.is_a?(Array) ? @bcc.join(", ") : @bcc
158: buf << "Bcc: #{bcc}\n"
159: end
160:
161: buf << "Subject: #@subject\n" if @subject
162:
163: buf << "Content-Type: #@content_type; charset=#@charset\n"
164:
165: buf << "\n"
166: buf << @body
167:
168: return buf
169: end
[ show source ]
# File lib/nitro/mailer/mail.rb, line 68
68: def parse_headers
69: @from = @headers["From"]
70: @to = @headers["To"]
71: @cc = @headers["Cc"]
72: @bcc = @headers["Bcc"]
73: @subject = @headers["Subject"]
74: end