mailfactory 1.2.3 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ end
50
50
 
51
51
  # An easy class for creating a mail message
52
52
  class MailFactory
53
-
53
+
54
54
  def initialize()
55
55
  @headers = Array.new()
56
56
  @attachments = Array.new()
@@ -58,11 +58,15 @@ class MailFactory
58
58
  @bodyboundary = generate_boundary()
59
59
  @html = nil
60
60
  @text = nil
61
+ @charset = 'utf-8'
61
62
  end
62
63
 
63
64
 
64
65
  # adds a header to the bottom of the headers
65
66
  def add_header(header, value)
67
+ value = quote_if_necessary(value, @charset) if header == 'subject'
68
+ value = quote_address_if_necessary(value, @charset) if header == 'from'
69
+ value = quote_address_if_necessary(value, @charset) if header == 'to'
66
70
  @headers << "#{header}: #{value}"
67
71
  end
68
72
 
@@ -104,7 +108,7 @@ class MailFactory
104
108
  # sets the HTML body of the message. Only the body of the
105
109
  # html should be provided
106
110
  def html=(newhtml)
107
- @html = "<html>\n<head>\n<meta content=\"text/html;charset=ISO-8859-1\" http-equiv=\"Content-Type\">\n</head>\n<body bgcolor=\"#ffffff\" text=\"#000000\">\n#{newhtml}\n</body>\n</html>"
111
+ @html = "<html>\n<head>\n<meta content=\"text/html;charset=#{@charset}\" http-equiv=\"Content-Type\">\n</head>\n<body bgcolor=\"#ffffff\" text=\"#000000\">\n#{newhtml}\n</body>\n</html>"
108
112
  end
109
113
 
110
114
 
@@ -327,11 +331,11 @@ protected
327
331
 
328
332
  if(@attachments.length > 0)
329
333
  # text part
330
- body << "#{buildbodyboundary('text/plain; charset=ISO-8859-1; format=flowed', '7bit')}\r\n\r\n#{@text}"
334
+ body << "#{buildbodyboundary("text/plain; charset=#{@charset}; format=flowed", 'quoted-printable')}\r\n\r\n#{quote_text_if_necessary(@text)}"
331
335
 
332
336
  # html part if one is provided
333
337
  if @html
334
- body << "#{buildbodyboundary('text/html; charset=ISO-8859-1', '7bit')}\r\n\r\n#{@html}"
338
+ body << "#{buildbodyboundary("text/html; charset=#{@charset}", 'quoted-printable')}\r\n\r\n#{quote_text_if_necessary(@html)}"
335
339
  end
336
340
 
337
341
  body << "--#{@bodyboundary}--"
@@ -345,10 +349,10 @@ protected
345
349
  end
346
350
  else
347
351
  # text part
348
- body << "#{buildbodyboundary('text/plain; charset=ISO-8859-1; format=flowed', '7bit')}\r\n\r\n#{@text}"
352
+ body << "#{buildbodyboundary("text/plain; charset=#{@charset}; format=flowed", 'quoted-printable')}\r\n\r\n#{quote_text_if_necessary(@text)}"
349
353
 
350
354
  # html part
351
- body << "#{buildbodyboundary('text/html; charset=ISO-8859-1', '7bit')}\r\n\r\n#{@html}"
355
+ body << "#{buildbodyboundary("text/html; charset=#{@charset}", 'quoted-printable')}\r\n\r\n#{quote_text_if_necessary(@html)}"
352
356
 
353
357
  body << "--#{@bodyboundary}--"
354
358
  end
@@ -389,5 +393,35 @@ protected
389
393
  return(enc)
390
394
  end
391
395
 
396
+ def quote_text_if_necessary(text)
397
+ text = text.gsub( /[^a-z ]/i ) { quote_char($&) }.
398
+ gsub( / /, "_" )
399
+ text
400
+ end
401
+
402
+ def quote_if_necessary(text, charset)
403
+ text = text.gsub( /[^a-z ]/i ) { quote_char($&) }.
404
+ gsub( / /, "_" )
405
+ "=?#{charset}?Q?#{text}?="
406
+ end
407
+
408
+ def quote_address_if_necessary(address, charset)
409
+ if Array === address
410
+ address.map { |a| quote_address_if_necessary(a, charset) }
411
+ elsif address =~ /^(\S.*)\s+(<.*>)$/
412
+ address = $2
413
+ phrase = quote_if_necessary($1.gsub(/^['"](.*)['"]$/, '\1'), charset)
414
+ "\"#{phrase}\" #{address}"
415
+ else
416
+ address
417
+ end
418
+ end
419
+
420
+ def quote_char(character)
421
+ result = ""
422
+ character.each_byte { |b| result << "=%02x" % b }
423
+ result
424
+ end
425
+
392
426
  end
393
427
 
@@ -98,7 +98,7 @@ class TC_MailFactory < Test::Unit::TestCase
98
98
  @mail.subject = "Test Subject"
99
99
  }
100
100
 
101
- assert_equal(@mail.subject, ["Test Subject"], "subject does not equal what it was set to")
101
+ assert_equal("=?utf-8?Q?Test_Subject?=", @mail.subject.to_s, "subject does not equal what it was set to")
102
102
 
103
103
  assert_nothing_raised("exception raised while setting subject=") {
104
104
  @mail.subject = "A Different Subject"
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: mailfactory
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.3
7
- date: 2006-08-01 00:00:00 -04:00
6
+ version: 1.3.0
7
+ date: 2008-04-13 00:00:00 +01:00
8
8
  summary: MailFactory is a pure-ruby MIME mail generator
9
9
  require_paths:
10
10
  - lib
@@ -25,6 +25,7 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - David Powers
30
31
  files: