mailfactory 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mailfactory.rb CHANGED
@@ -126,6 +126,17 @@ class MailFactory
126
126
  end
127
127
 
128
128
 
129
+ def replyto=(newreplyto)
130
+ remove_header("Reply-To")
131
+ add_header("Reply-To", newreplyto)
132
+ end
133
+
134
+
135
+ def replyto()
136
+ return(get_header("Reply-To")[0])
137
+ end
138
+
139
+
129
140
  # sets the plain text body of the message
130
141
  def text=(newtext)
131
142
  @text = newtext
@@ -220,8 +231,14 @@ class MailFactory
220
231
  def add_attachment(filename, type=nil)
221
232
  attachment = Array.new()
222
233
  attachment[0] = Pathname.new(filename).basename
223
- attachment[1] = MIME::Types.type_for(filename).to_s
224
- File.open(filename, File::RDONLY) { |fp|
234
+ if(type == nil)
235
+ attachment[1] = MIME::Types.type_for(filename).to_s
236
+ else
237
+ attachment[1] = type
238
+ end
239
+
240
+ # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
241
+ File.open(filename, "rb") { |fp|
225
242
  attachment[2] = Base64.b64encode(fp.read())
226
243
  }
227
244
  @attachments << attachment
@@ -230,13 +247,24 @@ class MailFactory
230
247
 
231
248
  # adds an attachment to the mail as emailfilename. Type may be given as a mime type. If it
232
249
  # is left off and the MIME::Types module is available it will be determined automagically.
233
- def add_attachment_as(filename, emailfilename, type=nil)
250
+ # file may be given as an IO stream (which will be read until the end) or as a filename.
251
+ def add_attachment_as(file, emailfilename, type=nil)
234
252
  attachment = Array.new()
235
253
  attachment[0] = emailfilename
236
- attachment[1] = MIME::Types.type_for(filename).to_s
237
- File.open(filename, File::RDONLY) { |fp|
238
- attachment[2] = Base64.b64encode(fp.read())
239
- }
254
+ if(!file.respond_to?(:stat) and type == nil)
255
+ attachment[1] = MIME::Types.type_for(file.to_s()).to_s
256
+ else
257
+ attachemnt[1] = type
258
+ end
259
+
260
+ if(file.respond_to?(:stat))
261
+ # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
262
+ File.open(file.to_s(), "rb") { |fp|
263
+ attachment[2] = Base64.b64encode(fp.read())
264
+ }
265
+ else
266
+ attachment[2] = Base64.b64encode(file.read())
267
+ end
240
268
  @attachments << attachment
241
269
  end
242
270
 
@@ -1,8 +1,47 @@
1
1
  #!/usr/local/bin/ruby
2
2
 
3
- require 'test/unit'
3
+ require 'test/unit/ui/console/testrunner'
4
4
  require '../lib/mailfactory.rb'
5
5
 
6
+
7
+ def get_options()
8
+ options = Hash.new()
9
+
10
+ opts = OptionParser.new() { |opts|
11
+ opts.on_tail("-h", "--help", "Print this message") {
12
+ print(opts)
13
+ exit()
14
+ }
15
+
16
+ opts.on("-s", "--smtpserver SERVER", "SMTP server to use for remote tests") { |server|
17
+ options['smtpserver'] = server
18
+ }
19
+
20
+ opts.on("-f", "--from ADDRESS", "address to send the mail from") { |address|
21
+ options['from'] = address
22
+ }
23
+
24
+ opts.on("-t", "--to ADDRESS", "address to send the mail to") { |address|
25
+ options['to'] = address
26
+ }
27
+
28
+ opts.on("-u", "--username USERNAME", "username for smtp auth (required)") { |username|
29
+ options['username'] = username
30
+ }
31
+
32
+ opts.on("-p", "--password PASSWORD", "password for smtp auth (required)") { |password|
33
+ options['password'] = password
34
+ }
35
+
36
+ }
37
+
38
+ opts.parse(ARGV)
39
+
40
+ return(options)
41
+ end
42
+
43
+
44
+
6
45
  class TC_MailFactory < Test::Unit::TestCase
7
46
 
8
47
  def setup()
@@ -30,6 +69,7 @@ class TC_MailFactory < Test::Unit::TestCase
30
69
  }
