actionmailer 0.3.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of actionmailer might be problematic. Click here for more details.

Files changed (34) hide show
  1. data/CHANGELOG +3 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README +102 -0
  4. data/install.rb +61 -0
  5. data/lib/action_mailer.rb +44 -0
  6. data/lib/action_mailer/base.rb +111 -0
  7. data/lib/action_mailer/mail_helper.rb +17 -0
  8. data/lib/action_mailer/vendor/text/format.rb +1447 -0
  9. data/lib/action_mailer/vendor/tmail.rb +4 -0
  10. data/lib/action_mailer/vendor/tmail/address.rb +223 -0
  11. data/lib/action_mailer/vendor/tmail/base64.rb +52 -0
  12. data/lib/action_mailer/vendor/tmail/config.rb +50 -0
  13. data/lib/action_mailer/vendor/tmail/encode.rb +447 -0
  14. data/lib/action_mailer/vendor/tmail/facade.rb +531 -0
  15. data/lib/action_mailer/vendor/tmail/header.rb +893 -0
  16. data/lib/action_mailer/vendor/tmail/info.rb +16 -0
  17. data/lib/action_mailer/vendor/tmail/loader.rb +1 -0
  18. data/lib/action_mailer/vendor/tmail/mail.rb +420 -0
  19. data/lib/action_mailer/vendor/tmail/mailbox.rb +414 -0
  20. data/lib/action_mailer/vendor/tmail/mbox.rb +1 -0
  21. data/lib/action_mailer/vendor/tmail/net.rb +261 -0
  22. data/lib/action_mailer/vendor/tmail/obsolete.rb +116 -0
  23. data/lib/action_mailer/vendor/tmail/parser.rb +1503 -0
  24. data/lib/action_mailer/vendor/tmail/port.rb +358 -0
  25. data/lib/action_mailer/vendor/tmail/scanner.rb +22 -0
  26. data/lib/action_mailer/vendor/tmail/scanner_r.rb +244 -0
  27. data/lib/action_mailer/vendor/tmail/stringio.rb +260 -0
  28. data/lib/action_mailer/vendor/tmail/tmail.rb +1 -0
  29. data/lib/action_mailer/vendor/tmail/utils.rb +215 -0
  30. data/rakefile +99 -0
  31. data/test/fixtures/templates/signed_up.rhtml +3 -0
  32. data/test/fixtures/test_mailer/signed_up.rhtml +3 -0
  33. data/test/mail_service_test.rb +53 -0
  34. metadata +86 -0
