Class: SimpleMailer

Inherits:
Object
  • Object
show all
Defined in:
lib/simpleMailer.rb

Constant Summary collapse

MARKER =
"THATSMAMARKER"

Instance Method Summary collapse

Constructor Details

#initialize(host = "", port = 25) ⇒ SimpleMailer

Returns a new instance of SimpleMailer.



8
9
10
11
12
13
14
15
16
17
# File 'lib/simpleMailer.rb', line 8

def initialize(host="",port=25)
  @host=host
  @port=port
  @from=''
  @to=''
  @title=''
  @message=''
  @body=''
  @files=Array.new
end

Instance Method Details

#addr(field) ⇒ Object



61
62
63
# File 'lib/simpleMailer.rb', line 61

def addr(field)
  return field.gsub(/[^<]*<([^>]*)>/, '\1')
end

#attach_files(files) ⇒ Object

WARNING : with_files clear @files before adding new ones



51
52
53
54
55
56
57
58
59
# File 'lib/simpleMailer.rb', line 51

def attach_files(files)
  @files=Array.new
  files.each do |file|
    raise "File #{file} doesn't exist" unless File.exists?(file)
    raise "Can't read file #{file}" unless File.readable?(file)
    @files<<file
  end
  self
end

#from(from) ⇒ Object



34
35
36
37
38
# File 'lib/simpleMailer.rb', line 34

def from(from)
  raise "Not a proper email address" unless from.match(/.+@.+\..+/)
  @from=from
  self
end

#generate_bodyObject

TODO: Get information and format them to create a multipart message



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/simpleMailer.rb', line 84

def generate_body
  @body =<<EOF
From: #{@from}
To: #{@to.join(",")}
Subject: #{@title}
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=#{MARKER}
--#{MARKER}
Content-Type: text/plain
Content-Transfer-Encoding:8bit

EOF
  @body+=@message+"\n\n"
  # Time to process our files
  @files.each do |file|
    @body+=generate_file_part(file)
  end
  @body+="--#{MARKER}--"
  return true
end

#generate_file_part(file) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simpleMailer.rb', line 65

def generate_file_part(file)
  b64file=String.new
  filename=File.basename(file)
  part=String.new
  File.open(file) do |f|
    b64file=Base64.encode64(f.read)
  end
  part =<<EOF
--#{MARKER}
Content-Disposition: attachment; filename="#{filename}"
Content-Type: #{MIME::check(file).to_s}; name="#{filename}"
Content-Transfer-Encoding: base64

#{b64file}

EOF
end

#message(message) ⇒ Object



45
46
47
48
# File 'lib/simpleMailer.rb', line 45

def message(message)
  @message=cr_lf(message)
  self
end

#sendObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/simpleMailer.rb', line 105

def send()
  Net::SMTP.start(@host,@port) do |smtp|
    begin
      generate_body
      to = @to.map { |t| addr(t) }
      smtp.send_message(@body,addr(@from),to)
      return true
    rescue => e
      e
    end
  end
end

#title(title) ⇒ Object



40
41
42
43
# File 'lib/simpleMailer.rb', line 40

def title(title)
  @title=title
  self
end

#to(to) ⇒ Object

Want an array



26
27
28
29
30
31
32
# File 'lib/simpleMailer.rb', line 26

def to(to)
  to.each do |ad|
    raise "Not a proper email address" unless ad.match(/.+@.+\..+/)
  end
  @to=to
  self
end