31
70
  assert_equal(1, count, "Count of To: headers expected to be 1, but was #{count}")
32
71
  end
72
+
33
73
 
34
74
  def test_set_from
35
75
  assert_nothing_raised("exception raised while setting from=") {
@@ -52,6 +92,7 @@ class TC_MailFactory < Test::Unit::TestCase
52
92
  assert_equal(1, count, "Count of From: headers expected to be 1, but was #{count}")
53
93
  end
54
94
 
95
+
55
96
  def test_set_subject
56
97
  assert_nothing_raised("exception raised while setting subject=") {
57
98
  @mail.subject = "Test Subject"
@@ -73,6 +114,7 @@ class TC_MailFactory < Test::Unit::TestCase
73
114
  assert_equal(1, count, "Count of Subject: headers expected to be 1, but was #{count}")
74
115
  end
75
116
 
117
+
76
118
  def test_set_header
77
119
  assert_nothing_raised("exception raised while setting arbitrary header") {
78
120
  @mail.set_header("arbitrary", "some value")
@@ -80,6 +122,7 @@ class TC_MailFactory < Test::Unit::TestCase
80
122
 
81
123
  assert_equal("some value", @mail.get_header("arbitrary")[0], "arbitrary header does not equal \"some value\"")
82
124
  end
125
+
83
126
 
84
127
  def test_boundary_generator
85
128
  1.upto(50) {
@@ -87,10 +130,32 @@ class TC_MailFactory < Test::Unit::TestCase
87
130
  }
88
131
  end
89
132
 
133
+
90
134
  def test_email
91
135
  @mail.to="test@test.com"
92
136
  @mail.from="test@othertest.com"
93
137
  @mail.subject="This is a test"
94
138
  @mail.text = "This is a test message with\na few\n\nlines."
139
+
140
+ @mail.attach('testfile.txt')
141
+ @mail.attach('testsheet.xls')
142
+
143
+ if($options['smtpserver'] != nil and $options['to'] != nil and $options['from'] != nil)
144
+ assert_nothing_raised() {
145
+ require('net/smtp')
146
+ Net::SMTP.start($options['smtpserver'], 25, 'mail.from.domain', $options['username'], $options['password'], :cram_md5) { |smtp|
147
+ smtp.send_message(@mail.to_s(), $options['from'], $options['to'])
148
+ }
149
+ }
150
+ end
151
+ end
152
+
153
+
154
+ def test_attach
95
155
  end
156
+
96
157
  end
158
+
159
+
160
+ $options = get_options()
161
+ Test::Unit::UI::Console::TestRunner.run(TC_MailFactory)
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: mailfactory
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.0
7
- date: 2005-05-21
6
+ version: 0.5.1
7
+ date: 2005-06-16
8
8
  summary: MailFactory is a pure-ruby MIME mail generator
9
9
  require_paths:
10
10
  - lib
@@ -29,7 +29,6 @@ authors:
29
29
  - David Powers
30
30
  files:
31
31
  - lib/mailfactory.rb
32
- - lib/test.rb
33
32
  test_files:
34
33
  - tests/test_mailfactory.rb
35
34
  rdoc_options: []
data/lib/test.rb DELETED
@@ -1,21 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'net/smtp'
4
- require 'rubygems'
5
- require 'mailfactory'
6
-
7
-
8
- mail = MailFactory.new()
9
- mail.to = "test@test.com"
10
- mail.from = "sender@sender.com"
11
- mail.subject = "Here are some files for you!"
12
- mail.text = "This is what people with plain text mail readers will see"
13
- mail.html = "A little something <b>special</b> for people with HTML readers"
14
- mail.attach("/etc/fstab")
15
- mail.attach("/some/other/file")
16
-
17
- Net::SMTP.start('smtp1.testmailer.com', 25, 'mail.from.domain', fromaddress, password, :cram_md5) { |smtp|
18
- mail.to = toaddress
19
- smtp.send_message(mail.to_s(), fromaddress, toaddress)
20
- }
21
-