Class: Waw::Tools::MailAgent::Mail
Overview
A helper to respect the mail protocol
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bcc ⇒ Object
Target b carbon copy addresses.
-
#body ⇒ Object
Mail body.
-
#cc ⇒ Object
Target carbon copy addresses.
-
#charset ⇒ Object
Content-type.
-
#content_type ⇒ Object
Content-type.
-
#date ⇒ Object
Mail date.
-
#from ⇒ Object
Source address.
-
#mime_version ⇒ Object
MIME version.
-
#subject ⇒ Object
Mail subject.
-
#to ⇒ Object
Target addresses.
Class Method Summary collapse
-
.decode(str) ⇒ Object
(also: parse)
Decodes a mail from a given string.
Instance Method Summary collapse
-
#dup ⇒ Object
Makes a deep copy of this mail.
-
#initialize(subject = nil, body = nil, from = nil, *to) ⇒ Mail
constructor
Creates an empty mail.
-
#to_s ⇒ Object
(also: #encode, #dump)
Returns a valid mail string instantiation.
Constructor Details
#initialize(subject = nil, body = nil, from = nil, *to) ⇒ Mail
Creates an empty mail
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/waw/tools/mail/mail.rb', line 38 def initialize(subject = nil, body = nil, from = nil, *to) @from = from @to = to.empty? ? [] : to @cc, @bcc = [], [] @subject = subject @date = Time.now @mime_version = "1.0" @content_type = "text/plain" @charset = "UTF-8" @body = body end |
Instance Attribute Details
#bcc ⇒ Object
Target b carbon copy addresses
17 18 19 |
# File 'lib/waw/tools/mail/mail.rb', line 17 def bcc @bcc end |
#cc ⇒ Object
Target carbon copy addresses
14 15 16 |
# File 'lib/waw/tools/mail/mail.rb', line 14 def cc @cc end |
#charset ⇒ Object
Content-type
32 33 34 |
# File 'lib/waw/tools/mail/mail.rb', line 32 def charset @charset end |
#content_type ⇒ Object
Content-type
29 30 31 |
# File 'lib/waw/tools/mail/mail.rb', line 29 def content_type @content_type end |
#mime_version ⇒ Object
MIME version
26 27 28 |
# File 'lib/waw/tools/mail/mail.rb', line 26 def mime_version @mime_version end |
#subject ⇒ Object
Mail subject
20 21 22 |
# File 'lib/waw/tools/mail/mail.rb', line 20 def subject @subject end |
Class Method Details
.decode(str) ⇒ Object Also known as: parse
Decodes a mail from a given string
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/waw/tools/mail/mail.rb', line 54 def decode(str) mail = Mail.new lines = str.split("\n") until false case line = lines.shift.strip when /^From: (.*)$/ mail.from = $1 when /^To: (.*)$/ mail.to = $1.split(',').collect{|s| s.strip} when /^Cc: (.*)$/ mail.cc = $1.split(',').collect{|s| s.strip} when /^Bcc: (.*)$/ mail.bcc = $1.split(',').collect{|s| s.strip} when /^Subject: (.*)$/ mail.subject = $1 when /^Date: (.*)$/ mail.date = Time.rfc2822($1) when /^MIME-Version: (.*)$/ mail.mime_version = $1 when /^Content-type: (.*); charset=(.*)$/ mail.content_type = $1 mail.charset = $2 when /^$/ break end end mail.body = lines.join("\n") mail end |
Instance Method Details
#dup ⇒ Object
Makes a deep copy of this mail. Changing the list of receivers in particular does not affect the original mail
108 109 110 111 112 113 114 |
# File 'lib/waw/tools/mail/mail.rb', line 108 def dup mail = super mail.to = to.nil? ? nil : to.dup mail.cc = cc.nil? ? nil : cc.dup mail.bcc = bcc.nil? ? nil : bcc.dup mail end |
#to_s ⇒ Object Also known as: encode, dump
Returns a valid mail string instantiation
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/waw/tools/mail/mail.rb', line 88 def to_s str = <<-MAIL_END From: #{from} To: #{to.nil? ? '' : to.join(", ")} Cc: #{cc.nil? ? '' : cc.join(", ")} Bcc: #{bcc.nil? ? '' : bcc.join(", ")} Subject: #{subject} Date: #{date.rfc2822} MIME-Version: #{mime_version} Content-type: #{content_type}; charset=#{charset} #{body} MAIL_END str.gsub(/^[ \t]*/, '') end |