mail-trunk 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (137) hide show
  1. data/CHANGELOG.rdoc +555 -0
  2. data/Dependencies.txt +3 -0
  3. data/Gemfile +29 -0
  4. data/README.mkd +583 -0
  5. data/Rakefile +66 -0
  6. data/TODO.rdoc +9 -0
  7. data/lib/VERSION +4 -0
  8. data/lib/mail.rb +89 -0
  9. data/lib/mail/attachments_list.rb +105 -0
  10. data/lib/mail/body.rb +292 -0
  11. data/lib/mail/configuration.rb +73 -0
  12. data/lib/mail/core_extensions/nil.rb +17 -0
  13. data/lib/mail/core_extensions/object.rb +13 -0
  14. data/lib/mail/core_extensions/shellwords.rb +57 -0
  15. data/lib/mail/core_extensions/smtp.rb +25 -0
  16. data/lib/mail/core_extensions/string.rb +31 -0
  17. data/lib/mail/core_extensions/string/access.rb +104 -0
  18. data/lib/mail/core_extensions/string/multibyte.rb +78 -0
  19. data/lib/mail/elements.rb +14 -0
  20. data/lib/mail/elements/address.rb +306 -0
  21. data/lib/mail/elements/address_list.rb +74 -0
  22. data/lib/mail/elements/content_disposition_element.rb +30 -0
  23. data/lib/mail/elements/content_location_element.rb +25 -0
  24. data/lib/mail/elements/content_transfer_encoding_element.rb +24 -0
  25. data/lib/mail/elements/content_type_element.rb +35 -0
  26. data/lib/mail/elements/date_time_element.rb +26 -0
  27. data/lib/mail/elements/envelope_from_element.rb +34 -0
  28. data/lib/mail/elements/message_ids_element.rb +29 -0
  29. data/lib/mail/elements/mime_version_element.rb +26 -0
  30. data/lib/mail/elements/phrase_list.rb +21 -0
  31. data/lib/mail/elements/received_element.rb +30 -0
  32. data/lib/mail/encodings.rb +266 -0
  33. data/lib/mail/encodings/7bit.rb +31 -0
  34. data/lib/mail/encodings/8bit.rb +31 -0
  35. data/lib/mail/encodings/base64.rb +33 -0
  36. data/lib/mail/encodings/binary.rb +31 -0
  37. data/lib/mail/encodings/quoted_printable.rb +38 -0
  38. data/lib/mail/encodings/transfer_encoding.rb +58 -0
  39. data/lib/mail/envelope.rb +35 -0
  40. data/lib/mail/field.rb +224 -0
  41. data/lib/mail/field_list.rb +33 -0
  42. data/lib/mail/fields.rb +35 -0
  43. data/lib/mail/fields/bcc_field.rb +56 -0
  44. data/lib/mail/fields/cc_field.rb +55 -0
  45. data/lib/mail/fields/comments_field.rb +41 -0
  46. data/lib/mail/fields/common/address_container.rb +16 -0
  47. data/lib/mail/fields/common/common_address.rb +125 -0
  48. data/lib/mail/fields/common/common_date.rb +42 -0
  49. data/lib/mail/fields/common/common_field.rb +51 -0
  50. data/lib/mail/fields/common/common_message_id.rb +44 -0
  51. data/lib/mail/fields/common/parameter_hash.rb +58 -0
  52. data/lib/mail/fields/content_description_field.rb +19 -0
  53. data/lib/mail/fields/content_disposition_field.rb +69 -0
  54. data/lib/mail/fields/content_id_field.rb +63 -0
  55. data/lib/mail/fields/content_location_field.rb +42 -0
  56. data/lib/mail/fields/content_transfer_encoding_field.rb +50 -0
  57. data/lib/mail/fields/content_type_field.rb +198 -0
  58. data/lib/mail/fields/date_field.rb +57 -0
  59. data/lib/mail/fields/from_field.rb +55 -0
  60. data/lib/mail/fields/in_reply_to_field.rb +55 -0
  61. data/lib/mail/fields/keywords_field.rb +44 -0
  62. data/lib/mail/fields/message_id_field.rb +83 -0
  63. data/lib/mail/fields/mime_version_field.rb +53 -0
  64. data/lib/mail/fields/optional_field.rb +13 -0
  65. data/lib/mail/fields/received_field.rb +75 -0
  66. data/lib/mail/fields/references_field.rb +55 -0
  67. data/lib/mail/fields/reply_to_field.rb +55 -0
  68. data/lib/mail/fields/resent_bcc_field.rb +55 -0
  69. data/lib/mail/fields/resent_cc_field.rb +55 -0
  70. data/lib/mail/fields/resent_date_field.rb +35 -0
  71. data/lib/mail/fields/resent_from_field.rb +55 -0
  72. data/lib/mail/fields/resent_message_id_field.rb +34 -0
  73. data/lib/mail/fields/resent_sender_field.rb +62 -0
  74. data/lib/mail/fields/resent_to_field.rb +55 -0
  75. data/lib/mail/fields/return_path_field.rb +65 -0
  76. data/lib/mail/fields/sender_field.rb +67 -0
  77. data/lib/mail/fields/structured_field.rb +51 -0
  78. data/lib/mail/fields/subject_field.rb +16 -0
  79. data/lib/mail/fields/to_field.rb +55 -0
  80. data/lib/mail/fields/unstructured_field.rb +182 -0
  81. data/lib/mail/header.rb +265 -0
  82. data/lib/mail/indifferent_hash.rb +146 -0
  83. data/lib/mail/mail.rb +255 -0
  84. data/lib/mail/message.rb +2017 -0
  85. data/lib/mail/multibyte.rb +42 -0
  86. data/lib/mail/multibyte/chars.rb +474 -0
  87. data/lib/mail/multibyte/exceptions.rb +8 -0
  88. data/lib/mail/multibyte/unicode.rb +392 -0
  89. data/lib/mail/multibyte/utils.rb +60 -0
  90. data/lib/mail/network.rb +13 -0
  91. data/lib/mail/network/delivery_methods/file_delivery.rb +40 -0
  92. data/lib/mail/network/delivery_methods/sendmail.rb +62 -0
  93. data/lib/mail/network/delivery_methods/smtp.rb +137 -0
  94. data/lib/mail/network/delivery_methods/smtp_connection.rb +74 -0
  95. data/lib/mail/network/delivery_methods/test_mailer.rb +40 -0
  96. data/lib/mail/network/retriever_methods/base.rb +63 -0
  97. data/lib/mail/network/retriever_methods/imap.rb +158 -0
  98. data/lib/mail/network/retriever_methods/pop3.rb +140 -0
  99. data/lib/mail/network/retriever_methods/test_retriever.rb +47 -0
  100. data/lib/mail/parsers/address_lists.rb +64 -0
  101. data/lib/mail/parsers/address_lists.treetop +19 -0
  102. data/lib/mail/parsers/content_disposition.rb +535 -0
  103. data/lib/mail/parsers/content_disposition.treetop +46 -0
  104. data/lib/mail/parsers/content_location.rb +139 -0
  105. data/lib/mail/parsers/content_location.treetop +20 -0
  106. data/lib/mail/parsers/content_transfer_encoding.rb +162 -0
  107. data/lib/mail/parsers/content_transfer_encoding.treetop +20 -0
  108. data/lib/mail/parsers/content_type.rb +967 -0
  109. data/lib/mail/parsers/content_type.treetop +68 -0
  110. data/lib/mail/parsers/date_time.rb +114 -0
  111. data/lib/mail/parsers/date_time.treetop +11 -0
  112. data/lib/mail/parsers/envelope_from.rb +194 -0
  113. data/lib/mail/parsers/envelope_from.treetop +32 -0
  114. data/lib/mail/parsers/message_ids.rb +45 -0
  115. data/lib/mail/parsers/message_ids.treetop +15 -0
  116. data/lib/mail/parsers/mime_version.rb +144 -0
  117. data/lib/mail/parsers/mime_version.treetop +19 -0
  118. data/lib/mail/parsers/phrase_lists.rb +45 -0
  119. data/lib/mail/parsers/phrase_lists.treetop +15 -0
  120. data/lib/mail/parsers/received.rb +71 -0
  121. data/lib/mail/parsers/received.treetop +11 -0
  122. data/lib/mail/parsers/rfc2045.rb +464 -0
  123. data/lib/mail/parsers/rfc2045.treetop +36 -0
  124. data/lib/mail/parsers/rfc2822.rb +5341 -0
  125. data/lib/mail/parsers/rfc2822.treetop +410 -0
  126. data/lib/mail/parsers/rfc2822_obsolete.rb +3757 -0
  127. data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
  128. data/lib/mail/part.rb +116 -0
  129. data/lib/mail/parts_list.rb +51 -0
  130. data/lib/mail/patterns.rb +35 -0
  131. data/lib/mail/utilities.rb +215 -0
  132. data/lib/mail/version.rb +24 -0
  133. data/lib/mail/version_specific/ruby_1_8.rb +98 -0
  134. data/lib/mail/version_specific/ruby_1_9.rb +113 -0
  135. data/lib/tasks/corpus.rake +125 -0
  136. data/lib/tasks/treetop.rake +10 -0
  137. metadata +221 -0
