mailfactory-acd 1.4.1 → 1.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mailfactory.rb +71 -71
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7944ba1c34653b3ef22ca177489516e4b9c743d4
4
- data.tar.gz: 42206921b890dfa2c00acdd1956f6ea07a97aa07
3
+ metadata.gz: 48524b9ba34f31d8cc9355cf5a3b396b1c23c87b
4
+ data.tar.gz: bbe948834feb6aeadfa1c1c7dd40f1fef835cc85
5
5
  SHA512:
6
- metadata.gz: 46d34ad603e9496e24d9da646d8cca4459d61df8357dfa8f1c3628677a805fe109fb3ddd0e20a0985ff616bd0ffec58fae1c6831ba2dcee49b19d58f780cb779
7
- data.tar.gz: 89b194d5a70f2ca254faed154e7153ad3509ba212a0b3a8d08261da189690733fc385c43afab662ecf625c454fb7763b4ab352af2298604f46566b1b095bcb1b
6
+ metadata.gz: e3d46f291b7e9315e9b6d45e51c5ad87433da8b0c407c2ea1e21276aa35fe98a9838a96874c1c7d3b99329affcb8a2f4c296dd1c81884b43ad4445c5d0b9a6f4
7
+ data.tar.gz: d5bfce55e19e5c11d037b6834184477a7999dbf92dca15161a4a091e2925de1350aaf93c4558e5a5c72c77c6f78f53e823cf27d48cc1d28002f2c034141cde19
@@ -48,8 +48,8 @@ end
48
48
  # An easy class for creating a mail message
49
49
  class MailFactory
50
50
 
51
- VERSION = '1.4.1'
52
-
51
+ VERSION = '1.4.2'
52
+
53
53
  def initialize()
54
54
  @headers = Array.new()
55
55
  @attachments = Array.new()
@@ -59,16 +59,16 @@ class MailFactory
59
59
  @text = nil
60
60
  @charset = 'utf-8'
61
61
  end
62
-
63
-
62
+
63
+
64
64
  # adds a header to the bottom of the headers
65
65
  def add_header(header, value)
66
66
  value = quoted_printable_with_instruction(value, @charset) if header == 'subject'
67
67
  value = quote_address_if_necessary(value, @charset) if %w[from to cc bcc reply-to].include?(header.downcase)
68
68
  @headers << "#{header}: #{value}"
69
69
  end
70
-
71
-
70
+
71
+
72
72
  # removes the named header - case insensitive
73
73
  def remove_header(header)
74
74
  @headers.each_index() { |i|
@@ -77,64 +77,64 @@ class MailFactory
77
77
  end
78
78
  }
79
79
  end
80
-
81
-
80
+
81
+
82
82
  # sets a header (removing any other versions of that header)
83
83
  def set_header(header, value)
84
84
  remove_header(header)
85
85
  add_header(header, value)
86
86
  end
87
-
88
-
87
+
88
+
89
89
  def replyto=(newreplyto)
90
90
  remove_header("Reply-To")
91
91
  add_header("Reply-To", newreplyto)
92
92
  end
93
-
94
-
93
+
94
+
95
95
  def replyto()
96
96
  return(get_header("Reply-To")[0])
97
97
  end
98
-
99
-
98
+
99
+
100
100
  # sets the plain text body of the message
101
101
  def text=(newtext)
102
102
  @text = newtext
103
103
  end
104
-
105
-
104
+
105
+
106
106
  # sets the HTML body of the message. Only the body of the
107
107
  # html should be provided
108
108
  def html=(newhtml)
109
109
  @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>"
110
110
  end
111
-
112
-
111
+
112
+
113
113
  # sets the HTML body of the message. The entire HTML section should be provided
114
114
  def rawhtml=(newhtml)
115
115
  @html = newhtml
116
116
  end
117
-
118
-
117
+
118
+
119
119
  # implement method missing to provide helper methods for setting and getting headers.
120
120
  # Headers with '-' characters may be set/gotten as 'x_mailer' or 'XMailer' (splitting
121
121
  # will occur between capital letters or on '_' chracters)
122
122
  def method_missing(methId, *args)
123
123
  name = methId.id2name()
124
-
124
+
125
125
  # mangle the name if we have to
126
126
  if(name =~ /_/)
127
127
  name = name.gsub(/_/, '-')
128
128
  elsif(name =~ /[A-Z]/)
129
129
  name = name.gsub(/([a-zA-Z])([A-Z])/, '\1-\2')
130
130
  end
131
-
131
+
132
132
  # determine if it sets or gets, and do the right thing
133
133
  if(name =~ /=$/)
134
134
  if(args.length != 1)
135
135
  super(methId, args)
136
136
  end
137
- set_header(name[/^(.*)=$/, 1], args[0])
137
+ set_header(name[/^(.*)=$/, 1], args[0])
138
138
  else
