Class: Waw::Tools::MailAgent::Mail

Inherits:
Object
  • Object
show all
Defined in:
lib/waw/tools/mail/mail.rb

Overview

A helper to respect the mail protocol

Direct Known Subclasses

Template

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bccObject

Target b carbon copy addresses



17
18
19
# File 'lib/waw/tools/mail/mail.rb', line 17

def bcc
  @bcc
end

#bodyObject

Mail body



35
36
37
# File 'lib/waw/tools/mail/mail.rb', line 35

def body
  @body
end

#ccObject

Target carbon copy addresses



14
15
16
# File 'lib/waw/tools/mail/mail.rb', line 14

def cc
  @cc
end

#charsetObject

Content-type



32
33
34
# File 'lib/waw/tools/mail/mail.rb', line 32

def charset
  @charset
end

#content_typeObject

Content-type



29
30
31
# File 'lib/waw/tools/mail/mail.rb', line 29

def content_type
  @content_type
end

#dateObject

Mail date



23
24
25
# File 'lib/waw/tools/mail/mail.rb', line 23

def date
  @date
end

#fromObject

Source address



8
9
10
# File 'lib/waw/tools/mail/mail.rb', line 8

def from
  @from
end

#mime_versionObject

MIME version



26
27
28
# File 'lib/waw/tools/mail/mail.rb', line 26

def mime_version
  @mime_version
end

#subjectObject

Mail subject



20
21
22
# File 'lib/waw/tools/mail/mail.rb', line 20

def subject
  @subject
end

#toObject

Target addresses



11
12
13
# File 'lib/waw/tools/mail/mail.rb', line 11

def to
  @to
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

#dupObject

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_sObject 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