mail 1.6.0 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of mail might be problematic. Click here for more details.
- data/CHANGELOG.rdoc +37 -0
- data/README.rdoc +9 -10
- data/Rakefile +1 -1
- data/lib/mail.rb +3 -3
- data/lib/mail/attachments_list.rb +76 -0
- data/lib/mail/body.rb +16 -16
- data/lib/mail/configuration.rb +35 -111
- data/lib/mail/encodings/encodings.rb +4 -0
- data/lib/mail/fields/common/address_container.rb +16 -0
- data/lib/mail/fields/common/common_address.rb +28 -3
- data/lib/mail/fields/return_path_field.rb +4 -0
- data/lib/mail/mail.rb +69 -57
- data/lib/mail/message.rb +169 -58
- data/lib/mail/network/delivery_methods/file_delivery.rb +32 -10
- data/lib/mail/network/delivery_methods/sendmail.rb +52 -14
- data/lib/mail/network/delivery_methods/smtp.rb +85 -68
- data/lib/mail/network/delivery_methods/test_mailer.rb +7 -2
- data/lib/mail/network/retriever_methods/imap.rb +7 -6
- data/lib/mail/network/retriever_methods/pop3.rb +48 -73
- data/lib/mail/parts_list.rb +34 -0
- data/lib/mail/vendor/treetop-1.4.3/examples/lambda_calculus/arithmetic.rb +1 -1
- data/lib/mail/vendor/treetop-1.4.3/examples/lambda_calculus/lambda_calculus.rb +1 -1
- data/lib/mail/vendor/treetop-1.4.3/lib/treetop/compiler/metagrammar.rb +1 -1
- data/lib/mail/vendor/treetop.rb +1 -1
- data/lib/mail/version.rb +3 -3
- metadata +5 -5
- data/lib/mail/attachment.rb +0 -107
- data/lib/mail/network/deliverable.rb +0 -15
- data/lib/mail/network/retrievable.rb +0 -19
@@ -10,7 +10,7 @@ module Mail
|
|
10
10
|
|
11
11
|
def parse(val = value)
|
12
12
|
unless val.blank?
|
13
|
-
@tree
|
13
|
+
@tree = AddressList.new(val)
|
14
14
|
else
|
15
15
|
nil
|
16
16
|
end
|
@@ -25,12 +25,26 @@ module Mail
|
|
25
25
|
|
26
26
|
# Returns the address string of all the addresses in the address list
|
27
27
|
def addresses
|
28
|
-
tree.addresses.map { |a| a.address }
|
28
|
+
list = tree.addresses.map { |a| a.address }
|
29
|
+
Mail::AddressContainer.new(self, list)
|
29
30
|
end
|
30
31
|
|
31
32
|
# Returns the formatted string of all the addresses in the address list
|
32
33
|
def formatted
|
33
|
-
tree.addresses.map { |a| a.format }
|
34
|
+
list = tree.addresses.map { |a| a.format }
|
35
|
+
Mail::AddressContainer.new(self, list)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the display name of all the addresses in the address list
|
39
|
+
def display_names
|
40
|
+
list = tree.addresses.map { |a| a.display_name }
|
41
|
+
Mail::AddressContainer.new(self, list)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns the actual address objects in the address list
|
45
|
+
def addrs
|
46
|
+
list = tree.addresses
|
47
|
+
Mail::AddressContainer.new(self, list)
|
34
48
|
end
|
35
49
|
|
36
50
|
# Returns a hash of group name => address strings for the address list
|
@@ -55,6 +69,17 @@ module Mail
|
|
55
69
|
def default
|
56
70
|
addresses
|
57
71
|
end
|
72
|
+
|
73
|
+
def <<(val)
|
74
|
+
case
|
75
|
+
when val.nil?
|
76
|
+
raise ArgumentError, "Need to pass an address to <<"
|
77
|
+
when val.blank?
|
78
|
+
parse(encoded)
|
79
|
+
else
|
80
|
+
parse((formatted + [val]).join(", "))
|
81
|
+
end
|
82
|
+
end
|
58
83
|
|
59
84
|
private
|
60
85
|
|
data/lib/mail/mail.rb
CHANGED
@@ -50,52 +50,70 @@ module Mail
|
|
50
50
|
Mail::Message.new(args, &block)
|
51
51
|
end
|
52
52
|
|
53
|
-
#
|
54
|
-
#
|
55
|
-
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
#
|
60
|
-
#
|
61
|
-
#
|
62
|
-
#
|
63
|
-
#
|
53
|
+
# Sets the default delivery method and retriever method for all new Mail objects.
|
54
|
+
# The delivery_method and retriever_method default to :smtp and :pop3, with defaults
|
55
|
+
# set.
|
56
|
+
#
|
57
|
+
# So sending a new email, if you have an SMTP server running on localhost is
|
58
|
+
# as easy as:
|
59
|
+
#
|
60
|
+
# Mail.deliver do
|
61
|
+
# to 'mikel@test.lindsaar.net'
|
62
|
+
# from 'bob@test.lindsaar.net'
|
63
|
+
# subject 'hi there!'
|
64
|
+
# body 'this is a body'
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# If you do not specify anything, you will get the following equivalent code set in
|
68
|
+
# every new mail object:
|
69
|
+
#
|
70
|
+
# Mail.defaults do
|
71
|
+
# delivery_method :smtp, { :address => "localhost",
|
72
|
+
# :port => 25,
|
73
|
+
# :domain => 'localhost.localdomain',
|
74
|
+
# :user_name => nil,
|
75
|
+
# :password => nil,
|
76
|
+
# :authentication => nil,
|
77
|
+
# :enable_starttls_auto => true }
|
78
|
+
#
|
79
|
+
# retriever_method :pop3, { :address => "localhost",
|
80
|
+
# :port => 995,
|
81
|
+
# :user_name => nil,
|
82
|
+
# :password => nil,
|
83
|
+
# :enable_ssl => true }
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# Mail.delivery_method.new #=> Mail::SMTP instance
|
87
|
+
# Mail.retriever_method.new #=> Mail::POP3 instance
|
64
88
|
#
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
69
|
-
# pass 'mypass'
|
70
|
-
# enable_tls
|
71
|
-
# end
|
72
|
-
# end
|
89
|
+
# Each mail object inherits the default set in Mail.delivery_method, however, on
|
90
|
+
# a per email basis, you can override the method:
|
91
|
+
#
|
92
|
+
# mail.delivery_method :sendmail
|
73
93
|
#
|
74
|
-
#
|
75
|
-
# are SMTP and POP3. You set the types by passing a symbol:
|
94
|
+
# Or you can override the method and pass in settings:
|
76
95
|
#
|
77
|
-
#
|
78
|
-
# retriever_method :pop3
|
79
|
-
# delivery_method :smtp
|
80
|
-
# end
|
96
|
+
# mail.delivery_method :sendmail, { :address => 'some.host' }
|
81
97
|
#
|
82
|
-
#
|
98
|
+
# You can also just modify the settings:
|
83
99
|
#
|
84
|
-
#
|
85
|
-
# or a retriever class which must respond to :get_messages
|
100
|
+
# mail.delivery_settings = { :address => 'some.host' }
|
86
101
|
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
# Once you have set defaults, you can call Mail.deliver to send an email
|
92
|
-
# through the Mail.deliver method.
|
102
|
+
# The passed in hash is just merged against the defaults with +merge!+ and the result
|
103
|
+
# assigned the mail object. So the above example will change only the :address value
|
104
|
+
# of the global smtp_settings to be 'some.host', keeping all other values
|
93
105
|
def Mail.defaults(&block)
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
106
|
+
Mail::Configuration.instance.instance_eval(&block)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns the delivery method selected, defaults to an instance of Mail::SMTP
|
110
|
+
def Mail.delivery_method
|
111
|
+
Mail::Configuration.instance.delivery_method
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns the retriever method selected, defaults to an instance of Mail::POP3
|
115
|
+
def Mail.retriever_method
|
116
|
+
Mail::Configuration.instance.retriever_method
|
99
117
|
end
|
100
118
|
|
101
119
|
# Send an email using the default configuration. You do need to set a default
|
@@ -119,41 +137,35 @@ module Mail
|
|
119
137
|
# And your email object will be created and sent.
|
120
138
|
def Mail.deliver(*args, &block)
|
121
139
|
mail = Mail.new(args, &block)
|
122
|
-
|
140
|
+
delivery_method.deliver!(mail)
|
123
141
|
mail
|
124
142
|
end
|
125
143
|
|
144
|
+
# Find emails in a POP3 server.
|
145
|
+
# See Mail::POP3 for a complete documentation.
|
146
|
+
def Mail.find(*args, &block)
|
147
|
+
retriever_method.find(*args, &block)
|
148
|
+
end
|
149
|
+
|
126
150
|
# Receive the first email(s) from a Pop3 server.
|
127
151
|
# See Mail::POP3 for a complete documentation.
|
128
152
|
def Mail.first(*args, &block)
|
129
|
-
|
153
|
+
retriever_method.first(*args, &block)
|
130
154
|
end
|
131
155
|
|
132
156
|
# Receive the first email(s) from a Pop3 server.
|
133
157
|
# See Mail::POP3 for a complete documentation.
|
134
158
|
def Mail.last(*args, &block)
|
135
|
-
|
159
|
+
retriever_method.last(*args, &block)
|
136
160
|
end
|
137
161
|
|
138
|
-
# Receive all emails from a
|
162
|
+
# Receive all emails from a POP3 server.
|
139
163
|
# See Mail::POP3 for a complete documentation.
|
140
164
|
def Mail.all(*args, &block)
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
# Receive all emails from a Pop3 server.
|
145
|
-
# DEPRECATED: please use Mail.all instead.
|
146
|
-
def Mail.get_all_mail(*args, &block)
|
147
|
-
warn "Mail.get_all_mail is deprecated. Please use Mail.all instead."
|
148
|
-
all(*args, &block)
|
149
|
-
end
|
150
|
-
|
151
|
-
# Find emails in a Pop3 server.
|
152
|
-
# See Mail::POP3 for a complete documentation.
|
153
|
-
def Mail.find(*args, &block)
|
154
|
-
Retrievable.find(*args, &block)
|
165
|
+
retriever_method.all(*args, &block)
|
155
166
|
end
|
156
167
|
|
168
|
+
# Reads in an email message from a path and instantiates it as a new Mail::Message
|
157
169
|
def Mail.read(filename)
|
158
170
|
Mail.new(File.read(filename))
|
159
171
|
end
|
data/lib/mail/message.rb
CHANGED
@@ -100,6 +100,8 @@ module Mail
|
|
100
100
|
@text_part = nil
|
101
101
|
@html_part = nil
|
102
102
|
|
103
|
+
@delivery_method = Mail.delivery_method.clone
|
104
|
+
|
103
105
|
if args.flatten.first.respond_to?(:each_pair)
|
104
106
|
init_with_hash(args.flatten.first)
|
105
107
|
else
|
@@ -119,8 +121,18 @@ module Mail
|
|
119
121
|
#
|
120
122
|
# mail = Mail.read('file.eml')
|
121
123
|
# mail.deliver!
|
122
|
-
def deliver
|
123
|
-
|
124
|
+
def deliver
|
125
|
+
@delivery_method.deliver!(self)
|
126
|
+
end
|
127
|
+
|
128
|
+
alias :deliver! :deliver
|
129
|
+
|
130
|
+
def delivery_method(method = nil, settings = {})
|
131
|
+
unless method
|
132
|
+
@delivery_method
|
133
|
+
else
|
134
|
+
@delivery_method = Mail::Configuration.instance.lookup_delivery_method(method).new(settings)
|
135
|
+
end
|
124
136
|
end
|
125
137
|
|
126
138
|
# Provides the operator needed for sort et al.
|
@@ -271,6 +283,15 @@ module Mail
|
|
271
283
|
#
|
272
284
|
# mail.bcc 'Mikel <mikel@test.lindsaar.net>'
|
273
285
|
# mail.bcc #=> ['mikel@test.lindsaar.net']
|
286
|
+
#
|
287
|
+
# Additionally, you can append new addresses to the returned Array like
|
288
|
+
# object.
|
289
|
+
#
|
290
|
+
# Example:
|
291
|
+
#
|
292
|
+
# mail.bcc 'Mikel <mikel@test.lindsaar.net>'
|
293
|
+
# mail.bcc << 'ada@test.lindsaar.net'
|
294
|
+
# mail.bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
274
295
|
def bcc( val = nil )
|
275
296
|
default :bcc, val
|
276
297
|
end
|
@@ -303,6 +324,15 @@ module Mail
|
|
303
324
|
#
|
304
325
|
# mail.cc 'Mikel <mikel@test.lindsaar.net>'
|
305
326
|
# mail.cc #=> ['mikel@test.lindsaar.net']
|
327
|
+
#
|
328
|
+
# Additionally, you can append new addresses to the returned Array like
|
329
|
+
# object.
|
330
|
+
#
|
331
|
+
# Example:
|
332
|
+
#
|
333
|
+
# mail.cc 'Mikel <mikel@test.lindsaar.net>'
|
334
|
+
# mail.cc << 'ada@test.lindsaar.net'
|
335
|
+
# mail.cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
306
336
|
def cc( val = nil )
|
307
337
|
default :cc, val
|
308
338
|
end
|
@@ -399,6 +429,15 @@ module Mail
|
|
399
429
|
#
|
400
430
|
# mail.from 'Mikel <mikel@test.lindsaar.net>'
|
401
431
|
# mail.from #=> ['mikel@test.lindsaar.net']
|
432
|
+
#
|
433
|
+
# Additionally, you can append new addresses to the returned Array like
|
434
|
+
# object.
|
435
|
+
#
|
436
|
+
# Example:
|
437
|
+
#
|
438
|
+
# mail.from 'Mikel <mikel@test.lindsaar.net>'
|
439
|
+
# mail.from << 'ada@test.lindsaar.net'
|
440
|
+
# mail.from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
402
441
|
def from( val = nil )
|
403
442
|
default :from, val
|
404
443
|
end
|
@@ -520,6 +559,15 @@ module Mail
|
|
520
559
|
#
|
521
560
|
# mail.reply_to 'Mikel <mikel@test.lindsaar.net>'
|
522
561
|
# mail.reply_to #=> ['mikel@test.lindsaar.net']
|
562
|
+
#
|
563
|
+
# Additionally, you can append new addresses to the returned Array like
|
564
|
+
# object.
|
565
|
+
#
|
566
|
+
# Example:
|
567
|
+
#
|
568
|
+
# mail.reply_to 'Mikel <mikel@test.lindsaar.net>'
|
569
|
+
# mail.reply_to << 'ada@test.lindsaar.net'
|
570
|
+
# mail.reply_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
523
571
|
def reply_to( val = nil )
|
524
572
|
default :reply_to, val
|
525
573
|
end
|
@@ -552,6 +600,15 @@ module Mail
|
|
552
600
|
#
|
553
601
|
# mail.resent_bcc 'Mikel <mikel@test.lindsaar.net>'
|
554
602
|
# mail.resent_bcc #=> ['mikel@test.lindsaar.net']
|
603
|
+
#
|
604
|
+
# Additionally, you can append new addresses to the returned Array like
|
605
|
+
# object.
|
606
|
+
#
|
607
|
+
# Example:
|
608
|
+
#
|
609
|
+
# mail.resent_bcc 'Mikel <mikel@test.lindsaar.net>'
|
610
|
+
# mail.resent_bcc << 'ada@test.lindsaar.net'
|
611
|
+
# mail.resent_bcc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
555
612
|
def resent_bcc( val = nil )
|
556
613
|
default :resent_bcc, val
|
557
614
|
end
|
@@ -584,6 +641,15 @@ module Mail
|
|
584
641
|
#
|
585
642
|
# mail.resent_cc 'Mikel <mikel@test.lindsaar.net>'
|
586
643
|
# mail.resent_cc #=> ['mikel@test.lindsaar.net']
|
644
|
+
#
|
645
|
+
# Additionally, you can append new addresses to the returned Array like
|
646
|
+
# object.
|
647
|
+
#
|
648
|
+
# Example:
|
649
|
+
#
|
650
|
+
# mail.resent_cc 'Mikel <mikel@test.lindsaar.net>'
|
651
|
+
# mail.resent_cc << 'ada@test.lindsaar.net'
|
652
|
+
# mail.resent_cc #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
587
653
|
def resent_cc( val = nil )
|
588
654
|
default :resent_cc, val
|
589
655
|
end
|
@@ -624,6 +690,15 @@ module Mail
|
|
624
690
|
#
|
625
691
|
# mail.resent_from ['Mikel <mikel@test.lindsaar.net>']
|
626
692
|
# mail.resent_from #=> 'mikel@test.lindsaar.net'
|
693
|
+
#
|
694
|
+
# Additionally, you can append new addresses to the returned Array like
|
695
|
+
# object.
|
696
|
+
#
|
697
|
+
# Example:
|
698
|
+
#
|
699
|
+
# mail.resent_from 'Mikel <mikel@test.lindsaar.net>'
|
700
|
+
# mail.resent_from << 'ada@test.lindsaar.net'
|
701
|
+
# mail.resent_from #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
627
702
|
def resent_from( val = nil )
|
628
703
|
default :resent_from, val
|
629
704
|
end
|
@@ -649,7 +724,8 @@ module Mail
|
|
649
724
|
end
|
650
725
|
|
651
726
|
# Returns the Resent-Sender value of the mail object, as a single string of an address
|
652
|
-
# spec. A sender per RFC 2822 must be a single address
|
727
|
+
# spec. A sender per RFC 2822 must be a single address, so you can not append to
|
728
|
+
# this address.
|
653
729
|
#
|
654
730
|
# Example:
|
655
731
|
#
|
@@ -692,6 +768,15 @@ module Mail
|
|
692
768
|
#
|
693
769
|
# mail.resent_to 'Mikel <mikel@test.lindsaar.net>'
|
694
770
|
# mail.resent_to #=> ['mikel@test.lindsaar.net']
|
771
|
+
#
|
772
|
+
# Additionally, you can append new addresses to the returned Array like
|
773
|
+
# object.
|
774
|
+
#
|
775
|
+
# Example:
|
776
|
+
#
|
777
|
+
# mail.resent_to 'Mikel <mikel@test.lindsaar.net>'
|
778
|
+
# mail.resent_to << 'ada@test.lindsaar.net'
|
779
|
+
# mail.resent_to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
695
780
|
def resent_to( val = nil )
|
696
781
|
default :resent_to, val
|
697
782
|
end
|
@@ -719,7 +804,7 @@ module Mail
|
|
719
804
|
end
|
720
805
|
|
721
806
|
# Returns the Sender value of the mail object, as a single string of an address
|
722
|
-
# spec. A sender per RFC 2822 must be a single address
|
807
|
+
# spec. A sender per RFC 2822 must be a single address.
|
723
808
|
#
|
724
809
|
# Example:
|
725
810
|
#
|
@@ -791,6 +876,15 @@ module Mail
|
|
791
876
|
#
|
792
877
|
# mail.to 'Mikel <mikel@test.lindsaar.net>'
|
793
878
|
# mail.to #=> ['mikel@test.lindsaar.net']
|
879
|
+
#
|
880
|
+
# Additionally, you can append new addresses to the returned Array like
|
881
|
+
# object.
|
882
|
+
#
|
883
|
+
# Example:
|
884
|
+
#
|
885
|
+
# mail.to 'Mikel <mikel@test.lindsaar.net>'
|
886
|
+
# mail.to << 'ada@test.lindsaar.net'
|
887
|
+
# mail.to #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
794
888
|
def to( val = nil )
|
795
889
|
default :to, val
|
796
890
|
end
|
@@ -1211,20 +1305,47 @@ module Mail
|
|
1211
1305
|
content_type_parameters ? content_type_parameters['boundary'] : nil
|
1212
1306
|
end
|
1213
1307
|
|
1214
|
-
# Returns
|
1308
|
+
# Returns a parts list object of all the parts in the message
|
1215
1309
|
def parts
|
1216
1310
|
body.parts
|
1217
1311
|
end
|
1218
1312
|
|
1219
|
-
# Returns an
|
1313
|
+
# Returns an AttachmentsList object, which holds all of the attachments in
|
1314
|
+
# the receiver object (either the entier email or a part within) and all
|
1315
|
+
# of it's descendants.
|
1316
|
+
#
|
1317
|
+
# It also allows you to add attachments to the mail object directly, like so:
|
1318
|
+
#
|
1319
|
+
# mail.attachments['filename.jpg'] = File.read('/path/to/filename.jpg')
|
1320
|
+
#
|
1321
|
+
# If you do this, then Mail will take the file name and work out the mime type
|
1322
|
+
# set the Content-Type, Content-Disposition, Content-Transfer-Encoding and
|
1323
|
+
# base64 encode the contents of the attachment all for you.
|
1324
|
+
#
|
1325
|
+
# You can also specify overrides if you want by passing a hash instead of a string:
|
1326
|
+
#
|
1327
|
+
# mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
|
1328
|
+
# :content => File.read('/path/to/filename.jpg')}
|
1329
|
+
#
|
1330
|
+
# If you want to use a different encoding than Base64, you can pass an encoding in,
|
1331
|
+
# but then it is up to you to pass in the content pre-encoded, and don't expect
|
1332
|
+
# Mail to know how to decode this data:
|
1333
|
+
#
|
1334
|
+
# file_content = SpecialEncode(File.read('/path/to/filename.jpg'))
|
1335
|
+
# mail.attachments['filename.jpg'] = {:mime_type => 'application/x-gzip',
|
1336
|
+
# :encoding => 'SpecialEncoding',
|
1337
|
+
# :content => file_content }
|
1338
|
+
#
|
1339
|
+
# You can also search for specific attachments:
|
1340
|
+
#
|
1341
|
+
# # By Filename
|
1342
|
+
# mail.attachments['filename.jpg'] #=> Mail::Part object or nil
|
1343
|
+
#
|
1344
|
+
# # or by index
|
1345
|
+
# mail.attachments[0] #=> Mail::Part (first attachment)
|
1346
|
+
#
|
1220
1347
|
def attachments
|
1221
|
-
|
1222
|
-
if p.parts.empty?
|
1223
|
-
p.attachment if p.attachment?
|
1224
|
-
else
|
1225
|
-
p.attachments
|
1226
|
-
end
|
1227
|
-
end.compact.flatten
|
1348
|
+
parts.attachments
|
1228
1349
|
end
|
1229
1350
|
|
1230
1351
|
def has_attachments?
|
@@ -1310,20 +1431,16 @@ module Mail
|
|
1310
1431
|
# Adds a file to the message. You have two options with this method, you can
|
1311
1432
|
# just pass in the absolute path to the file you want and Mail will read the file,
|
1312
1433
|
# get the filename from the path you pass in and guess the mime type, or you
|
1313
|
-
# can pass in the filename as a string, and pass in the file
|
1434
|
+
# can pass in the filename as a string, and pass in the file content as a blob.
|
1314
1435
|
#
|
1315
1436
|
# Example:
|
1316
1437
|
#
|
1317
1438
|
# m = Mail.new
|
1318
1439
|
# m.add_file('/path/to/filename.png')
|
1319
1440
|
#
|
1320
|
-
# or
|
1321
|
-
#
|
1322
1441
|
# m = Mail.new
|
1323
|
-
# m.add_file(:filename => 'filename.png', :
|
1324
|
-
#
|
1325
|
-
# The above two alternatives will produce the same email message.
|
1326
|
-
#
|
1442
|
+
# m.add_file(:filename => 'filename.png', :content => File.read('/path/to/file.jpg'))
|
1443
|
+
#
|
1327
1444
|
# Note also that if you add a file to an existing message, Mail will convert that message
|
1328
1445
|
# to a MIME multipart email, moving whatever plain text body you had into it's own text
|
1329
1446
|
# plain part.
|
@@ -1338,14 +1455,19 @@ module Mail
|
|
1338
1455
|
# m.multipart? #=> true
|
1339
1456
|
# m.parts.first.content_type.content_type #=> 'text/plain'
|
1340
1457
|
# m.parts.last.content_type.content_type #=> 'image/png'
|
1341
|
-
|
1458
|
+
#
|
1459
|
+
# See also #attachments
|
1460
|
+
def add_file(values)
|
1342
1461
|
convert_to_multipart unless self.multipart? || self.body.decoded.blank?
|
1343
1462
|
add_multipart_mixed_header
|
1344
|
-
if
|
1345
|
-
|
1463
|
+
if values.is_a?(String)
|
1464
|
+
basename = File.basename(values)
|
1465
|
+
filedata = File.read(values)
|
1346
1466
|
else
|
1347
|
-
|
1467
|
+
basename = values[:filename]
|
1468
|
+
filedata = values[:content] || File.read(values[:filename])
|
1348
1469
|
end
|
1470
|
+
self.attachments[basename] = filedata
|
1349
1471
|
end
|
1350
1472
|
|
1351
1473
|
def convert_to_multipart
|
@@ -1384,7 +1506,27 @@ module Mail
|
|
1384
1506
|
end
|
1385
1507
|
|
1386
1508
|
def decoded
|
1387
|
-
|
1509
|
+
if self.attachment?
|
1510
|
+
decode_body
|
1511
|
+
else
|
1512
|
+
raise NoMethodError, 'Can not decode an entire message, try calling #decoded on the various fields and body or parts if it is a multipart message.'
|
1513
|
+
end
|
1514
|
+
end
|
1515
|
+
|
1516
|
+
def read
|
1517
|
+
if self.attachment?
|
1518
|
+
decode_body
|
1519
|
+
else
|
1520
|
+
raise NoMethodError, 'Can not call read on a part unless it is an attachment.'
|
1521
|
+
end
|
1522
|
+
end
|
1523
|
+
|
1524
|
+
def decode_body
|
1525
|
+
if Mail::Encodings.defined?(content_transfer_encoding)
|
1526
|
+
Mail::Encodings.get_encoding(content_transfer_encoding).decode(body.encoded)
|
1527
|
+
else
|
1528
|
+
raise UnknownEncodingType, "Don't know how to decode #{content_transfer_encoding}, please call #encoded and decode it yourself."
|
1529
|
+
end
|
1388
1530
|
end
|
1389
1531
|
|
1390
1532
|
# Returns true if this part is an attachment
|
@@ -1399,11 +1541,7 @@ module Mail
|
|
1399
1541
|
|
1400
1542
|
# Returns the filename of the attachment
|
1401
1543
|
def filename
|
1402
|
-
|
1403
|
-
attachment.filename
|
1404
|
-
else
|
1405
|
-
nil
|
1406
|
-
end
|
1544
|
+
find_attachment
|
1407
1545
|
end
|
1408
1546
|
|
1409
1547
|
private
|
@@ -1446,6 +1584,7 @@ module Mail
|
|
1446
1584
|
end
|
1447
1585
|
|
1448
1586
|
def add_required_fields
|
1587
|
+
add_multipart_mixed_header unless parts.empty?
|
1449
1588
|
@body = Mail::Body.new('') if body.nil?
|
1450
1589
|
add_message_id unless (has_message_id? || self.class == Mail::Part)
|
1451
1590
|
add_date unless has_date?
|
@@ -1481,16 +1620,6 @@ module Mail
|
|
1481
1620
|
@header = Mail::Header.new
|
1482
1621
|
@body = Mail::Body.new
|
1483
1622
|
|
1484
|
-
# Strip out the attachment headers and make an attachment
|
1485
|
-
if passed_in_options.has_key?(:filename)
|
1486
|
-
add_attachment(passed_in_options)
|
1487
|
-
passed_in_options.delete(:content_disposition)
|
1488
|
-
passed_in_options.delete(:content_type)
|
1489
|
-
passed_in_options.delete(:mime_type)
|
1490
|
-
passed_in_options.delete(:filename)
|
1491
|
-
passed_in_options.delete(:data)
|
1492
|
-
end
|
1493
|
-
|
1494
1623
|
passed_in_options.each_pair do |k,v|
|
1495
1624
|
k = underscoreize(k).to_sym if k.class == String
|
1496
1625
|
if k == :headers
|
@@ -1501,29 +1630,11 @@ module Mail
|
|
1501
1630
|
end
|
1502
1631
|
end
|
1503
1632
|
|
1504
|
-
def add_attachment(options_hash)
|
1505
|
-
@attachment = Mail::Attachment.new(options_hash)
|
1506
|
-
mime_type = options_hash[:content_type] || attachment.mime_type
|
1507
|
-
self.content_type = "#{mime_type}; filename=\"#{attachment.filename}\""
|
1508
|
-
self.content_transfer_encoding = "Base64"
|
1509
|
-
|
1510
|
-
disposition = options_hash[:content_disposition] || "attachment"
|
1511
|
-
self.content_disposition = "#{disposition}; filename=\"#{attachment.filename}\""
|
1512
|
-
add_boundary
|
1513
|
-
self.body = attachment.encoded
|
1514
|
-
end
|
1515
|
-
|
1516
1633
|
def init_with_string(string)
|
1517
1634
|
self.raw_source = string
|
1518
1635
|
set_envelope_header
|
1519
1636
|
parse_message
|
1520
1637
|
separate_parts if multipart?
|
1521
|
-
if filename = attachment?
|
1522
|
-
encoding = header[:content_transfer_encoding].encoding if content_transfer_encoding
|
1523
|
-
@attachment = Mail::Attachment.new(:filename => filename,
|
1524
|
-
:data => body.encoded,
|
1525
|
-
:encoding => encoding)
|
1526
|
-
end
|
1527
1638
|
end
|
1528
1639
|
|
1529
1640
|
# Returns the filename of the attachment (if it exists) or returns nil
|