@@ -0,0 +1,3 @@
1
+ treetop: we need to include this in the gem spec
2
+ tlsmail: if ruby < 1.8.6... we could make it optional, or embed it in Mail
3
+ mime/types: I think we embed a simplified version, or help maintain it, it is old (2006)
data/Gemfile ADDED
@@ -0,0 +1,29 @@
1
+ source :rubygems
2
+
3
+ gem "activesupport", ">= 2.3.6"
4
+ gem "tlsmail" if RUBY_VERSION <= '1.8.6'
5
+ gem "mime-types", "~> 1.16"
6
+ gem "treetop", "~> 1.4.8"
7
+ gem "i18n", ">= 0.4.0"
8
+
9
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
10
+ gem 'jruby-openssl'
11
+ end
12
+
13
+ group :test do
14
+ gem "ZenTest", "~> 4.4.0"
15
+ gem "rake", "~> 0.8.7"
16
+ gem "bundler"
17
+ gem "rspec", "~> 1.3.0"
18
+ gem "diff-lcs"
19
+ case
20
+ when defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
21
+ # Skip it
22
+ when RUBY_PLATFORM == 'java'
23
+ # Skip it
24
+ when RUBY_VERSION < '1.9'
25
+ gem "ruby-debug"
26
+ else
27
+ # Skip it
28
+ end
29
+ end
@@ -0,0 +1,583 @@
1
+ Mail
2
+ ====
3
+
4
+ Introduction
5
+ ------------
6
+
7
+ Mail is an internet library for Ruby that is designed to handle emails
8
+ generation, parsing and sending in a simple, rubyesque manner.
9
+
10
+ The purpose of this library is to provide a single point of access to handle
11
+ all email functions, including sending and receiving emails. All network
12
+ type actions are done through proxy methods to Net::SMTP, Net::POP3 etc.
13
+
14
+ Built from my experience with TMail, it is designed to be a pure ruby
15
+ implementation that makes generating, sending and parsing emails a no
16
+ brainer.
17
+
18
+ It is also designed form the ground up to work with Ruby 1.9. This is because
19
+ Ruby 1.9 handles text encodings much more magically than Ruby 1.8.x and so
20
+ these features have been taken full advantage of in this library allowing
21
+ Mail to handle a lot more messages more cleanly than TMail. Mail does run on
22
+ Ruby 1.8.x... it's just not as fun to code.
23
+
24
+ Finally, Mail has been designed with a very simple object oriented system
25
+ that really opens up the email messages you are parsing, if you know what
26
+ you are doing, you can fiddle with every last bit of your email directly.
27
+
28
+ Compatibility
29
+ -------------
30
+
31
+ Mail is tested and works on the following platforms:
32
+
33
+ * jruby-1.5.2 [ x86\_64-java ]
34
+ * ree-1.8.7-2010.02 [ x86\_64 ]
35
+ * ruby-1.8.6-p399 [ x86\_64 ]
36
+ * ruby-1.8.7-p302 [ x86\_64 ]
37
+ * ruby-1.9.2-p0 [ x86\_64 ]
38
+
39
+ Discussion
40
+ ----------
41
+
42
+ If you want to discuss mail with like minded individuals, please subscribe to
43
+ the [Google Group](http://groups.google.com/group/mail-ruby).
44
+
45
+ Current Capabilities of Mail
46
+ ----------------------------
47
+
48
+ * RFC2822 Support, Reading and Writing
49
+ * RFC2045-2049 Support for multipart emails
50
+ * Support for creating multipart alternate emails
51
+ * Support for reading multipart/report emails &amp; getting details from such
52
+ * Support for multibyte emails - needs quite a lot of work and testing
53
+ * Wrappers for File, Net/POP3, Net/SMTP
54
+ * Auto encoding of non US-ASCII header fields
55
+ * Auto encoding of non US-ASCII bodies
56
+
57
+ Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
58
+ emails. There are a few obsoleted syntax emails that it will have problems with, but
59
+ it also is quite robust, meaning, if it finds something it doesn't understand it will
60
+ not crash, instead, it will skip the problem and keep parsing. In the case of a header
61
+ it doesn't understand, it will initialise the header as an optional unstructured
62
+ field and continue parsing.
63
+
64
+ This means Mail won't (ever) crunch your data (I think).
65
+
66
+ You can also create MIME emails. There are helper methods for making a
67
+ multipart/alternate email for text/plain and text/html (the most common pair)
68
+ and you can manually create any other type of MIME email.
69
+
70
+ Roadmap
71
+ -------
72
+
73
+ Next TODO:
74
+
75
+ * Improve MIME support for character sets in headers, currently works, mostly, needs
76
+ refinement.
77
+
78
+ Testing Policy
79
+ --------------
80
+
81
+ Basically... we do BDD on Mail. No method gets written in Mail without a
82
+ corresponding or covering spec. We expect as a minimum 100% coverage
83
+ measured by RCov. While this is not perfect by any measure, it is pretty
84
+ good. Additionally, all functional tests from TMail are to be passing before
85
+ the gem gets released.
86
+
87
+ It also means you can be sure Mail will behave correctly.
88
+
89
+ API Policy
90
+ ----------
91
+
92
+ No API removals within a single point release. All removals to be depreciated with
93
+ warnings for at least one MINOR point release before removal.
94
+
95
+ Also, all private or protected methods to be declared as such - though this is still I/P.
96
+
97
+ Installation
98
+ ------------
99
+
100
+ Installation is fairly simple, I host mail on rubygems, so you can just do:
101
+
102
+ # gem install mail
103
+
104
+ Encodings
105
+ ---------
106
+
107
+ If you didn't know, handling encodings in Emails is not as straight forward as you
108
+ would hope.
109
+
110
+ I have tried to simplify it some:
111
+
112
+ 1. All objects that can render into an email, have an `#encoded` method. Encoded will
113
+ return the object as a complete string ready to send in the mail system, that is,
114
+ it will include the header field and value and CRLF at the end and wrapped as
115
+ needed.
116
+
117
+ 2. All objects that can render into an email, have a :decoded method. Decoded will
118
+ return the object's "value" only as a string. This means it will not include
119
+ the header fields (like 'To:' or 'Subject:').
120
+
121
+ 3. By default, calling <code>#to_s</code> on a container object will call its encoded
122
+ method, while <code>#to_s</code> on a field object will call it's decoded method.
123
+ So calling <code>#to_s</code> on a Mail object will return the mail, all encoded
124
+ ready to send, while calling <code>#to_s</code> on the From field or the body will
125
+ return the decoded value of the object. The header object of Mail is considered a
126
+ container. If you are in doubt, call <code>#encoded</code>, or <code>#decoded</code>
127
+ explicitly, this is safer if you are not sure.
128
+
129
+ 4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
130
+ provide decoded parameter values when you call the parameter names as methods against
131
+ the object.
132
+
133
+ 5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
134
+ provide encoded parameter values when you call the parameter names through the
135
+ <code>object.parameters['<parameter_name>']</code> method call.
136
+
137
+ Contributing
138
+ ------------
139
+
140
+ Please do! Contributing is easy in Mail:
141
+
142
+ 1. Check the Reference RFCs, they are in the References directory, so no excuses.
143
+ 2. Open a ticket on GitHub, maybe someone else has the problem too
144
+ 3. Make a fork of my GitHub repository
145
+ 4. Make a spec driven change to the code base
146
+ 5. Make sure it works and all specs pass, on Ruby versions 1.8.6, 1.8.7 and 1.9
147
+ 6. Update the README if needed to reflect your change / addition
148
+ 7. With all specs passing push your changes back to your fork
149
+ 8. Send me a pull request
150
+
151
+ Usage
152
+ -----
153
+
154
+ All major mail functions should be able to happen from the Mail module.
155
+ So, you should be able to just <code>require 'mail'</code> to get started.
156
+
157
+ ### Making an email
158
+
159
+ ```ruby
160
+ mail = Mail.new do
161
+ from 'mikel@test.lindsaar.net'
162
+ to 'you@test.lindsaar.net'
163
+ subject 'This is a test email'
164
+ body File.read('body.txt')
165
+ end
166
+
167
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
168
+ ```
169
+
170
+ ### Making an email, have it your way:
171
+
172
+ ```ruby
173
+ mail = Mail.new do
174
+ body File.read('body.txt')
175
+ end
176
+
177
+ mail['from'] = 'mikel@test.lindsaar.net'
178
+ mail[:to] = 'you@test.lindsaar.net'
179
+ mail.subject = 'This is a test email'
180
+
181
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
182
+ ```
183
+
184
+ ### Don't Worry About Message IDs:
185
+
186
+ ```ruby
187
+ mail = Mail.new do
188
+ to 'you@test.lindsaar.net'
189
+ body 'Some simple body'
190
+ end
191
+
192
+ mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
193
+ ```
194
+
195
+ Mail will automatically add a Message-ID field if it is missing and
196
+ give it a unique, random Message-ID along the lines of:
197
+
198
+ <4a7ff76d7016_13a81ab802e1@local.fqdn.mail>
199
+
200
+ ### Or do worry about Message-IDs:
201
+
202
+ ```ruby
203
+ mail = Mail.new do
204
+ to 'you@test.lindsaar.net'
205
+ message_id '<ThisIsMyMessageId@some.domain.com>'
206
+ body 'Some simple body'
207
+ end
208
+
209
+ mail.to_s =~ /Message\-ID: <ThisIsMyMessageId@some.domain.com>/ #=> 27
210
+ ```
211
+
212
+ Mail will take the message_id you assign to it trusting that you know
213
+ what you are doing.
214
+
215
+ ### Sending an email:
216
+
217
+ Mail defaults to sending via SMTP to local host port 25. If you have a
218
+ sendmail or postfix daemon running on on this port, sending email is as
219
+ easy as:
220
+
221
+ ```ruby
222
+ Mail.deliver do
223
+ from 'me@test.lindsaar.net'
224
+ to 'you@test.lindsaar.net'
225
+ subject 'Here is the image you wanted'
226
+ body File.read('body.txt')
227
+ add_file '/full/path/to/somefile.png'
228
+ end
229
+ ```
230
+
231
+ or
232
+
233
+ ```ruby
234
+ mail = Mail.new do
235
+ from 'me@test.lindsaar.net'
236
+ to 'you@test.lindsaar.net'
237
+ subject 'Here is the image you wanted'
238
+ body File.read('body.txt')
239
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
240
+ end
241
+
242
+ mail.deliver!
243
+ ```
244
+
245
+ Sending via sendmail can be done like so:
246
+
247
+ ```ruby
248
+ mail = Mail.new do
249
+ from 'me@test.lindsaar.net'
250
+ to 'you@test.lindsaar.net'
251
+ subject 'Here is the image you wanted'
252
+ body File.read('body.txt')
253
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
254
+ end
255
+
256
+ mail.delivery_method :sendmail
257
+
258
+ mail.deliver
259
+ ```
260
+
261
+ ### Getting emails from a pop server:
262
+
263
+ You can configure Mail to receive email using <code>retriever_method</code>
264
+ within <code>Mail.defaults</code>:
265
+
266
+ ```ruby
267
+ Mail.defaults do
268
+ retriever_method :pop3, :address => "pop.gmail.com",
269
+ :port => 995,
270
+ :user_name => '<username>',
271
+ :password => '<password>',
272
+ :enable_ssl => true
273
+ end
274
+ ```
275
+
276
+ You can access incoming email in a number of ways.
277
+
278
+ The most recent email:
279
+
280
+ ```ruby
281
+ Mail.all #=> Returns an array of all emails
282
+ Mail.first #=> Returns the first unread email
283
+ Mail.last #=> Returns the first unread email
284
+ ```
285
+
286
+ The first 10 emails sorted by date in ascending order:
287
+
288
+ ```ruby
289
+ emails = Mail.find(:what => :first, :count => 10, :order => :asc)
290
+ emails.length #=> 10
291
+ ```
292
+
293
+ Or even all emails:
294
+
295
+ ```ruby
296
+ emails = Mail.all
297
+ emails.length #=> LOTS!
298
+ ```
299
+
300
+
301
+ ### Reading an Email
302
+
303
+ ```ruby
304
+ mail = Mail.read('/path/to/message.eml')
305
+
306
+ mail.envelope.from #=> 'mikel@test.lindsaar.net'
307
+ mail.from.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
308
+ mail.sender.address #=> 'mikel@test.lindsaar.net'
309
+ mail.to #=> 'bob@test.lindsaar.net'
310
+ mail.cc #=> 'sam@test.lindsaar.net'
311
+ mail.subject #=> "This is the subject"
312
+ mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
313
+ mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
314
+ mail.body.decoded #=> 'This is the body of the email...
315
+ ```
316
+
317
+ Many more methods available.
318
+
319
+ ### Reading a Multipart Email
320
+
321
+ ```ruby
322
+ mail = Mail.read('multipart_email')
323
+
324
+ mail.multipart? #=> true
325
+ mail.parts.length #=> 2
326
+ mail.preamble #=> "Text before the first part"
327
+ mail.epilogue #=> "Text after the last part"
328
+ mail.parts.map { |p| p.content_type } #=> ['text/plain', 'application/pdf']
329
+ mail.parts.map { |p| p.class } #=> [Mail::Message, Mail::Message]
330
+ mail.parts[0].content_type_parameters #=> {'charset' => 'ISO-8859-1'}
331
+ mail.parts[1].content_type_parameters #=> {'name' => 'my.pdf'}
332
+ ```
333
+
334
+ Mail generates a tree of parts. Each message has many or no parts. Each part
335
+ is another message which can have many or no parts.
336
+
337
+ A message will only have parts if it is a multipart/mixed or related/mixed
338
+ content type and has a boundary defined.
339
+
340
+ ### Writing and sending a multipart/alternative (html and text) email
341
+
342
+ Mail makes some basic assumptions and makes doing the common thing as
343
+ simple as possible.... (asking a lot from a mail library)
344
+
345
+ ```ruby
346
+ mail = Mail.deliver do
347
+ to 'nicolas@test.lindsaar.net.au'
348
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
349
+ subject 'First multipart email sent with Mail'
350
+
351
+ text_part do
352
+ body 'This is plain text'
353
+ end
354
+
355
+ html_part do
356
+ content_type 'text/html; charset=UTF-8'
357
+ body '<h1>This is HTML</h1>'
358
+ end
359
+ end
360
+ ```
361
+
362
+ Mail then delivers the email at the end of the block and returns the
363
+ resulting Mail::Message object, which you can then inspect if you
364
+ so desire...
365
+
366
+ ```
367
+ puts mail.to_s #=>
368
+
369
+ To: nicolas@test.lindsaar.net.au
370
+ From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
371
+ Subject: First multipart email sent with Mail
372
+ Content-Type: multipart/alternative;
373
+ boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659
374
+ Message-ID: <4a914f12ac7e_6f0f1ab80267d1@baci.local.mail>
375
+ Date: Mon, 24 Aug 2009 00:15:46 +1000
376
+ Mime-Version: 1.0
377
+ Content-Transfer-Encoding: 7bit
378
+
379
+
380
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659
381
+ Content-ID: <4a914f12c8c4_6f0f1ab80268d6@baci.local.mail>
382
+ Date: Mon, 24 Aug 2009 00:15:46 +1000
383
+ Mime-Version: 1.0
384
+ Content-Type: text/plain
385
+ Content-Transfer-Encoding: 7bit
386
+
387
+ This is plain text
388
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659
389
+ Content-Type: text/html; charset=UTF-8
390
+ Content-ID: <4a914f12cf86_6f0f1ab802692c@baci.local.mail>
391
+ Date: Mon, 24 Aug 2009 00:15:46 +1000
392
+ Mime-Version: 1.0
393
+ Content-Transfer-Encoding: 7bit
394
+
395
+ <h1>This is HTML</h1>
396
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659--
397
+ ```
398
+
399
+ Mail inserts the content transfer encoding, the mime version,
400
+ the content-id's and handles the content-type and boundary.
401
+
402
+ Mail assumes that if your text in the body is only us-ascii, that your
403
+ transfer encoding is 7bit and it is text/plain. You can override this
404
+ by explicitly declaring it.
405
+
406
+ ### Making Multipart/Alternate, without a block
407
+
408
+ You don't have to use a block with the text and html part included, you
409
+ can just do it declaratively. However, you need to add Mail::Parts to
410
+ an email, not Mail::Messages.
411
+
412
+ ```ruby
413
+ mail = Mail.new do
414
+ to 'nicolas@test.lindsaar.net.au'
415
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
416
+ subject 'First multipart email sent with Mail'
417
+ end
418
+
419
+ text_part = Mail::Part.new do
420
+ body 'This is plain text'
421
+ end
422
+
423
+ html_part = Mail::Part.new do
424
+ content_type 'text/html; charset=UTF-8'
425
+ body '<h1>This is HTML</h1>'
426
+ end
427
+
428
+ mail.text_part = text_part
429
+ mail.html_part = html_part
430
+ ```
431
+
432
+ Results in the same email as done using the block form
433
+
434
+ ### Getting error reports from an email:
435
+
436
+ ```ruby
437
+ @mail = Mail.read('/path/to/bounce_message.eml')
438
+
439
+ @mail.bounced? #=> true
440
+ @mail.final_recipient #=> rfc822;mikel@dont.exist.com
441
+ @mail.action #=> failed
442
+ @mail.error_status #=> 5.5.0
443
+ @mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
444
+ @mail.retryable? #=> false
445
+ ```
446
+
447
+ ### Attaching and Detaching Files
448
+
449
+ You can just read the file off an absolute path, Mail will try
450
+ to guess the mime_type and will encode the file in Base64 for you.
451
+
452
+ ```ruby
453
+ @mail = Mail.new
454
+ @mail.add_file("/path/to/file.jpg")
455
+ @mail.parts.first.attachment? #=> true
456
+ @mail.parts.first.content_transfer_encoding.to_s #=> 'base64'
457
+ @mail.attachments.first.mime_type #=> 'image/jpg'
458
+ @mail.attachments.first.filename #=> 'file.jpg'
459
+ @mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=> true
460
+ ```
461
+
462
+ Or You can pass in file_data and give it a filename, again, mail
463
+ will try and guess the mime_type for you.
464
+
465
+ ```ruby
466
+ @mail = Mail.new
467
+ @mail.attachments['myfile.pdf'] = File.read('path/to/myfile.pdf')
468
+ @mail.parts.first.attachment? #=> true
469
+ @mail.attachments.first.mime_type #=> 'application/pdf'
470
+ @mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=> true
471
+ ```
472
+
473
+ You can also override the guessed MIME media type if you really know better
474
+ than mail (this should be rarely needed)
475
+
476
+ ```ruby
477
+ @mail = Mail.new
478
+ file_data = File.read('path/to/myfile.pdf')
479
+ @mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
480
+ :content => File.read('path/to/myfile.pdf') }
481
+ @mail.parts.first.mime_type #=> 'application/x-pdf'
482
+ ```
483
+
484
+ Of course... Mail will round trip an attachment as well
485
+
486
+ ```ruby
487
+ @mail = Mail.new do
488
+ to 'nicolas@test.lindsaar.net.au'
489
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
490
+ subject 'First multipart email sent with Mail'
491
+
492
+ text_part do
493
+ body 'Here is the attachment you wanted'
494
+ end
495
+
496
+ html_part do
497
+ content_type 'text/html; charset=UTF-8'
498
+ body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
499
+ end
500
+
501
+ add_file '/path/to/myfile.pdf'
502
+ end
503
+
504
+ @round_tripped_mail = Mail.new(@mail.encoded)
505
+
506
+ @round_tripped_mail.attachments.length #=> 1
507
+ @round_tripped_mail.attachments.first.filename #=> 'myfile.pdf'
508
+ ```
509
+
510
+ Using Mail with Testing or Spec'ing Libraries
511
+ ---------------------------------------------
512
+
513
+ If mail is part of your system, you'll need a way to test it without actually
514
+ sending emails, the TestMailer can do this for you.
515
+
516
+ ```
517
+ require 'mail'
518
+ => true
519
+ Mail.defaults do
520
+ delivery_method :test
521
+ end
522
+ => #<Mail::Configuration:0x19345a8 @delivery_method=Mail::TestMailer>
523
+ Mail::TestMailer.deliveries
524
+ => []
525
+ Mail.deliver do
526
+ to 'mikel@me.com'
527
+ from 'you@you.com'
528
+ subject 'testing'
529
+ body 'hello'
530
+ end
531
+ => #<Mail::Message:0x19284ec ...
532
+ Mail::TestMailer.deliveries.length
533
+ => 1
534
+ Mail::TestMailer.deliveries.first
535
+ => #<Mail::Message:0x19284ec ...
536
+ Mail::TestMailer.deliveries.clear
537
+ => []
538
+ ```
539
+
540
+ Excerpts from TREC Spam Corpus 2005
541
+ -----------------------------------
542
+
543
+ The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
544
+ 2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
545
+ that project and license agreement. They are used in this project to verify
546
+ and describe the development of this email parser implementation.
547
+
548
+ http://plg.uwaterloo.ca/~gvcormac/treccorpus/
549
+
550
+ They are used as allowed by 'Permitted Uses, Clause 3':
551
+
552
+ "Small excerpts of the information may be displayed to others
553
+ or published in a scientific or technical context, solely for
554
+ the purpose of describing the research and development and
555
+ related issues."
556
+
557
+ -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
558
+
559
+ License
560
+ -------
561
+
562
+ (The MIT License)
563
+
564
+ Copyright (c) 2009, 2010, 2011
565
+
566
+ Permission is hereby granted, free of charge, to any person obtaining
567
+ a copy of this software and associated documentation files (the
568
+ 'Software'), to deal in the Software without restriction, including
569
+ without limitation the rights to use, copy, modify, merge, publish,
570
+ distribute, sublicense, and/or sell copies of the Software, and to
571
+ permit persons to whom the Software is furnished to do so, subject to
572
+ the following conditions:
573
+
574
+ The above copyright notice and this permission notice shall be
575
+ included in all copies or substantial portions of the Software.
576
+
577
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
578
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
579
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
580
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
581
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
582
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
583
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.