rhizmail 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rhizmail.rb +56 -5
  2. data/lib/rhizmail.rb~ +70 -38
  3. metadata +2 -2
data/lib/rhizmail.rb CHANGED
@@ -30,6 +30,10 @@
30
30
  # RhizMail comes with a SimpleTemplateMessage class, suited for times when you
31
31
  # want email templating that is simpler than Amrita or ERB.
32
32
  #
33
+ # == Contributors
34
+ #
35
+ # Brian Takita added SMTP authentication for 0.1.3.
36
+ #
33
37
  # == Dependencies
34
38
  #
35
39
  # RhizMail depends on three external libraries:
@@ -44,7 +48,17 @@ require 'net/smtp'
44
48
  require 'text/format'
45
49
 
46
50
  module RhizMail
47
- Version = '0.1.2'
51
+ Version = '0.1.3'
52
+
53
+ # Sets RhizMail to run using mocks for use in testing. Calling
54
+ # <tt>RhizMail.mock = true</tt> sets the Mailer to use MockMailer instead.
55
+ def self.mock=( maybe_mock )
56
+ if maybe_mock and Mailer.get_mailer.is_a?( Mailer )
57
+ Mailer.set_mailer MockMailer.new
58
+ elsif !maybe_mock and Mailer.get_mailer.is_a?( MockMailer )
59
+ ContextualService::Context.instance.set_resource( Mailer, nil )
60
+ end
61
+ end
48
62
 
49
63
  # Returns a boolean value describing whether <tt>address</tt> is a plausible
50
64
  # email address format.
@@ -61,7 +75,42 @@ module RhizMail
61
75
  class Mailer < ContextualService::Service
62
76
  @@smtpServer = 'localhost'
63
77
  @@smtpClass = Net::SMTP
78
+ @@smtpLogin = nil
79
+ @@smtpPassword = nil
80
+ @@smtpAuthType = nil
64
81
  @@messagesSent = []
82
+
83
+ def self.smtp_server
84
+ @@smtpServer
85
+ end
86
+
87
+ def self.smtp_server=(smtpServer)
88
+ @@smtpServer = smtpServer
89
+ end
90
+
91
+ def self.smtp_login
92
+ @@smtpLogin
93
+ end
94
+
95
+ def self.smtp_login=(smtpLogin)
96
+ @@smtpLogin = smtpLogin
97
+ end
98
+
99
+ def self.smtp_password
100
+ @@smtpPassword
101
+ end
102
+
103
+ def self.smtp_password=(smtpPassword)
104
+ @@smtpPassword = smtpPassword
105
+ end
106
+
107
+ def self.smtp_auth_type
108
+ @@smtpAuthType
109
+ end
110
+
111
+ def self.smtp_auth_type=(smtpAuthType)
112
+ @@smtpAuthType = smtpAuthType
113
+ end
65
114
 
66
115
  # Flushes record of messages sent.
67
116
  def self.flush; @@messagesSent = []; end
@@ -79,7 +128,9 @@ module RhizMail
79
128
  msg << "\n"
80
129
  msg << email.body
81
130
  begin
82
- @@smtpClass.start( @@smtpServer ) { |smtp|
131
+ @@smtpClass.start(
132
+ @@smtpServer, @@smtpLogin, @@smtpPassword, @@smtpAuthType
133
+ ) { |smtp|
83
134
  smtp.sendmail(msg, email.from_address, [ email.to_address ])
84
135
  }
85
136
  @@messagesSent << email
@@ -137,7 +188,7 @@ module RhizMail
137
188
  def from_header # :nodoc:
138
189
  fromHeader = "From: "
139
190
  if @from_name
140
- fromHeader += "#{@from_name} <#{@from_address}>"
191
+ fromHeader += "\"#{@from_name}\" <#{@from_address}>"
141
192
  else
142
193
  fromHeader += "#{@from_address}"
143
194
  end
@@ -164,9 +215,9 @@ module RhizMail
164
215
  def to_header # :nodoc:
165
216
  toHeader = "To: "
166
217
  if @to_name
167
- toHeader += " #{@to_name} <#{@to_address}>"
218
+ toHeader += "\"#{@to_name}\" <#{@to_address}>"
168
219
  else
169
- toHeader += " #{@to_address}"
220
+ toHeader += "#{@to_address}"
170
221
  end
171
222
  toHeader
172
223
  end
data/lib/rhizmail.rb~ CHANGED
@@ -32,10 +32,11 @@
32
32
  #
33
33
  # == Dependencies
34
34
  #
35
- # RhizMail depends on two external libraries:
35
+ # RhizMail depends on three external libraries:
36
36
  #
37
37
  # * ContextualService: http://contxtlservice.rubyforge.org
38
38
  # * MockFS: http://mockfs.rubyforge.org
39
+ # * Text::Format: http://rubyforge.org/projects/text-format
39
40
 
40
41
  require 'contxtlservice'
41
42
  require 'mockfs'
@@ -43,7 +44,7 @@ require 'net/smtp'
43
44
  require 'text/format'
44
45
 
45
46
  module RhizMail
46
- Version = '0.1.1'
47
+ Version = '0.1.2'
47
48
 
48
49
  # Returns a boolean value describing whether <tt>address</tt> is a plausible
49
50
  # email address format.
@@ -133,25 +134,23 @@ module RhizMail
133
134
  # Sends the email.
134
135
  def deliver; Mailer.get_mailer.send_email( self ); end
135
136
 
136
- # Returns an array of strings describing the headers for this email
137
- # message.
138
- def headers
139
- headers = []
140
- headers << "Subject: #{@subject}"
141
- toHeader = "To: "
142
- if @to_name
143
- toHeader += " #{@to_name} <#{@to_address}>"
144
- else
145
- toHeader += " #{@to_address}"
146
- end
147
- headers << toHeader
137
+ def from_header # :nodoc:
148
138
  fromHeader = "From: "
149
139
  if @from_name
150
140
  fromHeader += "#{@from_name} <#{@from_address}>"
151
141
  else
152
142
  fromHeader += "#{@from_address}"
153
143
  end
154
- headers << fromHeader
144
+ fromHeader
145
+ end
146
+
147
+ # Returns an array of strings describing the headers for this email
148
+ # message.
149
+ def headers
150
+ headers = []
151
+ headers << "Subject: #{@subject}"
152
+ headers << to_header
153
+ headers << from_header
155
154
  if content_type == :html
156
155
  headers << "Content-Type: text/html; charset=\"#{@charset}\""
157
156
  headers << "MIME-Version: 1.0"
@@ -161,6 +160,16 @@ module RhizMail
161
160
  end
162
161
  headers
163
162
  end
163
+
164
+ def to_header # :nodoc:
165
+ toHeader = "To: "
166
+ if @to_name
167
+ toHeader += " #{@to_name} <#{@to_address}>"
168
+ else
169
+ toHeader += " #{@to_address}"
170
+ end
171
+ toHeader
172
+ end
164
173
 
165
174
  # Mailer calls this before sending a message; subclasses can override this
166
175
  # if they want to ensure that certain parts of the message are valid before
@@ -225,8 +234,17 @@ module RhizMail
225
234
  #
226
235
  # Thanks!
227
236
  #
228
- # Note that the first line needs to be in the format "Subject: [ SUBJECT ]",
229
- # and should be followed by a blank line.
237
+ # The Subject and From information can be set explicitly in the template,
238
+ # with lines like:
239
+ #
240
+ # Subject: Thanks for joining Website.com!
241
+ # From: "Bill Smith" <bill.smith@email.com>
242
+ #
243
+ # These header lines need to be at the top of the template, and they should
244
+ # be followed by a blank line dividing them from the template body. Header
245
+ # lines are optional; you can leave them and set them explicitly using
246
+ # SimpleTemplateMessage#subject=, SimpleTemplateMessage#from_name=, and
247
+ # SimpleTemplateMessage#from_address= instead.
230
248
  #
231
249
  # Then you create an instance of SimpleTemplateMessage and use
232
250
  # SimpleTemplateMessage#substitute to change the contents:
@@ -286,7 +304,7 @@ module RhizMail
286
304
  #
287
305
  # If you call SimpleTemplateMessage#deliver without doing all the
288
306
  # substitutions required by the template, it will raise an InvalidStateError.
289
- # (Getting an exception is probably better than sending a customer an email
307
+ # (Getting an exception is probably better than sending somebody an email
290
308
  # with funny symbols in it.)
291
309
  class SimpleTemplateMessage < Message
292
310
  SubclassAttributes = Struct.new( :substitutions, :template, :from_address )
@@ -357,23 +375,7 @@ module RhizMail
357
375
  def initialize( *args )
358
376
  if args.first.is_a? Hash
359
377
  h = args.first.clone
360
- verboten_keys = [ :body, :charset, :content_type, :subject ]
361
- raise InvalidStateError unless ( h.keys & verboten_keys ).empty?
362
- template_file = nil
363
- if h[:template_file]
364
- template_file = h[:template_file]
365
- h.delete :template_file
366
- else
367
- template_file = class_attributes.template
368
- end
369
- raise InvalidStateError if template_file.nil?
370
- read_template template_file
371
- if class_attributes.from_address and !h[:from_address]
372
- h[:from_address] = class_attributes.from_address
373
- end
374
- unless h[:body_wrap].nil?
375
- @body_wrap = h.delete :body_wrap
376
- end
378
+ initialize_from_hash h
377
379
  super h
378
380
  else
379
381
  to_address, from_address, template_file = args
@@ -396,11 +398,15 @@ module RhizMail
396
398
  def body_template( template ) # :nodoc:
397
399
  body = ''
398
400
  blank_line_found = false
401
+ headers_included = true
399
402
  template.each { |line|
400
- if blank_line_found
403
+ if blank_line_found or !headers_included
401
404
  body += line
402
- else
405
+ elsif line =~ /: /
403
406
  blank_line_found = true if line =~ /^\n/
407
+ else
408
+ headers_included = false
409
+ body += line
404
410
  end
405
411
  }
406
412
  body
@@ -424,11 +430,29 @@ module RhizMail
424
430
  }