@@ -0,0 +1,531 @@
1
+ #
2
+ # facade.rb
3
+ #
4
+ # Copyright (c) 1998-2003 Minero Aoki <aamine@loveruby.net>
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the terms of
8
+ # the GNU Lesser General Public License version 2 or later.
9
+ #
10
+
11
+ require 'tmail/utils'
12
+
13
+
14
+ module TMail
15
+
16
+ class Mail
17
+
18
+ def header_string( name, default = nil )
19
+ h = @header[name.downcase] or return default
20
+ h.to_s
21
+ end
22
+
23
+ ###
24
+ ### attributes
25
+ ###
26
+
27
+ include TextUtils
28
+
29
+ def set_string_array_attr( key, strs )
30
+ strs.flatten!
31
+ if strs.empty?
32
+ @header.delete key.downcase
33
+ else
34
+ store key, strs.join(', ')
35
+ end
36
+ strs
37
+ end
38
+ private :set_string_array_attr
39
+
40
+ def set_string_attr( key, str )
41
+ if str
42
+ store key, str
43
+ else
44
+ @header.delete key.downcase
45
+ end
46
+ str
47
+ end
48
+ private :set_string_attr
49
+
50
+ def set_addrfield( name, arg )
51
+ if arg
52
+ h = HeaderField.internal_new(name, @config)
53
+ h.addrs.replace [arg].flatten
54
+ @header[name] = h
55
+ else
56
+ @header.delete name
57
+ end
58
+ arg
59
+ end
60
+ private :set_addrfield
61
+
62
+ def addrs2specs( addrs )
63
+ return nil unless addrs
64
+ list = addrs.map {|addr|
65
+ if addr.address_group?
66
+ then addr.map {|a| a.spec }
67
+ else addr.spec
68
+ end
69
+ }.flatten
70
+ return nil if list.empty?
71
+ list
72
+ end
73
+ private :addrs2specs
74
+
75
+
76
+ #
77
+ # date time
78
+ #
79
+
80
+ def date( default = nil )
81
+ if h = @header['date']
82
+ h.date
83
+ else
84
+ default
85
+ end
86
+ end
87
+
88
+ def date=( time )
89
+ if time
90
+ store 'Date', time2str(time)
91
+ else
92
+ @header.delete 'date'
93
+ end
94
+ time
95
+ end
96
+
97
+ def strftime( fmt, default = nil )
98
+ if t = date
99
+ t.strftime(fmt)
100
+ else
101
+ default
102
+ end
103
+ end
104
+
105
+
106
+ #
107
+ # destination
108
+ #
109
+
110
+ def to_addrs( default = nil )
111
+ if h = @header['to']
112
+ h.addrs
113
+ else
114
+ default
115
+ end
116
+ end
117
+
118
+ def cc_addrs( default = nil )
119
+ if h = @header['cc']
120
+ h.addrs
121
+ else
122
+ default
123
+ end
124
+ end
125
+
126
+ def bcc_addrs( default = nil )
127
+ if h = @header['bcc']
128
+ h.addrs
129
+ else
130
+ default
131
+ end
132
+ end
133
+
134
+ def to_addrs=( arg )
135
+ set_addrfield 'to', arg
136
+ end
137
+
138
+ def cc_addrs=( arg )
139
+ set_addrfield 'cc', arg
140
+ end
141
+
142
+ def bcc_addrs=( arg )
143
+ set_addrfield 'bcc', arg
144
+ end
145
+
146
+ def to( default = nil )
147
+ addrs2specs(to_addrs(nil)) || default
148
+ end
149
+
150
+ def cc( default = nil )
151
+ addrs2specs(cc_addrs(nil)) || default
152
+ end
153
+
154
+ def bcc( default = nil )
155
+ addrs2specs(bcc_addrs(nil)) || default
156
+ end
157
+
158
+ def to=( *strs )
159
+ set_string_array_attr 'To', strs
160
+ end
161
+
162
+ def cc=( *strs )
163
+ set_string_array_attr 'Cc', strs
164
+ end
165
+
166
+ def bcc=( *strs )
167
+ set_string_array_attr 'Bcc', strs
168
+ end
169
+
170
+
171
+ #
172
+ # originator
173
+ #
174
+
175
+ def from_addrs( default = nil )
176
+ if h = @header['from']
177
+ h.addrs
178
+ else
179
+ default
180
+ end
181
+ end
182
+
183
+ def from_addrs=( arg )
184
+ set_addrfield 'from', arg
185
+ end
186
+
187
+ def from( default = nil )
188
+ addrs2specs(from_addrs(nil)) || default
189
+ end
190
+
191
+ def from=( *strs )
192
+ set_string_array_attr 'From', strs
193
+ end
194
+
195
+ def friendly_from( default = nil )
196
+ h = @header['from']
197
+ a, = h.addrs
198
+ return default unless a
199
+ return a.phrase if a.phrase
200
+ return h.comments.join(' ') unless h.comments.empty?
201
+ a.spec
202
+ end
203
+
204
+
205
+ def reply_to_addrs( default = nil )
206
+ if h = @header['reply-to']
207
+ h.addrs
208
+ else
209
+ default
210
+ end
211
+ end
212
+
213
+ def reply_to_addrs=( arg )
214
+ set_addrfield 'reply-to', arg
215
+ end
216
+
217
+ def reply_to( default = nil )
218
+ addrs2specs(reply_to_addrs(nil)) || default
219
+ end
220
+
221
+ def reply_to=( *strs )
222
+ set_string_array_attr 'Reply-To', strs
223
+ end
224
+
225
+
226
+ def sender_addr( default = nil )
227
+ f = @header['sender'] or return default
228
+ f.addr or return default
229
+ end
230
+
231
+ def sender_addr=( addr )
232
+ if addr
233
+ h = HeaderField.internal_new('sender', @config)
234
+ h.addr = addr
235
+ @header['sender'] = h
236
+ else
237
+ @header.delete 'sender'
238
+ end
239
+ addr
240
+ end
241
+
242
+ def sender( default )
243
+ f = @header['sender'] or return default
244
+ a = f.addr or return default
245
+ a.spec
246
+ end
247
+
248
+ def sender=( str )
249
+ set_string_attr 'Sender', str
250
+ end
251
+
252
+
253
+ #
254
+ # subject
255
+ #
256
+
257
+ def subject( default = nil )
258
+ if h = @header['subject']
259
+ h.body
260
+ else
261
+ default
262
+ end
263
+ end
264
+
265
+ def subject=( str )
266
+ set_string_attr 'Subject', str
267
+ end
268
+
269
+
270
+ #
271
+ # identity & threading
272
+ #
273
+
274
+ def message_id( default = nil )
275
+ if h = @header['message-id']
276
+ h.id || default
277
+ else
278
+ default
279
+ end
280
+ end
281
+
282
+ def message_id=( str )
283
+ set_string_attr 'Message-Id', str
284
+ end
285
+
286
+ def in_reply_to( default = nil )
287
+ if h = @header['in-reply-to']
288
+ h.ids
289
+ else
290
+ default
291
+ end
292
+ end
293
+
294
+ def in_reply_to=( *idstrs )
295
+ set_string_array_attr 'In-Reply-To', idstrs
296
+ end
297
+
298
+ def references( default = nil )
299
+ if h = @header['references']
300
+ h.refs
301
+ else
302
+ default
303
+ end
304
+ end
305
+
306
+ def references=( *strs )
307
+ set_string_array_attr 'References', strs
308
+ end
309
+
310
+
311
+ #
312
+ # MIME headers
313
+ #
314
+
315
+ def mime_version( default = nil )
316
+ if h = @header['mime-version']
317
+ h.version || default
318
+ else
319
+ default
320
+ end
321
+ end
322
+
323
+ def mime_version=( m, opt = nil )
324
+ if opt
325
+ if h = @header['mime-version']
326
+ h.major = m
327
+ h.minor = opt
328
+ else
329
+ store 'Mime-Version', "#{m}.#{opt}"
330
+ end
331
+ else
332
+ store 'Mime-Version', m
333
+ end
334
+ m
335
+ end
336
+
337
+
338
+ def content_type( default = nil )
339
+ if h = @header['content-type']
340
+ h.content_type || default
341
+ else
342
+ default
343
+ end
344
+ end
345
+
346
+ def main_type( default = nil )
347
+ if h = @header['content-type']
348
+ h.main_type || default
349
+ else
350
+ default
351
+ end
352
+ end
353
+
354
+ def sub_type( default = nil )
355
+ if h = @header['content-type']
356
+ h.sub_type || default
357
+ else
358
+ default
359
+ end
360
+ end
361
+
362
+ def set_content_type( str, sub = nil, param = nil )
363
+ if sub
364
+ main, sub = str, sub
365
+ else
366
+ main, sub = str.split(%r</>, 2)
367
+ raise ArgumentError, "sub type missing: #{str.inspect}" unless sub
368
+ end
369
+ if h = @header['content-type']
370
+ h.main_type = main
371
+ h.sub_type = sub
372
+ h.params.clear
373
+ else
374
+ store 'Content-Type', "#{main}/#{sub}"
375
+ end
376
+ @header['content-type'].params.replace param if param
377
+
378
+ str
379
+ end
380
+
381
+ alias content_type= set_content_type
382
+
383
+ def type_param( name, default = nil )
384
+ if h = @header['content-type']
385
+ h[name] || default
386
+ else
387
+ default
388
+ end
389
+ end
390
+
391
+ def charset( default = nil )
392
+ if h = @header['content-type']
393
+ h['charset'] or default
394
+ else
395
+ default
396
+ end
397
+ end
398
+
399
+ def charset=( str )
400
+ if str
401
+ if h = @header[ 'content-type' ]
402
+ h['charset'] = str
403
+ else
404
+ store 'Content-Type', "text/plain; charset=#{str}"
405
+ end
406
+ end
407
+ str
408
+ end
409
+
410
+
411
+ def transfer_encoding( default = nil )
412
+ if h = @header['content-transfer-encoding']
413
+ h.encoding || default
414
+ else
415
+ default
416
+ end
417
+ end
418
+
419
+ def transfer_encoding=( str )
420
+ set_string_attr 'Content-Transfer-Encoding', str
421
+ end
422
+
423
+ alias encoding transfer_encoding
424
+ alias encoding= transfer_encoding=
425
+ alias content_transfer_encoding transfer_encoding
426
+ alias content_transfer_encoding= transfer_encoding=
427
+
428
+
429
+ def disposition( default = nil )
430
+ if h = @header['content-disposition']
431
+ h.disposition || default
432
+ else
433
+ default
434
+ end
435
+ end
436
+
437
+ alias content_disposition disposition
438
+
439
+ def set_disposition( str, params = nil )
440
+ if h = @header['content-disposition']
441
+ h.disposition = str
442
+ h.params.clear
443
+ else
444
+ h = store('Content-Disposition', str)
445
+ end
446
+ h.params.replace params if params
447
+ end
448
+
449
+ alias disposition= set_disposition
450
+ alias set_content_disposition set_disposition
451
+ alias content_disposition= set_disposition
452
+
453
+ def disposition_param( name, default = nil )
454
+ if h = @header['content-disposition']
455
+ h[name] || default
456
+ else
457
+ default
458
+ end
459
+ end
460
+
461
+ ###
462
+ ### utils
463
+ ###
464
+
465
+ def create_reply
466
+ mail = TMail::Mail.parse('')
467
+ mail.subject = 'Re: ' + subject('').sub(/\A(?:\[[^\]]+\])?(?:\s*Re:)*\s*/i, '')
468
+ mail.to_addrs = reply_addresses([])
469
+ mail.in_reply_to = [message_id(nil)].compact
470
+ mail.references = references([]) + [message_id(nil)].compact
471
+ mail.mime_version = '1.0'
472
+ mail
473
+ end
474
+
475
+
476
+ def base64_encode
477
+ store 'Content-Transfer-Encoding', 'Base64'
478
+ self.body = Base64.folding_encode(self.body)
479
+ end
480
+
481
+ def base64_decode
482
+ if /base64/i === self.transfer_encoding('')
483
+ store 'Content-Transfer-Encoding', '8bit'
484
+ self.body = Base64.decode(self.body, @config.strict_base64decode?)
485
+ end
486
+ end
487
+
488
+
489
+ def destinations( default = nil )
490
+ ret = []
491
+ %w( to cc bcc ).each do |nm|
492
+ if h = @header[nm]
493
+ h.addrs.each {|i| ret.push i.address }
494
+ end
495
+ end
496
+ ret.empty? ? default : ret
497
+ end
498
+
499
+ def each_destination( &block )
500
+ destinations([]).each do |i|
501
+ if Address === i
502
+ yield i
503
+ else
504
+ i.each(&block)
505
+ end
506
+ end
507
+ end
508
+
509
+ alias each_dest each_destination
510
+
511
+
512
+ def reply_addresses( default = nil )
513
+ reply_to_addrs(nil) or from_addrs(nil) or default
514
+ end
515
+
516
+ def error_reply_addresses( default = nil )
517
+ if s = sender(nil)
518
+ [s]
519
+ else
520
+ from_addrs(default)
521
+ end
522
+ end
523
+
524
+
525
+ def multipart?
526
+ main_type('').downcase == 'multipart'
527
+ end
528
+
529
+ end # class Mail
530
+
531
+ end # module TMail