139
139
  if(args.length != 0)
140
140
  super(methId, args)
@@ -144,7 +144,7 @@ class MailFactory
144
144
  end
145
145
  end
146
146
 
147
-
147
+
148
148
  # returns the value (or values) of the named header in an array
149
149
  def get_header(header)
150
150
  headers = Array.new()
@@ -154,11 +154,11 @@ class MailFactory
154
154
  headers << h[/^[^:]+:(.*)/i, 1].strip()
155
155
  end
156
156
  }
157
-
157
+
158
158
  return(headers)
159
159
  end
160
-
161
-
160
+
161
+
162
162
  # returns true if the email is multipart
163
163
  def multipart?()
164
164
  if(@attachments.length > 0 or @html != nil)
@@ -167,7 +167,7 @@ class MailFactory
167
167
  return(false)
168
168
  end
169
169
  end
170
-
170
+
171
171
 
172
172
  # builds an email and returns it as a string. Takes the following options:
173
173
  # <tt>:messageid</tt>:: Adds a message id to the message based on the from header (defaults to false)
@@ -176,7 +176,7 @@ class MailFactory
176
176
  if(options[:date] == nil)
177
177
  options[:date] = true
178
178
  end
179
-
179
+
180
180
  if(options[:messageid])
181
181
  # add a unique message-id
182
182
  remove_header("Message-ID")
@@ -195,7 +195,7 @@ class MailFactory
195
195
  if(get_header("MIME-Version").length == 0)
196
196
  add_header("MIME-Version", "1.0")
197
197
  end
198
-
198
+
199
199
  if(get_header("Content-Type").length == 0)
200
200
  if(@attachments.length == 0)
201
201
  add_header("Content-Type", "multipart/alternative;boundary=\"#{@bodyboundary}\"")
@@ -204,17 +204,17 @@ class MailFactory
204
204
  end
205
205
  end
206
206
  end
207
-
207
+
208
208
  return("#{headers_to_s()}#{body_to_s()}")
209
209
  end
210
210
 
211
-
212
- # returns a formatted email - equivalent to construct(:messageid => true)
211
+
212
+ # returns a formatted email - equivalent to construct(:messageid => true)
213
213
  def to_s()
214
214
  return(construct(:messageid => true))
215
215
  end
216
-
217
-
216
+
217
+
218
218
  # generates a unique boundary string
219
219
  def generate_boundary()
220
220
  randomstring = Array.new()
@@ -234,8 +234,8 @@ class MailFactory
234
234
  }
235
235
  return("----=_NextPart_#{randomstring.join()}")
236
236
  end
237
-
238
-
237
+
238
+
239
239
  # adds an attachment to the mail. Type may be given as a mime type. If it
240
240
  # is left off and the MIME::Types module is available it will be determined automagically.
241
241
  # If the optional attachemntheaders is given, then they will be added to the attachment
@@ -248,8 +248,8 @@ class MailFactory
248
248
  attachment['mimetype'] = MIME::Types.type_for(filename).to_s
249
249
  else
250
250
  attachment['mimetype'] = type
251
- end
252
-
251
+ end
252
+
253
253
  # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