425
431
  end
426
432
 
433
+ def initialize_from_hash( h ) # :nodoc:
434
+ verboten_keys = [ :body, :charset, :content_type, :subject ]
435
+ raise InvalidStateError unless ( h.keys & verboten_keys ).empty?
436
+ template_file = template_file_from_hash h
437
+ raise InvalidStateError if template_file.nil?
438
+ read_template template_file
439
+ if class_attributes.from_address and !h[:from_address]
440
+ h[:from_address] = class_attributes.from_address
441
+ end
442
+ unless h[:body_wrap].nil?
443
+ @body_wrap = h.delete :body_wrap
444
+ end
445
+ end
446
+
427
447
  def read_template( template_file ) # :nodoc:
428
448
  @template = ''
429
449
  MockFS.file.open( template_file ) { |file| @template = file.gets nil }
430
450
  @template =~ /Subject: (.*)/
431
- @subject = $1
451
+ @subject = ( $1 or '' )
452
+ if @template =~ /^From: "(.*?)" \<(.*?)\>$/m
453
+ self.from_name = $1
454
+ self.from_address = $2
455
+ end
432
456
  end
433
457
 
434
458
  # Substitute a token with a value in the email body.
@@ -452,6 +476,14 @@ module RhizMail
452
476
  # to make when you create a new instance. +substitutions+ should always
453
477
  # return a hash of tokens to either values or Procs.
454
478
  def substitutions; {}; end
479
+
480
+ def template_file_from_hash( h ) # :nodoc:
481
+ if h[:template_file]
482
+ h.delete :template_file
483
+ else
484
+ template_file = class_attributes.template
485
+ end
486
+ end
455
487
 
456
488
  # Mailer will call this before sending this message; this will fail if any
457
489
  # tokens are left unsubstituted.
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: rhizmail
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2005-09-16
6
+ version: 0.1.3
7
+ date: 2005-10-26
8
8
  summary: RhizMail is a test-friendly library for sending out customized emails.
9
9
  require_paths:
10
10
  - lib