Class: TMail::Mail
- Inherits:
-
Object
show all
- Includes:
- StrategyInterface, TextUtils
- Defined in:
- lib/tmail/obsolete.rb,
lib/tmail/net.rb,
lib/tmail/mail.rb,
lib/tmail/quoting.rb,
lib/tmail/obsolete.rb,
lib/tmail/interface.rb,
lib/tmail/attachments.rb
Overview
Constant Summary
collapse
- NOSEND_FIELDS =
%w(
received
bcc
)
- ALLOW_MULTIPLE =
{
'received' => true,
'resent-date' => true,
'resent-from' => true,
'resent-sender' => true,
'resent-to' => true,
'resent-cc' => true,
'resent-bcc' => true,
'resent-message-id' => true,
'comments' => true,
'keywords' => true
}
- USE_ARRAY =
ALLOW_MULTIPLE
- FIELD_ORDER =
%w(
return-path received
resent-date resent-from resent-sender resent-to
resent-cc resent-bcc resent-message-id
date from sender reply-to to cc bcc
message-id in-reply-to references
subject comments keywords
mime-version content-type content-transfer-encoding
content-disposition content-description
)
Constants included
from TextUtils
TextUtils::ATOM_UNSAFE, TextUtils::CONTROL_CHAR, TextUtils::MESSAGE_ID, TextUtils::MIME_ENCODED, TextUtils::MONTH, TextUtils::NKF_FLAGS, TextUtils::PHRASE_UNSAFE, TextUtils::RFC2231_ENCODED, TextUtils::TOKEN_UNSAFE, TextUtils::WDAY, TextUtils::ZONESTR_TABLE
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](key) ⇒ Object
(also: #fetch)
Returns a TMail::AddressHeader object of the field you are querying.
-
#[]=(key, val) ⇒ Object
(also: #store)
Allows you to set or delete TMail header objects at will.
-
#accept(strategy) ⇒ Object
-
#add_date ⇒ Object
-
#add_message_id(fqdn = nil) ⇒ Object
-
#attachment?(part) ⇒ Boolean
-
#attachments ⇒ Object
-
#base64_decode ⇒ Object
-
#base64_encode ⇒ Object
-
#bcc(default = nil) ⇒ Object
-
#bcc=(*strs) ⇒ Object
-
#bcc_addrs(default = nil) ⇒ Object
-
#bcc_addrs=(arg) ⇒ Object
-
#body(to_charset = 'utf-8', &block) ⇒ Object
-
#body=(str) ⇒ Object
(also: #preamble=)
-
#body_port ⇒ Object
-
#cc(default = nil) ⇒ Object
-
#cc=(*strs) ⇒ Object
-
#cc_addrs(default = nil) ⇒ Object
-
#cc_addrs=(arg) ⇒ Object
-
#charset(default = nil) ⇒ Object
-
#charset=(str) ⇒ Object
-
#clear ⇒ Object
-
#content_type(default = nil) ⇒ Object
-
#create_empty_mail ⇒ Object
-
#create_forward ⇒ Object
-
#create_reply ⇒ Object
-
#date(default = nil) ⇒ Object
-
#date=(time) ⇒ Object
-
#delete(key) ⇒ Object
-
#delete_if ⇒ Object
-
#delete_no_send_fields ⇒ Object
-
#destinations(default = nil) ⇒ Object
-
#disposition(default = nil) ⇒ Object
(also: #content_disposition)
-
#disposition_param(name, default = nil) ⇒ Object
-
#each(&block) ⇒ Object
-
#each_destination(&block) ⇒ Object
(also: #each_dest)
-
#each_field(&block) ⇒ Object
(also: #each_value)
-
#each_header ⇒ Object
(also: #each_pair)
-
#each_header_name(&block) ⇒ Object
(also: #each_key)
-
#each_part(&block) ⇒ Object
-
#epilogue ⇒ Object
-
#epilogue=(str) ⇒ Object
-
#error_reply_addresses(default = nil) ⇒ Object
-
#friendly_from(default = nil) ⇒ Object
-
#from(default = nil) ⇒ Object
-
#from=(*strs) ⇒ Object
-
#from_addr(default = nil) ⇒ Object
-
#from_address(default = nil) ⇒ Object
-
#from_address= ⇒ Object
-
#from_addrs(default = nil) ⇒ Object
-
#from_addrs=(arg) ⇒ Object
-
#from_phrase(default = nil) ⇒ Object
-
#has_attachments? ⇒ Boolean
-
#header ⇒ Object
-
#header_string(name, default = nil) ⇒ Object
-
#in_reply_to(default = nil) ⇒ Object
-
#in_reply_to=(*idstrs) ⇒ Object
-
#initialize(port = nil, conf = DEFAULT_CONFIG) ⇒ Mail
constructor
-
#inspect ⇒ Object
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?)
-
#keys ⇒ Object
-
#main_type(default = nil) ⇒ Object
-
#message_id(default = nil) ⇒ Object
-
#message_id=(str) ⇒ Object
-
#mime_encode ⇒ Object
-
#mime_encode_binary(body) ⇒ Object
-
#mime_encode_multipart(top = true) ⇒ Object
-
#mime_encode_singlepart ⇒ Object
-
#mime_encode_text(body) ⇒ Object
-
#mime_version(default = nil) ⇒ Object
-
#mime_version=(m, opt = nil) ⇒ Object
-
#msgid ⇒ Object
-
#msgid= ⇒ Object
-
#multipart? ⇒ Boolean
-
#ordered_each ⇒ Object
-
#parts ⇒ Object
-
#preamble ⇒ Object
-
#quoted_body ⇒ Object
-
#ready_to_send ⇒ Object
-
#references(default = nil) ⇒ Object
-
#references=(*strs) ⇒ Object
-
#reply_addresses(default = nil) ⇒ Object
-
#reply_to(default = nil) ⇒ Object
-
#reply_to=(*strs) ⇒ Object
-
#reply_to_addrs(default = nil) ⇒ Object
-
#reply_to_addrs=(arg) ⇒ Object
-
#send_text_to(smtp) ⇒ Object
-
#send_to(smtp) ⇒ Object
-
#send_to_0(smtp, from, to) ⇒ Object
-
#sender(default) ⇒ Object
-
#sender=(str) ⇒ Object
-
#sender_addr(default = nil) ⇒ Object
-
#sender_addr=(addr) ⇒ Object
-
#set_content_type(str, sub = nil, param = nil) ⇒ Object
(also: #content_type=)
-
#set_disposition(str, params = nil) ⇒ Object
(also: #disposition=, #set_content_disposition, #content_disposition=)
-
#setup_forward(mail) ⇒ Object
-
#setup_reply(m) ⇒ Object
-
#strftime(fmt, default = nil) ⇒ Object
-
#sub_header(key, param) ⇒ Object
-
#sub_type(default = nil) ⇒ Object
-
#subject(default = nil) ⇒ Object
(also: #quoted_subject)
-
#subject=(str) ⇒ Object
-
#to(default = nil) ⇒ Object
-
#to=(*strs) ⇒ Object
-
#to_addrs(default = nil) ⇒ Object
-
#to_addrs=(arg) ⇒ Object
-
#transfer_encoding(default = nil) ⇒ Object
(also: #encoding, #content_transfer_encoding)
-
#transfer_encoding=(str) ⇒ Object
(also: #encoding=, #content_transfer_encoding=)
-
#type_param(name, default = nil) ⇒ Object
-
#unquoted_body(to_charset = 'utf-8') ⇒ Object
-
#value?(val) ⇒ Boolean
(also: #has_value?)
-
#values ⇒ Object
-
#values_at(*args) ⇒ Object
(also: #indexes, #indices)
-
#write_back(eol = "\n", charset = 'e') ⇒ Object
Methods included from TextUtils
#atom_safe?, #decode_RFC2231, #decode_params, #join_domain, #message_id?, #mime_encoded?, #quote_atom, #quote_boundary, #quote_phrase, #quote_token, #time2str, #timezone_string_to_unixtime, #to_kcode, #token_safe?, #unquote
#accept_strategy, create_dest, #decoded, #encoded
Constructor Details
#initialize(port = nil, conf = DEFAULT_CONFIG) ⇒ Mail
Returns a new instance of Mail.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/tmail/mail.rb', line 77
def initialize( port = nil, conf = DEFAULT_CONFIG )
@port = port || StringPort.new
@config = Config.to_config(conf)
@header = {}
@body_port = nil
@body_parsed = false
@epilogue = ''
@parts = []
@port.ropen {|f|
f
parse_body f unless @port.reproducible?
}
end
|
Instance Attribute Details
Returns the value of attribute port.
93
94
95
|
# File 'lib/tmail/mail.rb', line 93
def port
@port
end
|
Class Method Details
.load(fname) ⇒ Object
Also known as:
load_from, loadfrom
64
65
66
|
# File 'lib/tmail/mail.rb', line 64
def load( fname )
new(FilePort.new(fname))
end
|
.parse(str) ⇒ Object
71
72
73
|
# File 'lib/tmail/mail.rb', line 71
def parse( str )
new(StringPort.new(str))
end
|
Instance Method Details
#[](key) ⇒ Object
Also known as:
fetch
Returns a TMail::AddressHeader object of the field you are querying. Examples:
@mail['from'] @mail['to']
You can get the string value of this by passing “to_s” to the query: Example:
@mail['to'].to_s
190
191
192
|
# File 'lib/tmail/mail.rb', line 190
def []( key )
@header[key.downcase]
end
|
#[]=(key, val) ⇒ Object
Also known as:
store
Allows you to set or delete TMail header objects at will. Eamples:
@mail = TMail::Mail.new
@mail['to'].to_s @mail['to'] = '[email protected]'
@mail['to'].to_s @mail.encoded @mail['to'] = nil
@mail['to'].to_s @mail.encoded
Note: setting mail[] = nil actualy deletes the header field in question from the object, it does not just set the value of the hash to nil
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/tmail/mail.rb', line 213
def []=( key, val )
dkey = key.downcase
if val.nil?
@header.delete dkey
return nil
end
case val
when String
= new_hf(key, val)
when HeaderField
;
when Array
ALLOW_MULTIPLE.include? dkey or
raise ArgumentError, "#{key}: Header must not be multiple"
@header[dkey] = val
return val
else
= new_hf(key, val.to_s)
end
if ALLOW_MULTIPLE.include? dkey
(@header[dkey] ||= []).push
else
@header[dkey] =
end
val
end
|
#accept(strategy) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/tmail/mail.rb', line 112
def accept( strategy )
with_multipart_encoding(strategy) {
ordered_each do |name, field|
next if field.empty?
strategy. canonical(name)
field.accept strategy
strategy.puts
end
strategy.puts
body_port().ropen {|r|
strategy.write r.read
}
}
end
|
88
89
90
|
# File 'lib/tmail/net.rb', line 88
def add_date
self.date = Time.now
end
|
#add_message_id(fqdn = nil) ⇒ Object
84
85
86
|
# File 'lib/tmail/net.rb', line 84
def add_message_id( fqdn = nil )
self.message_id = ::TMail::new_message_id(fqdn)
end
|
#attachment?(part) ⇒ Boolean
19
20
21
22
|
# File 'lib/tmail/attachments.rb', line 19
def attachment?(part)
(part['content-disposition'] && part['content-disposition'].disposition == "attachment") ||
part.['content-type'].main_type != "text"
end
|
#attachments ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/tmail/attachments.rb', line 24
def attachments
if multipart?
parts.collect { |part|
if part.multipart?
part.attachments
elsif attachment?(part)
content = part.body file_name = (part['content-location'] &&
part['content-location'].body) ||
part.("content-type", "name") ||
part.("content-disposition", "filename")
next if file_name.blank? || content.blank?
attachment = Attachment.new(content)
attachment.original_filename = file_name.strip
attachment.content_type = part.content_type
attachment
end
}.flatten.compact
end
end
|
#base64_decode ⇒ Object
493
494
495
496
497
498
|
# File 'lib/tmail/interface.rb', line 493
def base64_decode
if /base64/i === self.transfer_encoding('')
store 'Content-Transfer-Encoding', '8bit'
self.body = Base64.decode(self.body, @config.strict_base64decode?)
end
end
|
#base64_encode ⇒ Object
488
489
490
491
|
# File 'lib/tmail/interface.rb', line 488
def base64_encode
store 'Content-Transfer-Encoding', 'Base64'
self.body = Base64.folding_encode(self.body)
end
|
#bcc(default = nil) ⇒ Object
172
173
174
|
# File 'lib/tmail/interface.rb', line 172
def bcc( default = nil )
addrs2specs(bcc_addrs(nil)) || default
end
|
#bcc=(*strs) ⇒ Object
184
185
186
|
# File 'lib/tmail/interface.rb', line 184
def bcc=( *strs )
set_string_array_attr 'Bcc', strs
end
|
#bcc_addrs(default = nil) ⇒ Object
144
145
146
147
148
149
150
|
# File 'lib/tmail/interface.rb', line 144
def bcc_addrs( default = nil )
if h = @header['bcc']
h.addrs
else
default
end
end
|
#bcc_addrs=(arg) ⇒ Object
160
161
162
|
# File 'lib/tmail/interface.rb', line 160
def bcc_addrs=( arg )
set_addrfield 'bcc', arg
end
|
#body(to_charset = 'utf-8', &block) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/tmail/quoting.rb', line 36
def body(to_charset = 'utf-8', &block)
attachment_presenter = block || Proc.new { |file_name| "Attachment: #{file_name}\n" }
if multipart?
parts.collect { |part|
= part["content-type"]
if part.multipart?
part.body(to_charset, &attachment_presenter)
elsif .nil?
""
elsif !attachment?(part)
part.unquoted_body(to_charset)
else
attachment_presenter.call(["name"] || "(unnamed)")
end
}.join
else
unquoted_body(to_charset)
end
end
|
#body=(str) ⇒ Object
Also known as:
preamble=
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
# File 'lib/tmail/mail.rb', line 404
def body=( str )
@body_parsed = false
parse_body(StringInput.new(str))
parse_body
@body_port.wopen {|f| f.write str }
str
end
|
#body_port ⇒ Object
388
389
390
391
|
# File 'lib/tmail/mail.rb', line 388
def body_port
parse_body
@body_port
end
|
#cc(default = nil) ⇒ Object
168
169
170
|
# File 'lib/tmail/interface.rb', line 168
def cc( default = nil )
addrs2specs(cc_addrs(nil)) || default
end
|
#cc=(*strs) ⇒ Object
180
181
182
|
# File 'lib/tmail/interface.rb', line 180
def cc=( *strs )
set_string_array_attr 'Cc', strs
end
|
#cc_addrs(default = nil) ⇒ Object
136
137
138
139
140
141
142
|
# File 'lib/tmail/interface.rb', line 136
def cc_addrs( default = nil )
if h = @header['cc']
h.addrs
else
default
end
end
|
#cc_addrs=(arg) ⇒ Object
156
157
158
|
# File 'lib/tmail/interface.rb', line 156
def cc_addrs=( arg )
set_addrfield 'cc', arg
end
|
#charset(default = nil) ⇒ Object
405
406
407
408
409
410
411
|
# File 'lib/tmail/interface.rb', line 405
def charset( default = nil )
if h = @header['content-type']
h['charset'] or default
else
default
end
end
|
#charset=(str) ⇒ Object
413
414
415
416
417
418
419
420
421
422
|
# File 'lib/tmail/interface.rb', line 413
def charset=( str )
if str
if h = @header[ 'content-type' ]
h['charset'] = str
else
store 'Content-Type', "text/plain; charset=#{str}"
end
end
str
end
|
295
296
297
|
# File 'lib/tmail/mail.rb', line 295
def clear
@header.clear
end
|
#content_type(default = nil) ⇒ Object
353
354
355
356
357
358
359
|
# File 'lib/tmail/interface.rb', line 353
def content_type( default = nil )
if h = @header['content-type']
h.content_type || default
else
default
end
end
|
#create_empty_mail ⇒ Object
132
133
134
|
# File 'lib/tmail/net.rb', line 132
def create_empty_mail
self.class.new(StringPort.new(''), @config)
end
|
#create_forward ⇒ Object
155
156
157
|
# File 'lib/tmail/net.rb', line 155
def create_forward
setup_forward create_empty_mail()
end
|
#create_reply ⇒ Object
478
479
480
|
# File 'lib/tmail/interface.rb', line 478
def create_reply
setup_reply create_empty_mail()
end
|
#date(default = nil) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/tmail/interface.rb', line 99
def date( default = nil )
if h = @header['date']
h.date
else
default
end
end
|
#date=(time) ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/tmail/interface.rb', line 107
def date=( time )
if time
store 'Date', time2str(time)
else
@header.delete 'date'
end
time
end
|
#delete(key) ⇒ Object
299
300
301
|
# File 'lib/tmail/mail.rb', line 299
def delete( key )
@header.delete key.downcase
end
|
#delete_if ⇒ Object
303
304
305
306
307
308
309
310
311
312
|
# File 'lib/tmail/mail.rb', line 303
def delete_if
@header.delete_if do |key,val|
if Array === val
val.delete_if {|v| yield key, v }
val.empty?
else
yield key, val
end
end
end
|
#delete_no_send_fields ⇒ Object
77
78
79
80
81
82
|
# File 'lib/tmail/net.rb', line 77
def delete_no_send_fields
NOSEND_FIELDS.each do |nm|
delete nm
end
delete_if {|n,v| v.empty? }
end
|
#destinations(default = nil) ⇒ Object
500
501
502
503
504
505
506
507
508
|
# File 'lib/tmail/interface.rb', line 500
def destinations( default = nil )
ret = []
%w( to cc bcc ).each do |nm|
if h = @header[nm]
h.addrs.each {|i| ret.push i.address }
end
end
ret.empty? ? default : ret
end
|
#disposition(default = nil) ⇒ Object
Also known as:
content_disposition
441
442
443
444
445
446
447
|
# File 'lib/tmail/interface.rb', line 441
def disposition( default = nil )
if h = @header['content-disposition']
h.disposition || default
else
default
end
end
|
#disposition_param(name, default = nil) ⇒ Object
466
467
468
469
470
471
472
|
# File 'lib/tmail/interface.rb', line 466
def disposition_param( name, default = nil )
if h = @header['content-disposition']
h[name] || default
else
default
end
end
|
#each(&block) ⇒ Object
393
394
395
|
# File 'lib/tmail/mail.rb', line 393
def each( &block )
body_port().ropen {|f| f.each(&block) }
end
|
#each_destination(&block) ⇒ Object
Also known as:
each_dest
510
511
512
513
514
515
516
517
518
|
# File 'lib/tmail/interface.rb', line 510
def each_destination( &block )
destinations([]).each do |i|
if Address === i
yield i
else
i.each(&block)
end
end
end
|
#each_field(&block) ⇒ Object
Also known as:
each_value
266
267
268
|
# File 'lib/tmail/mail.rb', line 266
def each_field( &block )
@header.values.flatten.each(&block)
end
|
Allows you to loop through each header in the TMail::Mail object in a block Example:
@mail['to'] = '[email protected]'
@mail['from'] = '[email protected]'
@mail. { |k,v| puts "#{k} = #{v}" }
252
253
254
255
256
|
# File 'lib/tmail/mail.rb', line 252
def
@header.each do |key, val|
[val].flatten.each {|v| yield key, v }
end
end
|
260
261
262
|
# File 'lib/tmail/mail.rb', line 260
def ( &block )
@header.each_key(&block)
end
|
#each_part(&block) ⇒ Object
442
443
444
|
# File 'lib/tmail/mail.rb', line 442
def each_part( &block )
parts().each(&block)
end
|
426
427
428
429
|
# File 'lib/tmail/mail.rb', line 426
def epilogue
parse_body
@epilogue.dup
end
|
#epilogue=(str) ⇒ Object
431
432
433
434
435
|
# File 'lib/tmail/mail.rb', line 431
def epilogue=( str )
parse_body
@epilogue = str
str
end
|
#error_reply_addresses(default = nil) ⇒ Object
526
527
528
529
530
531
532
|
# File 'lib/tmail/interface.rb', line 526
def error_reply_addresses( default = nil )
if s = sender(nil)
[s]
else
from_addrs(default)
end
end
|
#friendly_from(default = nil) ⇒ Object
212
213
214
215
216
217
218
219
|
# File 'lib/tmail/interface.rb', line 212
def friendly_from( default = nil )
h = @header['from']
a, = h.addrs
return default unless a
return a.phrase if a.phrase
return h..join(' ') unless h..empty?
a.spec
end
|
#from(default = nil) ⇒ Object
204
205
206
|
# File 'lib/tmail/interface.rb', line 204
def from( default = nil )
addrs2specs(from_addrs(nil)) || default
end
|
#from=(*strs) ⇒ Object
208
209
210
|
# File 'lib/tmail/interface.rb', line 208
def from=( *strs )
set_string_array_attr 'From', strs
end
|
#from_addr(default = nil) ⇒ Object
57
58
59
60
|
# File 'lib/tmail/obsolete.rb', line 57
def from_addr( default = nil )
addr, = from_addrs(nil)
addr || default
end
|
#from_address(default = nil) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/tmail/obsolete.rb', line 62
def from_address( default = nil )
if a = from_addr(nil)
a.spec
else
default
end
end
|
#from_address= ⇒ Object
70
|
# File 'lib/tmail/obsolete.rb', line 70
alias from_address= from_addrs=
|
#from_addrs(default = nil) ⇒ Object
192
193
194
195
196
197
198
|
# File 'lib/tmail/interface.rb', line 192
def from_addrs( default = nil )
if h = @header['from']
h.addrs
else
default
end
end
|
#from_addrs=(arg) ⇒ Object
200
201
202
|
# File 'lib/tmail/interface.rb', line 200
def from_addrs=( arg )
set_addrfield 'from', arg
end
|
#from_phrase(default = nil) ⇒ Object
72
73
74
75
76
77
78
|
# File 'lib/tmail/obsolete.rb', line 72
def from_phrase( default = nil )
if a = from_addr(nil)
a.phrase
else
default
end
end
|
#has_attachments? ⇒ Boolean
15
16
17
|
# File 'lib/tmail/attachments.rb', line 15
def has_attachments?
multipart? && parts.any? { |part| attachment?(part) }
end
|
178
179
180
|
# File 'lib/tmail/mail.rb', line 178
def
@header.dup
end
|
38
39
40
41
|
# File 'lib/tmail/interface.rb', line 38
def ( name, default = nil )
h = @header[name.downcase] or return default
h.to_s
end
|
#in_reply_to(default = nil) ⇒ Object
303
304
305
306
307
308
309
|
# File 'lib/tmail/interface.rb', line 303
def in_reply_to( default = nil )
if h = @header['in-reply-to']
h.ids
else
default
end
end
|
#in_reply_to=(*idstrs) ⇒ Object
311
312
313
|
# File 'lib/tmail/interface.rb', line 311
def in_reply_to=( *idstrs )
set_string_array_attr 'In-Reply-To', idstrs
end
|
95
96
97
|
# File 'lib/tmail/mail.rb', line 95
def inspect
"\#<#{self.class} port=#{@port.inspect} bodyport=#{@body_port.inspect}>"
end
|
#key?(key) ⇒ Boolean
Also known as:
include?, has_key?
318
319
320
|
# File 'lib/tmail/mail.rb', line 318
def key?( key )
@header.key? key.downcase
end
|
314
315
316
|
# File 'lib/tmail/mail.rb', line 314
def keys
@header.keys
end
|
#main_type(default = nil) ⇒ Object
361
362
363
364
365
366
367
|
# File 'lib/tmail/interface.rb', line 361
def main_type( default = nil )
if h = @header['content-type']
h.main_type || default
else
default
end
end
|
#message_id(default = nil) ⇒ Object
291
292
293
294
295
296
297
|
# File 'lib/tmail/interface.rb', line 291
def message_id( default = nil )
if h = @header['message-id']
h.id || default
else
default
end
end
|
#message_id=(str) ⇒ Object
299
300
301
|
# File 'lib/tmail/interface.rb', line 299
def message_id=( str )
set_string_attr 'Message-Id', str
end
|
#mime_encode ⇒ Object
92
93
94
95
96
97
98
|
# File 'lib/tmail/net.rb', line 92
def mime_encode
if parts.empty?
mime_encode_singlepart
else
mime_encode_multipart true
end
end
|
#mime_encode_binary(body) ⇒ Object
116
117
118
119
120
|
# File 'lib/tmail/net.rb', line 116
def mime_encode_binary( body )
self.body = [body].pack('m')
self.set_content_type 'application', 'octet-stream'
self.encoding = 'Base64'
end
|
#mime_encode_multipart(top = true) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/tmail/net.rb', line 122
def mime_encode_multipart( top = true )
self.mime_version = '1.0' if top
self.set_content_type 'multipart', 'mixed'
e = encoding(nil)
if e and not /\A(?:7bit|8bit|binary)\z/i === e
raise ArgumentError,
'using C.T.Encoding with multipart mail is not permitted'
end
end
|
#mime_encode_singlepart ⇒ Object
100
101
102
103
104
105
106
107
108
|
# File 'lib/tmail/net.rb', line 100
def mime_encode_singlepart
self.mime_version = '1.0'
b = body
if NKF.guess(b) != NKF::BINARY
mime_encode_text b
else
mime_encode_binary b
end
end
|
#mime_encode_text(body) ⇒ Object
110
111
112
113
114
|
# File 'lib/tmail/net.rb', line 110
def mime_encode_text( body )
self.body = NKF.nkf('-j -m0', body)
self.set_content_type 'text', 'plain', {'charset' => 'iso-2022-jp'}
self.encoding = '7bit'
end
|
#mime_version(default = nil) ⇒ Object
331
332
333
334
335
336
337
|
# File 'lib/tmail/interface.rb', line 331
def mime_version( default = nil )
if h = @header['mime-version']
h.version || default
else
default
end
end
|
#mime_version=(m, opt = nil) ⇒ Object
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/tmail/interface.rb', line 339
def mime_version=( m, opt = nil )
if opt
if h = @header['mime-version']
h.major = m
h.minor = opt
else
store 'Mime-Version', "#{m}.#{opt}"
end
else
store 'Mime-Version', m
end
m
end
|
80
|
# File 'lib/tmail/obsolete.rb', line 80
alias msgid message_id
|
81
|
# File 'lib/tmail/obsolete.rb', line 81
alias msgid= message_id=
|
#multipart? ⇒ Boolean
534
535
536
|
# File 'lib/tmail/interface.rb', line 534
def multipart?
main_type('').downcase == 'multipart'
end
|
#ordered_each ⇒ Object
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/tmail/mail.rb', line 283
def ordered_each
list = @header.keys
FIELD_ORDER.each do |name|
if list.delete(name)
[@header[name]].flatten.each {|v| yield name, v }
end
end
list.each do |name|
[@header[name]].flatten.each {|v| yield name, v }
end
end
|
437
438
439
440
|
# File 'lib/tmail/mail.rb', line 437
def parts
parse_body
@parts
end
|
423
|
# File 'lib/tmail/mail.rb', line 423
alias preamble body
|
#quoted_body ⇒ Object
397
398
399
400
401
402
|
# File 'lib/tmail/mail.rb', line 397
def quoted_body
parse_body
@body_port.ropen {|f|
return f.read
}
end
|
#ready_to_send ⇒ Object
66
67
68
69
70
|
# File 'lib/tmail/net.rb', line 66
def ready_to_send
delete_no_send_fields
add_message_id
add_date
end
|
#references(default = nil) ⇒ Object
315
316
317
318
319
320
321
|
# File 'lib/tmail/interface.rb', line 315
def references( default = nil )
if h = @header['references']
h.refs
else
default
end
end
|
#references=(*strs) ⇒ Object
323
324
325
|
# File 'lib/tmail/interface.rb', line 323
def references=( *strs )
set_string_array_attr 'References', strs
end
|
#reply_addresses(default = nil) ⇒ Object
522
523
524
|
# File 'lib/tmail/interface.rb', line 522
def reply_addresses( default = nil )
reply_to_addrs(nil) or from_addrs(nil) or default
end
|
#reply_to(default = nil) ⇒ Object
234
235
236
|
# File 'lib/tmail/interface.rb', line 234
def reply_to( default = nil )
addrs2specs(reply_to_addrs(nil)) || default
end
|
#reply_to=(*strs) ⇒ Object
238
239
240
|
# File 'lib/tmail/interface.rb', line 238
def reply_to=( *strs )
set_string_array_attr 'Reply-To', strs
end
|
#reply_to_addrs(default = nil) ⇒ Object
222
223
224
225
226
227
228
|
# File 'lib/tmail/interface.rb', line 222
def reply_to_addrs( default = nil )
if h = @header['reply-to']
h.addrs
else
default
end
end
|
#reply_to_addrs=(arg) ⇒ Object
230
231
232
|
# File 'lib/tmail/interface.rb', line 230
def reply_to_addrs=( arg )
set_addrfield 'reply-to', arg
end
|
#send_text_to(smtp) ⇒ Object
45
46
47
48
49
50
|
# File 'lib/tmail/net.rb', line 45
def send_text_to( smtp )
do_send_to(smtp) do
ready_to_send
mime_encode
end
end
|
#send_to(smtp) ⇒ Object
39
40
41
42
43
|
# File 'lib/tmail/net.rb', line 39
def send_to( smtp )
do_send_to(smtp) do
ready_to_send
end
end
|
#send_to_0(smtp, from, to) ⇒ Object
60
61
62
63
64
|
# File 'lib/tmail/net.rb', line 60
def send_to_0( smtp, from, to )
smtp.ready(from, to) do |f|
encoded "\r\n", 'j', f, ''
end
end
|
#sender(default) ⇒ Object
259
260
261
262
263
|
# File 'lib/tmail/interface.rb', line 259
def sender( default )
f = @header['sender'] or return default
a = f.addr or return default
a.spec
end
|
#sender=(str) ⇒ Object
265
266
267
|
# File 'lib/tmail/interface.rb', line 265
def sender=( str )
set_string_attr 'Sender', str
end
|
#sender_addr(default = nil) ⇒ Object
243
244
245
246
|
# File 'lib/tmail/interface.rb', line 243
def sender_addr( default = nil )
f = @header['sender'] or return default
f.addr or return default
end
|
#sender_addr=(addr) ⇒ Object
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/tmail/interface.rb', line 248
def sender_addr=( addr )
if addr
h = HeaderField.internal_new('sender', @config)
h.addr = addr
@header['sender'] = h
else
@header.delete 'sender'
end
addr
end
|
#set_content_type(str, sub = nil, param = nil) ⇒ Object
Also known as:
content_type=
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
# File 'lib/tmail/interface.rb', line 377
def set_content_type( str, sub = nil, param = nil )
if sub
main, sub = str, sub
else
main, sub = str.split(%r</>, 2)
raise ArgumentError, "sub type missing: #{str.inspect}" unless sub
end
if h = @header['content-type']
h.main_type = main
h.sub_type = sub
h.params.clear
else
store 'Content-Type', "#{main}/#{sub}"
end
@header['content-type'].params.replace param if param
str
end
|
#set_disposition(str, params = nil) ⇒ Object
Also known as:
disposition=, set_content_disposition, content_disposition=
451
452
453
454
455
456
457
458
459
460
|
# File 'lib/tmail/interface.rb', line 451
def set_disposition( str, params = nil )
if h = @header['content-disposition']
h.disposition = str
h.params.clear
else
store('Content-Disposition', str)
h = @header['content-disposition']
end
h.params.replace params if params
end
|
#setup_forward(mail) ⇒ Object
159
160
161
162
163
164
165
|
# File 'lib/tmail/net.rb', line 159
def setup_forward( mail )
m = Mail.new(StringPort.new(''))
m.body = decoded
m.set_content_type 'message', 'rfc822'
m.encoding = encoding('7bit')
mail.parts.push m
end
|
#setup_reply(m) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/tmail/net.rb', line 140
def setup_reply( m )
if tmp = reply_addresses(nil)
m.to_addrs = tmp
end
mid = message_id(nil)
tmp = references(nil) || []
tmp.push mid if mid
m.in_reply_to = [mid] if mid
m.references = tmp unless tmp.empty?
m.subject = 'Re: ' + subject('').sub(/\A(?:\s*re:)+/i, '')
m
end
|
#strftime(fmt, default = nil) ⇒ Object
116
117
118
119
120
121
122
|
# File 'lib/tmail/interface.rb', line 116
def strftime( fmt, default = nil )
if t = date
t.strftime(fmt)
else
default
end
end
|
194
195
196
|
# File 'lib/tmail/mail.rb', line 194
def (key, param)
(hdr = self[key]) ? hdr[param] : nil
end
|
#sub_type(default = nil) ⇒ Object
369
370
371
372
373
374
375
|
# File 'lib/tmail/interface.rb', line 369
def sub_type( default = nil )
if h = @header['content-type']
h.sub_type || default
else
default
end
end
|
#subject(default = nil) ⇒ Object
Also known as:
quoted_subject
274
275
276
|
# File 'lib/tmail/interface.rb', line 274
def subject(to_charset = 'utf-8')
Unquoter.unquote_and_convert_to(quoted_subject, to_charset)
end
|
#subject=(str) ⇒ Object
283
284
285
|
# File 'lib/tmail/interface.rb', line 283
def subject=( str )
set_string_attr 'Subject', str
end
|
#to(default = nil) ⇒ Object
164
165
166
|
# File 'lib/tmail/interface.rb', line 164
def to( default = nil )
addrs2specs(to_addrs(nil)) || default
end
|
#to=(*strs) ⇒ Object
176
177
178
|
# File 'lib/tmail/interface.rb', line 176
def to=( *strs )
set_string_array_attr 'To', strs
end
|
#to_addrs(default = nil) ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/tmail/interface.rb', line 128
def to_addrs( default = nil )
if h = @header['to']
h.addrs
else
default
end
end
|
#to_addrs=(arg) ⇒ Object
152
153
154
|
# File 'lib/tmail/interface.rb', line 152
def to_addrs=( arg )
set_addrfield 'to', arg
end
|
#transfer_encoding(default = nil) ⇒ Object
Also known as:
encoding, content_transfer_encoding
424
425
426
427
428
429
430
|
# File 'lib/tmail/interface.rb', line 424
def transfer_encoding( default = nil )
if h = @header['content-transfer-encoding']
h.encoding || default
else
default
end
end
|
#transfer_encoding=(str) ⇒ Object
Also known as:
encoding=, content_transfer_encoding=
432
433
434
|
# File 'lib/tmail/interface.rb', line 432
def transfer_encoding=( str )
set_string_attr 'Content-Transfer-Encoding', str
end
|
#type_param(name, default = nil) ⇒ Object
397
398
399
400
401
402
403
|
# File 'lib/tmail/interface.rb', line 397
def type_param( name, default = nil )
if h = @header['content-type']
h[name] || default
else
default
end
end
|
#unquoted_body(to_charset = 'utf-8') ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/tmail/quoting.rb', line 12
def unquoted_body(to_charset = 'utf-8')
from_charset = ("content-type", "charset")
case (content_transfer_encoding || "7bit").downcase
when "quoted-printable"
if !from_charset.blank? && from_charset.downcase == 'us-ascii'
from_charset = 'iso-8859-1'
end
Unquoter.unquote_quoted_printable_and_convert_to(quoted_body,
to_charset, from_charset, true)
when "base64"
Unquoter.unquote_base64_and_convert_to(quoted_body, to_charset,
from_charset)
when "7bit", "8bit"
Unquoter.convert_to(quoted_body, to_charset, from_charset)
when "binary"
quoted_body
else
quoted_body
end
end
|
#value?(val) ⇒ Boolean
Also known as:
has_value?
45
46
47
48
49
|
# File 'lib/tmail/obsolete.rb', line 45
def value?( val )
HeaderField === val or return false
[ @header[val.name.downcase] ].flatten.include? val
end
|
39
40
41
42
43
|
# File 'lib/tmail/obsolete.rb', line 39
def values
ret = []
each_field {|v| ret.push v }
ret
end
|
#values_at(*args) ⇒ Object
Also known as:
indexes, indices
322
323
324
|
# File 'lib/tmail/mail.rb', line 322
def values_at( *args )
args.map {|k| @header[k.downcase] }.flatten
end
|
#write_back(eol = "\n", charset = 'e') ⇒ Object
107
108
109
110
|
# File 'lib/tmail/mail.rb', line 107
def write_back( eol = "\n", charset = 'e' )
parse_body
@port.wopen {|stream| encoded eol, charset, stream }
end
|