mail 1.0.0
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/.gitignore +4 -0
- data/Manifest.txt +106 -0
- data/README.rdoc +441 -0
- data/Rakefile +38 -0
- data/lib/mail.rb +86 -0
- data/lib/mail/attachment.rb +90 -0
- data/lib/mail/body.rb +149 -0
- data/lib/mail/configuration.rb +90 -0
- data/lib/mail/core_extensions.rb +6 -0
- data/lib/mail/core_extensions/blank.rb +41 -0
- data/lib/mail/core_extensions/nil.rb +15 -0
- data/lib/mail/core_extensions/string.rb +31 -0
- data/lib/mail/elements/address.rb +293 -0
- data/lib/mail/elements/address_list.rb +62 -0
- data/lib/mail/elements/content_disposition_element.rb +34 -0
- data/lib/mail/elements/content_transfer_encoding_element.rb +21 -0
- data/lib/mail/elements/content_type_element.rb +39 -0
- data/lib/mail/elements/date_time_element.rb +26 -0
- data/lib/mail/elements/envelope_from_element.rb +34 -0
- data/lib/mail/elements/message_ids_element.rb +29 -0
- data/lib/mail/elements/mime_version_element.rb +26 -0
- data/lib/mail/elements/phrase_list.rb +21 -0
- data/lib/mail/elements/received_element.rb +30 -0
- data/lib/mail/encodings/base64.rb +17 -0
- data/lib/mail/encodings/encodings.rb +24 -0
- data/lib/mail/encodings/quoted_printable.rb +26 -0
- data/lib/mail/envelope.rb +35 -0
- data/lib/mail/field.rb +202 -0
- data/lib/mail/field_list.rb +33 -0
- data/lib/mail/fields/bcc_field.rb +40 -0
- data/lib/mail/fields/cc_field.rb +40 -0
- data/lib/mail/fields/comments_field.rb +41 -0
- data/lib/mail/fields/common/common_address.rb +62 -0
- data/lib/mail/fields/common/common_date.rb +35 -0
- data/lib/mail/fields/common/common_field.rb +128 -0
- data/lib/mail/fields/common/common_message_id.rb +35 -0
- data/lib/mail/fields/content_description_field.rb +15 -0
- data/lib/mail/fields/content_disposition_field.rb +34 -0
- data/lib/mail/fields/content_id_field.rb +50 -0
- data/lib/mail/fields/content_transfer_encoding_field.rb +28 -0
- data/lib/mail/fields/content_type_field.rb +50 -0
- data/lib/mail/fields/date_field.rb +44 -0
- data/lib/mail/fields/from_field.rb +40 -0
- data/lib/mail/fields/in_reply_to_field.rb +42 -0
- data/lib/mail/fields/keywords_field.rb +22 -0
- data/lib/mail/fields/message_id_field.rb +70 -0
- data/lib/mail/fields/mime_version_field.rb +42 -0
- data/lib/mail/fields/optional_field.rb +11 -0
- data/lib/mail/fields/received_field.rb +49 -0
- data/lib/mail/fields/references_field.rb +42 -0
- data/lib/mail/fields/reply_to_field.rb +40 -0
- data/lib/mail/fields/resent_bcc_field.rb +40 -0
- data/lib/mail/fields/resent_cc_field.rb +40 -0
- data/lib/mail/fields/resent_date_field.rb +16 -0
- data/lib/mail/fields/resent_from_field.rb +40 -0
- data/lib/mail/fields/resent_message_id_field.rb +20 -0
- data/lib/mail/fields/resent_sender_field.rb +48 -0
- data/lib/mail/fields/resent_to_field.rb +40 -0
- data/lib/mail/fields/return_path_field.rb +34 -0
- data/lib/mail/fields/sender_field.rb +48 -0
- data/lib/mail/fields/structured_field.rb +32 -0
- data/lib/mail/fields/subject_field.rb +14 -0
- data/lib/mail/fields/to_field.rb +40 -0
- data/lib/mail/fields/unstructured_field.rb +27 -0
- data/lib/mail/header.rb +213 -0
- data/lib/mail/mail.rb +120 -0
- data/lib/mail/message.rb +648 -0
- data/lib/mail/network/deliverable.rb +42 -0
- data/lib/mail/network/retrievable.rb +63 -0
- data/lib/mail/parsers/address_lists.rb +61 -0
- data/lib/mail/parsers/address_lists.treetop +19 -0
- data/lib/mail/parsers/content_disposition.rb +358 -0
- data/lib/mail/parsers/content_disposition.treetop +45 -0
- data/lib/mail/parsers/content_transfer_encoding.rb +179 -0
- data/lib/mail/parsers/content_transfer_encoding.treetop +25 -0
- data/lib/mail/parsers/content_type.rb +507 -0
- data/lib/mail/parsers/content_type.treetop +58 -0
- data/lib/mail/parsers/date_time.rb +111 -0
- data/lib/mail/parsers/date_time.treetop +11 -0
- data/lib/mail/parsers/envelope_from.rb +188 -0
- data/lib/mail/parsers/envelope_from.treetop +32 -0
- data/lib/mail/parsers/message_ids.rb +42 -0
- data/lib/mail/parsers/message_ids.treetop +15 -0
- data/lib/mail/parsers/mime_version.rb +141 -0
- data/lib/mail/parsers/mime_version.treetop +19 -0
- data/lib/mail/parsers/phrase_lists.rb +42 -0
- data/lib/mail/parsers/phrase_lists.treetop +15 -0
- data/lib/mail/parsers/received.rb +68 -0
- data/lib/mail/parsers/received.treetop +11 -0
- data/lib/mail/parsers/rfc2045.rb +406 -0
- data/lib/mail/parsers/rfc2045.treetop +35 -0
- data/lib/mail/parsers/rfc2822.rb +5005 -0
- data/lib/mail/parsers/rfc2822.treetop +402 -0
- data/lib/mail/parsers/rfc2822_obsolete.rb +3607 -0
- data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
- data/lib/mail/part.rb +120 -0
- data/lib/mail/patterns.rb +42 -0
- data/lib/mail/utilities.rb +142 -0
- data/lib/mail/version.rb +10 -0
- data/lib/mail/version_specific/multibyte.rb +62 -0
- data/lib/mail/version_specific/multibyte/chars.rb +701 -0
- data/lib/mail/version_specific/multibyte/exceptions.rb +8 -0
- data/lib/mail/version_specific/multibyte/unicode_database.rb +71 -0
- data/lib/mail/version_specific/ruby_1_8.rb +61 -0
- data/lib/mail/version_specific/ruby_1_8_string.rb +88 -0
- data/lib/mail/version_specific/ruby_1_9.rb +49 -0
- metadata +192 -0
data/.gitignore
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
.gitignore
|
2
|
+
Manifest.txt
|
3
|
+
README.rdoc
|
4
|
+
Rakefile
|
5
|
+
lib/mail.rb
|
6
|
+
lib/mail/attachment.rb
|
7
|
+
lib/mail/body.rb
|
8
|
+
lib/mail/configuration.rb
|
9
|
+
lib/mail/core_extensions/blank.rb
|
10
|
+
lib/mail/core_extensions/nil.rb
|
11
|
+
lib/mail/core_extensions/string.rb
|
12
|
+
lib/mail/core_extensions.rb
|
13
|
+
lib/mail/elements/address.rb
|
14
|
+
lib/mail/elements/address_list.rb
|
15
|
+
lib/mail/elements/content_disposition_element.rb
|
16
|
+
lib/mail/elements/content_transfer_encoding_element.rb
|
17
|
+
lib/mail/elements/content_type_element.rb
|
18
|
+
lib/mail/elements/date_time_element.rb
|
19
|
+
lib/mail/elements/envelope_from_element.rb
|
20
|
+
lib/mail/elements/message_ids_element.rb
|
21
|
+
lib/mail/elements/mime_version_element.rb
|
22
|
+
lib/mail/elements/phrase_list.rb
|
23
|
+
lib/mail/elements/received_element.rb
|
24
|
+
lib/mail/encodings/base64.rb
|
25
|
+
lib/mail/encodings/encodings.rb
|
26
|
+
lib/mail/encodings/quoted_printable.rb
|
27
|
+
lib/mail/envelope.rb
|
28
|
+
lib/mail/field.rb
|
29
|
+
lib/mail/field_list.rb
|
30
|
+
lib/mail/fields/bcc_field.rb
|
31
|
+
lib/mail/fields/cc_field.rb
|
32
|
+
lib/mail/fields/comments_field.rb
|
33
|
+
lib/mail/fields/common/common_address.rb
|
34
|
+
lib/mail/fields/common/common_date.rb
|
35
|
+
lib/mail/fields/common/common_field.rb
|
36
|
+
lib/mail/fields/common/common_message_id.rb
|
37
|
+
lib/mail/fields/content_description_field.rb
|
38
|
+
lib/mail/fields/content_disposition_field.rb
|
39
|
+
lib/mail/fields/content_id_field.rb
|
40
|
+
lib/mail/fields/content_transfer_encoding_field.rb
|
41
|
+
lib/mail/fields/content_type_field.rb
|
42
|
+
lib/mail/fields/date_field.rb
|
43
|
+
lib/mail/fields/from_field.rb
|
44
|
+
lib/mail/fields/in_reply_to_field.rb
|
45
|
+
lib/mail/fields/keywords_field.rb
|
46
|
+
lib/mail/fields/message_id_field.rb
|
47
|
+
lib/mail/fields/mime_version_field.rb
|
48
|
+
lib/mail/fields/optional_field.rb
|
49
|
+
lib/mail/fields/received_field.rb
|
50
|
+
lib/mail/fields/references_field.rb
|
51
|
+
lib/mail/fields/reply_to_field.rb
|
52
|
+
lib/mail/fields/resent_bcc_field.rb
|
53
|
+
lib/mail/fields/resent_cc_field.rb
|
54
|
+
lib/mail/fields/resent_date_field.rb
|
55
|
+
lib/mail/fields/resent_from_field.rb
|
56
|
+
lib/mail/fields/resent_message_id_field.rb
|
57
|
+
lib/mail/fields/resent_sender_field.rb
|
58
|
+
lib/mail/fields/resent_to_field.rb
|
59
|
+
lib/mail/fields/return_path_field.rb
|
60
|
+
lib/mail/fields/sender_field.rb
|
61
|
+
lib/mail/fields/structured_field.rb
|
62
|
+
lib/mail/fields/subject_field.rb
|
63
|
+
lib/mail/fields/to_field.rb
|
64
|
+
lib/mail/fields/unstructured_field.rb
|
65
|
+
lib/mail/header.rb
|
66
|
+
lib/mail/mail.rb
|
67
|
+
lib/mail/message.rb
|
68
|
+
lib/mail/network/deliverable.rb
|
69
|
+
lib/mail/network/retrievable.rb
|
70
|
+
lib/mail/parsers/address_lists.rb
|
71
|
+
lib/mail/parsers/address_lists.treetop
|
72
|
+
lib/mail/parsers/content_disposition.rb
|
73
|
+
lib/mail/parsers/content_disposition.treetop
|
74
|
+
lib/mail/parsers/content_transfer_encoding.rb
|
75
|
+
lib/mail/parsers/content_transfer_encoding.treetop
|
76
|
+
lib/mail/parsers/content_type.rb
|
77
|
+
lib/mail/parsers/content_type.treetop
|
78
|
+
lib/mail/parsers/date_time.rb
|
79
|
+
lib/mail/parsers/date_time.treetop
|
80
|
+
lib/mail/parsers/envelope_from.rb
|
81
|
+
lib/mail/parsers/envelope_from.treetop
|
82
|
+
lib/mail/parsers/message_ids.rb
|
83
|
+
lib/mail/parsers/message_ids.treetop
|
84
|
+
lib/mail/parsers/mime_version.rb
|
85
|
+
lib/mail/parsers/mime_version.treetop
|
86
|
+
lib/mail/parsers/phrase_lists.rb
|
87
|
+
lib/mail/parsers/phrase_lists.treetop
|
88
|
+
lib/mail/parsers/received.rb
|
89
|
+
lib/mail/parsers/received.treetop
|
90
|
+
lib/mail/parsers/rfc2045.rb
|
91
|
+
lib/mail/parsers/rfc2045.treetop
|
92
|
+
lib/mail/parsers/rfc2822.rb
|
93
|
+
lib/mail/parsers/rfc2822.treetop
|
94
|
+
lib/mail/parsers/rfc2822_obsolete.rb
|
95
|
+
lib/mail/parsers/rfc2822_obsolete.treetop
|
96
|
+
lib/mail/part.rb
|
97
|
+
lib/mail/patterns.rb
|
98
|
+
lib/mail/utilities.rb
|
99
|
+
lib/mail/version.rb
|
100
|
+
lib/mail/version_specific/ruby_1_8.rb
|
101
|
+
lib/mail/version_specific/ruby_1_8_string.rb
|
102
|
+
lib/mail/version_specific/ruby_1_9.rb
|
103
|
+
lib/mail/version_specific/multibyte.rb
|
104
|
+
lib/mail/version_specific/multibyte/chars.rb
|
105
|
+
lib/mail/version_specific/multibyte/exceptions.rb
|
106
|
+
lib/mail/version_specific/multibyte/unicode_database.rb
|
data/README.rdoc
ADDED
@@ -0,0 +1,441 @@
|
|
1
|
+
= Mail
|
2
|
+
|
3
|
+
== Introduction
|
4
|
+
|
5
|
+
Mail is an internet library for Ruby that is designed to handle emails
|
6
|
+
generation, parsing and sending in a simple, rubyesque manner.
|
7
|
+
|
8
|
+
The purpose of this library is to provide a single point of access to handle
|
9
|
+
all email functions, including sending and receiving emails. All network
|
10
|
+
type actions are done through proxy methods to Net::SMTP, Net::POP3 etc.
|
11
|
+
|
12
|
+
Built from my experience with TMail, it is designed to be a pure ruby
|
13
|
+
implementation that makes generating, sending and parsing emails a no
|
14
|
+
brainer.
|
15
|
+
|
16
|
+
It is also designed form the ground up to work with Ruby 1.9. This is because
|
17
|
+
Ruby 1.9 handles text encodings much more magically than Ruby 1.8.x and so
|
18
|
+
these features have been taken full advantage of in this library allowing
|
19
|
+
Mail to handle a lot more messages more cleanly than TMail. Mail does run on
|
20
|
+
Ruby 1.8.x... it's just not as fun to code.
|
21
|
+
|
22
|
+
Finally, Mail has been designed with a very simple object oriented system
|
23
|
+
that really opens up the email messages you are parsing, if you know what
|
24
|
+
you are doing, you can fiddle with every last bit of your email directly.
|
25
|
+
|
26
|
+
== Current Capabilities of Mail
|
27
|
+
|
28
|
+
* RFC2822 Support, Reading and Writing
|
29
|
+
* RFC2045-2049 Support for multipart emails
|
30
|
+
* Support for creating multipart alternate emails
|
31
|
+
* Support for reading multipart/report emails & getting details from such
|
32
|
+
* Support for multibyte emails - needs quite a lot of work and testing
|
33
|
+
* Wrappers for File, Net/POP3, Net/SMTP
|
34
|
+
|
35
|
+
Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
|
36
|
+
emails. There are a few obsoleted syntax emails that it will have problems with, but
|
37
|
+
it also is quite robust, meaning, if it finds something it doesn't understand it will
|
38
|
+
not crash, instead, it will skip the problem and keep parsing. In the case of a header
|
39
|
+
it doesn't understand, it will initialise the header as an optional unstructured
|
40
|
+
field and continue parsing.
|
41
|
+
|
42
|
+
This means Mail won't (ever) crunch your data (I think).
|
43
|
+
|
44
|
+
You can also create MIME emails. There are helper methods for making a
|
45
|
+
multipart/alternate email for text/plain and text/html (the most common pair)
|
46
|
+
and you can manually create any other type of MIME email.
|
47
|
+
|
48
|
+
== Roadmap
|
49
|
+
|
50
|
+
Next TODO:
|
51
|
+
|
52
|
+
* Add multilingual MIME support for mixed ASCII and multibyte headers
|
53
|
+
* Add IMAP wrapper
|
54
|
+
|
55
|
+
== Testing Policy
|
56
|
+
|
57
|
+
Basically... we do BDD on Mail. No method gets written in Mail without a
|
58
|
+
corresponding or covering spec. We expect as a minimum 100% coverage
|
59
|
+
measured by RCov. While this is not perfect by any measure, it is pretty
|
60
|
+
good. Additionally, all functional tests from TMail are to be passing before
|
61
|
+
the gem gets released.
|
62
|
+
|
63
|
+
It also means you can be sure Mail will behave correctly.
|
64
|
+
|
65
|
+
== API Policy
|
66
|
+
|
67
|
+
Right now Mail is still under development for a 1.0.0 release.
|
68
|
+
|
69
|
+
The API will change between now and 1.0.0. How much? Not sure. Basically though
|
70
|
+
once 1.0.0 is released, no API removals within a single point release. All removals
|
71
|
+
to be depreciated with warnings for at least one point release before removal.
|
72
|
+
|
73
|
+
Also, all private or protected methods to be declared as such.
|
74
|
+
|
75
|
+
== Installation
|
76
|
+
|
77
|
+
Installation is fairly simple, I host mail on gemcutter, so you can just do:
|
78
|
+
|
79
|
+
# gem install mail
|
80
|
+
|
81
|
+
if you are on gemcutter, if you aren't, you can by doing:
|
82
|
+
|
83
|
+
# gem install gemcutter
|
84
|
+
# gem tumble
|
85
|
+
# gem install mail
|
86
|
+
|
87
|
+
Warning though, the above will change your first gem repository to gemcutter, this
|
88
|
+
may or may not be a problem for you.
|
89
|
+
|
90
|
+
If you want to install mail manually, you can download the gem from github and do:
|
91
|
+
|
92
|
+
# gem install mail-1.0.0.gem
|
93
|
+
|
94
|
+
== Contributing
|
95
|
+
|
96
|
+
Please do! Contributing is easy in Mail:
|
97
|
+
|
98
|
+
1. Check the Reference RFCs, they are in the References directory, so no excuses.
|
99
|
+
2. Open a ticket on github, maybe someone else has the problem too
|
100
|
+
3. Make a fork of my github repository
|
101
|
+
4. Make a spec driven change to the code base
|
102
|
+
5. Make sure it works and all specs pass, on Ruby versions 1.8.6, 1.8.7 and 1.9
|
103
|
+
6. Update the README if needed to reflect your change / addition
|
104
|
+
7. With all specs passing push your changes back to your fork
|
105
|
+
8. Send me a pull request
|
106
|
+
|
107
|
+
== Usage
|
108
|
+
|
109
|
+
All major mail functions should be able to happen from the Mail::module.
|
110
|
+
So, you should be able to just "require 'mail'" to get started.
|
111
|
+
|
112
|
+
=== Making an email
|
113
|
+
|
114
|
+
require 'mail'
|
115
|
+
|
116
|
+
mail = Mail.new do
|
117
|
+
from 'mikel@test.lindsaar.net'
|
118
|
+
to 'you@test.lindsaar.net'
|
119
|
+
subject 'This is a test email'
|
120
|
+
body File.read('body.txt')
|
121
|
+
end
|
122
|
+
|
123
|
+
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
|
124
|
+
|
125
|
+
=== Making an email, have it your way:
|
126
|
+
|
127
|
+
require 'mail'
|
128
|
+
|
129
|
+
mail = Mail.new do
|
130
|
+
body File.read('body.txt')
|
131
|
+
end
|
132
|
+
|
133
|
+
mail['from'] = 'mikel@test.lindsaar.net'
|
134
|
+
mail[:to] = 'you@test.lindsaar.net'
|
135
|
+
mail.subject = 'This is a test email'
|
136
|
+
|
137
|
+
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
|
138
|
+
|
139
|
+
=== Don't Worry About Message IDs:
|
140
|
+
|
141
|
+
require 'mail'
|
142
|
+
|
143
|
+
mail = Mail.new do
|
144
|
+
to 'you@test.lindsaar.net'
|
145
|
+
body 'Some simple body'
|
146
|
+
end
|
147
|
+
|
148
|
+
mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
|
149
|
+
|
150
|
+
Mail will automatically add a Message-ID field if it is missing and
|
151
|
+
give it a unique, random Message-ID along the lines of:
|
152
|
+
|
153
|
+
<4a7ff76d7016_13a81ab802e1@local.fqdn.mail>
|
154
|
+
|
155
|
+
=== Or do worry about Message-IDs:
|
156
|
+
|
157
|
+
require 'mail'
|
158
|
+
|
159
|
+
mail = Mail.new do
|
160
|
+
to 'you@test.lindsaar.net'
|
161
|
+
message_id '<ThisIsMyMessageId@some.domain.com>'
|
162
|
+
body 'Some simple body'
|
163
|
+
end
|
164
|
+
|
165
|
+
mail.to_s =~ /Message\-ID: <ThisIsMyMessageId@some.domain.com>/ #=> 27
|
166
|
+
|
167
|
+
Mail will take the message_id you assign to it trusting that you know
|
168
|
+
what you are doing.
|
169
|
+
|
170
|
+
=== Sending an email:
|
171
|
+
|
172
|
+
require 'mail'
|
173
|
+
|
174
|
+
Mail.defaults do
|
175
|
+
smtp '127.0.0.1', 25
|
176
|
+
end
|
177
|
+
|
178
|
+
Mail.deliver do
|
179
|
+
from 'me@test.lindsaar.net'
|
180
|
+
to 'you@test.lindsaar.net'
|
181
|
+
subject 'Here is the image you wanted'
|
182
|
+
body File.read('body.txt')
|
183
|
+
add_file 'New Header Image', '/somefile.png'
|
184
|
+
end
|
185
|
+
|
186
|
+
or
|
187
|
+
|
188
|
+
Mail.defaults do
|
189
|
+
smtp '127.0.0.1' # Port 25 defult
|
190
|
+
end
|
191
|
+
|
192
|
+
mail = Mail.new do
|
193
|
+
from 'me@test.lindsaar.net'
|
194
|
+
to 'you@test.lindsaar.net'
|
195
|
+
subject 'Here is the image you wanted'
|
196
|
+
body File.read('body.txt')
|
197
|
+
add_file 'New Header Image', '/somefile.png'
|
198
|
+
end
|
199
|
+
|
200
|
+
mail.deliver!
|
201
|
+
|
202
|
+
=== Getting an email from a pop server:
|
203
|
+
|
204
|
+
require 'mail'
|
205
|
+
|
206
|
+
Mail.defaults do
|
207
|
+
pop3 'mail.myhost.com.au', 110
|
208
|
+
user 'mikel'
|
209
|
+
pass 'mypass'
|
210
|
+
end
|
211
|
+
|
212
|
+
emails = Mail.get_all_mail
|
213
|
+
|
214
|
+
emails.length #=> 12
|
215
|
+
|
216
|
+
=== Reading an Email
|
217
|
+
|
218
|
+
require 'mail'
|
219
|
+
|
220
|
+
mail = Mail.read('/path/to/message.eml')
|
221
|
+
|
222
|
+
mail.envelope.from #=> 'mikel@test.lindsaar.net'
|
223
|
+
mail.from.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
224
|
+
mail.sender.address #=> 'mikel@test.lindsaar.net'
|
225
|
+
mail.to.addresses #=> ['bob@test.lindsaar.net']
|
226
|
+
mail.cc.addresses #=> ['sam@test.lindsaar.net']
|
227
|
+
mail.subject.to_s #=> "This is the subject"
|
228
|
+
mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
|
229
|
+
mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
|
230
|
+
mail.body #=> 'This is the body of the email...
|
231
|
+
|
232
|
+
Many more methods available.
|
233
|
+
|
234
|
+
=== Reading a Multipart Email
|
235
|
+
|
236
|
+
require 'mail'
|
237
|
+
|
238
|
+
mail = Mail.read('multipart_email')
|
239
|
+
|
240
|
+
mail.multipart? #=> true
|
241
|
+
mail.parts.length #=> 2
|
242
|
+
mail.preamble #=> "Text before the first part"
|
243
|
+
mail.epilogue #=> "Text after the last part"
|
244
|
+
mail.parts.map { |p| p.content_type } #=> ['text/plain', 'application/pdf']
|
245
|
+
mail.parts.map { |p| p.class } #=> [Mail::Message, Mail::Message]
|
246
|
+
mail.parts[0].mime_parameters #=> {'charset' => 'ISO-8859-1'}
|
247
|
+
mail.parts[1].mime_parameters #=> {'name' => 'my.pdf'}
|
248
|
+
|
249
|
+
Mail generates a tree of parts. Each message has many or no parts. Each part
|
250
|
+
is another message which can have many or no parts.
|
251
|
+
|
252
|
+
A message will only have parts if it is a multipart/mixed or related/mixed
|
253
|
+
content type and has a boundary defined.
|
254
|
+
|
255
|
+
=== Writing and sending a multipart/alternative (html and text) email
|
256
|
+
|
257
|
+
Mail makes some basic assumptions and makes doing the common thing as
|
258
|
+
simple as possible.... (asking a lot from a mail library)
|
259
|
+
|
260
|
+
require 'mail'
|
261
|
+
|
262
|
+
Mail.defaults do
|
263
|
+
smtp '127.0.0.1' # Port 25 defult
|
264
|
+
end
|
265
|
+
|
266
|
+
mail = Mail.deliver do
|
267
|
+
to 'nicolas@test.lindsaar.net.au'
|
268
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
269
|
+
subject 'First multipart email sent with Mail'
|
270
|
+
text_part do
|
271
|
+
body 'This is plain text'
|
272
|
+
end
|
273
|
+
html_part do
|
274
|
+
content_type 'text/html; charset=UTF-8'
|
275
|
+
body '<h1>This is HTML</h1>'
|
276
|
+
end
|
277
|
+
end
|
278
|
+
|
279
|
+
Mail then delivers the email at the end of the block and returns the
|
280
|
+
resulting Mail::Message object, which you can then inspect if you
|
281
|
+
so desire...
|
282
|
+
|
283
|
+
puts mail.to_s #=>
|
284
|
+
|
285
|
+
To: nicolas@test.lindsaar.net.au
|
286
|
+
From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
|
287
|
+
Subject: First multipart email sent with Mail
|
288
|
+
Content-Type: multipart/alternative;
|
289
|
+
boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659
|
290
|
+
Message-ID: <4a914f12ac7e_6f0f1ab80267d1@baci.local.mail>
|
291
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
292
|
+
Mime-Version: 1.0
|
293
|
+
Content-Transfer-Encoding: 7bit
|
294
|
+
|
295
|
+
|
296
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659
|
297
|
+
Content-ID: <4a914f12c8c4_6f0f1ab80268d6@baci.local.mail>
|
298
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
299
|
+
Mime-Version: 1.0
|
300
|
+
Content-Type: text/plain
|
301
|
+
Content-Transfer-Encoding: 7bit
|
302
|
+
|
303
|
+
This is plain text
|
304
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659
|
305
|
+
Content-Type: text/html; charset=UTF-8
|
306
|
+
Content-ID: <4a914f12cf86_6f0f1ab802692c@baci.local.mail>
|
307
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
308
|
+
Mime-Version: 1.0
|
309
|
+
Content-Transfer-Encoding: 7bit
|
310
|
+
|
311
|
+
<h1>This is HTML</h1>
|
312
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659--
|
313
|
+
|
314
|
+
Mail inserts the content transfer encoding, the mime version,
|
315
|
+
the content-id's and handles the content-type and boundary.
|
316
|
+
|
317
|
+
Mail assumes that if your text in the body is only us-ascii, that your
|
318
|
+
transfer encoding is 7bit and it is text/plain. You can override this
|
319
|
+
by explicitly declaring it.
|
320
|
+
|
321
|
+
=== Making Multipart/Alternate, without a block
|
322
|
+
|
323
|
+
You don't have to use a block with the text and html part included, you
|
324
|
+
can just do it declaratively. However, you need to add Mail::Parts to
|
325
|
+
an email, not Mail::Messages.
|
326
|
+
|
327
|
+
require 'mail'
|
328
|
+
|
329
|
+
mail = Mail.new do
|
330
|
+
to 'nicolas@test.lindsaar.net.au'
|
331
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
332
|
+
subject 'First multipart email sent with Mail'
|
333
|
+
end
|
334
|
+
|
335
|
+
text_part = Mail::Part.new do
|
336
|
+
body 'This is plain text'
|
337
|
+
end
|
338
|
+
|
339
|
+
html_part = Mail::Part.new do
|
340
|
+
content_type 'text/html; charset=UTF-8'
|
341
|
+
body '<h1>This is HTML</h1>'
|
342
|
+
end
|
343
|
+
|
344
|
+
mail.text_part = text_part
|
345
|
+
mail.html_part = html_part
|
346
|
+
|
347
|
+
Results in the same email as done using the block form
|
348
|
+
|
349
|
+
=== Getting error reports from an email:
|
350
|
+
|
351
|
+
require 'mail'
|
352
|
+
|
353
|
+
@mail = Mail.read('/path/to/bounce_message.eml')
|
354
|
+
|
355
|
+
@mail.bounced? #=> true
|
356
|
+
@mail.final_recipient #=> rfc822;mikel@dont.exist.com
|
357
|
+
@mail.action #=> failed
|
358
|
+
@mail.error_status #=> 5.5.0
|
359
|
+
@mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
|
360
|
+
@mail.retryable? #=> false
|
361
|
+
|
362
|
+
=== Attaching and Detaching Files
|
363
|
+
|
364
|
+
require 'mail'
|
365
|
+
|
366
|
+
You can just read the file off an absolute path, Mail will try
|
367
|
+
to guess the mime_type and will encode the file in Base64 for you.
|
368
|
+
|
369
|
+
@mail = Mail.new
|
370
|
+
@mail.add_file(:filename => "/path/to/file.jpg")
|
371
|
+
@mail.parts.first.attachment? #=> true
|
372
|
+
@mail.parts.first.content_transfer_encoding.to_s #=> 'base64'
|
373
|
+
@mail.attachments.first.mime_type #=> 'image/jpg'
|
374
|
+
@mail.attachments.first.filename #=> 'file.jpg'
|
375
|
+
@mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=> true
|
376
|
+
|
377
|
+
Or You can pass in file_data and give it a filename, again, mail
|
378
|
+
will try and guess the mime_type for you.
|
379
|
+
|
380
|
+
@mail = Mail.new
|
381
|
+
file_data = File.read('path/to/myfile.pdf')
|
382
|
+
@mail.add_file(:filename => 'myfile.pdf', :data => file_data)
|
383
|
+
@mail.parts.first.attachment? #=> true
|
384
|
+
@mail.attachments.first.mime_type #=> 'application/pdf'
|
385
|
+
@mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=> true
|
386
|
+
|
387
|
+
You can also override the guessed mime type if you really know better
|
388
|
+
than mail (this should be rarely needed)
|
389
|
+
|
390
|
+
@mail = Mail.new
|
391
|
+
file_data = File.read('path/to/myfile.pdf')
|
392
|
+
@mail.add_file(:filename => 'myfile.pdf',
|
393
|
+
:data => file_data,
|
394
|
+
:mime_type => 'application/x-pdf')
|
395
|
+
@mail.parts.first.mime_type #=> 'application/x-pdf'
|
396
|
+
|
397
|
+
Of course... Mail will round trip an attachment as well
|
398
|
+
|
399
|
+
@mail = Mail.new do
|
400
|
+
to 'nicolas@test.lindsaar.net.au'
|
401
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
402
|
+
subject 'First multipart email sent with Mail'
|
403
|
+
text_part do
|
404
|
+
body 'Here is the attachment you wanted'
|
405
|
+
end
|
406
|
+
html_part do
|
407
|
+
content_type 'text/html; charset=UTF-8'
|
408
|
+
body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
|
409
|
+
end
|
410
|
+
add_file :filename => '/path/to/myfile.pdf'
|
411
|
+
end
|
412
|
+
|
413
|
+
@round_tripped_mail = Mail.new(@mail.encoded)
|
414
|
+
|
415
|
+
@round_tripped_mail.attachments.length #=> 1
|
416
|
+
@round_tripped_mail.attachments.first.filename #=> 'myfile.pdf'
|
417
|
+
|
418
|
+
== License:
|
419
|
+
|
420
|
+
(The MIT License)
|
421
|
+
|
422
|
+
Copyright (c) 2009
|
423
|
+
|
424
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
425
|
+
a copy of this software and associated documentation files (the
|
426
|
+
'Software'), to deal in the Software without restriction, including
|
427
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
428
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
429
|
+
permit persons to whom the Software is furnished to do so, subject to
|
430
|
+
the following conditions:
|
431
|
+
|
432
|
+
The above copyright notice and this permission notice shall be
|
433
|
+
included in all copies or substantial portions of the Software.
|
434
|
+
|
435
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
436
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
437
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
438
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
439
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
440
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
441
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|