254
254
  File.open(filename, "rb") { |fp|
255
255
  attachment['attachment'] = file_encode(fp.read())
@@ -264,8 +264,8 @@ class MailFactory
264
264
 
265
265
  @attachments << attachment
266
266
  end
267
-
268
-
267
+
268
+
269
269
  # adds an attachment to the mail as emailfilename. Type may be given as a mime type. If it
270
270
  # is left off and the MIME::Types module is available it will be determined automagically.
271
271
  # file may be given as an IO stream (which will be read until the end) or as a filename.
@@ -278,17 +278,17 @@ class MailFactory
278
278
 
279
279
  if(type != nil)
280
280
  attachment['mimetype'] = type.to_s()
281
- elsif(file['\n'])
281
+ elsif(file["\n"])
282
282
  attachment['mimetype'] = MIME::Types.type_for(emailfilename).to_s
283
283
  elsif(file.kind_of?(String) or file.kind_of?(Pathname))
284
284
  attachment['mimetype'] = MIME::Types.type_for(file.to_s()).to_s
285
285
  else
286
286
  attachment['mimetype'] = ''
287
287
  end
288
-
289
- if(file['\n'])
288
+
289
+ if(file["\n"])
290
290
  attachment['attachment'] = file_encode(file)
291
- elsif(file.kind_of?(String) or file.kind_of?(Pathname))
291
+ elsif(file.kind_of?(String) or file.kind_of?(Pathname))
292
292
  # Open in rb mode to handle Windows, which mangles binary files opened in a text mode
293
293
  File.open(file.to_s(), "rb") { |fp|
294
294
  attachment['attachment'] = file_encode(fp.read())
@@ -298,50 +298,50 @@ class MailFactory
298
298
  else
299
299
  raise(Exception, "file is not a supported type (must be a String, Pathnamem, or support read method)")
300
300
  end
301
-
301
+
302
302
  if(attachmentheaders != nil)
303
303
  if(!attachmentheaders.kind_of?(Array))
304
304
  attachmentheaders = attachmentheaders.split(/\r?\n/)
305
305
  end
306
306
  attachment['headers'] = attachmentheaders
307
307
  end
308
-
308
+
309
309
  @attachments << attachment
310
310
  end
311
-
312
-
311
+
312
+
313
313
  alias attach add_attachment
314
314
  alias attach_as add_attachment_as
315
-
315
+
316
316
  protected
317
-
317
+
318
318
  # returns the @headers as a properly formatted string
319
319
  def headers_to_s()
320
320
  return("#{@headers.join("\r\n")}\r\n\r\n")
321
321
  end
322
-
323
-
322
+
323
+
324
324
  # returns the body as a properly formatted string
325
325
  def body_to_s()
326
326
  body = Array.new()
327
-
327
+
328
328
  # simple message with one part
329
329
  if(!multipart?())
330
330
  return(@text)
331
331
  else
332
332
  body << "This is a multi-part message in MIME format.\r\n\r\n--#{@attachmentboundary}\r\nContent-Type: multipart/alternative; boundary=\"#{@bodyboundary}\""
333
-
333
+
334
334
  if(@attachments.length > 0)
335
335
  # text part
336
336
  body << "#{buildbodyboundary("text/plain; charset=#{@charset}; format=flowed", 'quoted-printable')}\r\n\r\n#{quote_if_necessary(@text, @charset)}"
337
-
337
+
338
338
  # html part if one is provided
339
339
  if @html
340
340
  body << "#{buildbodyboundary("text/html; charset=#{@charset}", 'quoted-printable')}\r\n\r\n#{quote_if_necessary(@html, @charset)}"
341
341
  end
342
-
342
+
343
343
  body << "--#{@bodyboundary}--"
344
-
344
+
345
345
  # and, the attachments
346
346
  if(@attachments.length > 0)
347
347
  @attachments.each() { |attachment|
@@ -352,18 +352,18 @@ protected
352
352
  else
353
353
  # text part
354
354
  body << "#{buildbodyboundary("text/plain; charset=#{@charset}; format=flowed", 'quoted-printable')}\r\n\r\n#{quote_if_necessary(@text, @charset)}"
355
-
355
+
356
356
  # html part
357
357
  body << "#{buildbodyboundary("text/html; charset=#{@charset}", 'quoted-printable')}\r\n\r\n#{quote_if_necessary(@html, @charset)}"
358
-
358
+
359
359
  body << "--#{@bodyboundary}--"
360
360
  end
361
-
361
+
362
362
  return(body.join("\r\n\r\n"))
363
363
  end
364
364
  end
365
-
366
-
365
+
366
+
367
367
  # builds a boundary string for including attachments in the body, expects an attachment hash as built by
368
368
  # add_attachment and add_attachment_as
369
369
  def buildattachmentboundary(attachment)
@@ -372,11 +372,11 @@ protected
372
372
  if(attachment['headers'])
373
373
  boundary = boundary + "\r\n#{attachment['headers'].join("\r\n")}"
374
374
  end
375
-
375
+
376
376
  return(boundary)
377
377
  end
378
-
379
-
378
+
379
+
380
380
  # builds a boundary string for inclusion in the body of a message
381
381
  def buildbodyboundary(type, encoding)
382
382
  return("--#{@bodyboundary}\r\nContent-Type: #{type}\r\nContent-Transfer-Encoding: #{encoding}")
@@ -394,11 +394,11 @@ protected
394
394
  # return(collection.join("\n"))
395
395
  return(enc)
396
396
  end
397
-
398
-
397
+
398
+
399
399
  # Convert the given text into quoted printable format, with an instruction
400
400
  # that the text be eventually interpreted in the given charset.
401
-
401
+
402
402
  def quoted_printable_with_instruction(text, charset)
403
403
  text = quoted_printable_encode_header(text)
404
404
  "=?#{charset}?Q?#{text}?="
@@ -411,7 +411,7 @@ protected
411
411
 
412
412
  # Convert the given character to quoted printable format
413
413
  # see http://tools.ietf.org/html/rfc2047
414
-
414
+
415
415
  require 'enumerator' unless ''.respond_to? :enum_for
416
416
 
417
417
  def quoted_printable_encode_header(text)
@@ -465,5 +465,5 @@ protected
465
465
  address
466
466
  end
467
467
  end
468
-
468
+
469
469
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailfactory-acd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Powers
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-11-13 00:00:00.000000000 Z
12
+ date: 2014-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mime-types