mail 2.3.3 → 2.4.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.

Files changed (38) hide show
  1. data/CHANGELOG.rdoc +26 -0
  2. data/CONTRIBUTING.md +45 -0
  3. data/Gemfile +2 -8
  4. data/Gemfile.lock +31 -0
  5. data/README.md +649 -0
  6. data/Rakefile +4 -30
  7. data/lib/VERSION +2 -2
  8. data/lib/mail.rb +3 -1
  9. data/lib/mail/attachments_list.rb +2 -3
  10. data/lib/mail/body.rb +1 -2
  11. data/lib/mail/configuration.rb +3 -3
  12. data/lib/mail/core_extensions/nil.rb +4 -0
  13. data/lib/mail/core_extensions/shellwords.rb +57 -0
  14. data/lib/mail/core_extensions/string.rb +6 -4
  15. data/lib/mail/core_extensions/string/access.rb +41 -0
  16. data/lib/mail/encodings.rb +8 -8
  17. data/lib/mail/field.rb +2 -0
  18. data/lib/mail/fields/common/parameter_hash.rb +1 -1
  19. data/lib/mail/fields/unstructured_field.rb +1 -1
  20. data/lib/mail/matchers/has_sent_mail.rb +124 -0
  21. data/lib/mail/message.rb +72 -19
  22. data/lib/mail/multibyte/chars.rb +2 -2
  23. data/lib/mail/multibyte/utils.rb +1 -1
  24. data/lib/mail/network/delivery_methods/exim.rb +5 -38
  25. data/lib/mail/network/delivery_methods/file_delivery.rb +2 -2
  26. data/lib/mail/network/delivery_methods/sendmail.rb +3 -3
  27. data/lib/mail/network/delivery_methods/smtp.rb +20 -4
  28. data/lib/mail/network/retriever_methods/imap.rb +4 -2
  29. data/lib/mail/parsers/rfc2822.treetop +1 -1
  30. data/lib/mail/parsers/rfc2822_obsolete.rb +14 -3
  31. data/lib/mail/parsers/rfc2822_obsolete.treetop +2 -2
  32. data/lib/mail/parts_list.rb +4 -0
  33. data/lib/mail/utilities.rb +6 -2
  34. data/lib/mail/version_specific/ruby_1_8.rb +1 -1
  35. data/lib/mail/version_specific/ruby_1_9.rb +8 -4
  36. metadata +18 -12
  37. data/README.rdoc +0 -563
  38. data/lib/mail/core_extensions/shell_escape.rb +0 -56
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,29 @@
1
+ == Sun Jan 15 18:15:56 UTC 2011 Mikel Lindsaar <mikel@reinteractive.net>
2
+
3
+ * Speed up reading of messages by about 12x
4
+ * Added Message#without_attachments! that removes all message's attachments
5
+ * Added shoulda-style RSpec matchers
6
+ * Added support for @ in display name
7
+ * Added support for the :tls and :ssl options
8
+ * Added UTF-16 and UTF-32 support
9
+ * Added Exim as it's own delivery manager
10
+ * Added Ruby 1.9.3 compatibility
11
+ * Fix for Sendmail return-path escaping
12
+ * Fix for alias for SJIS was changed from shift_jis to windows-31J in Ruby 1.9.3
13
+ * Fix for undefined method 'constantize' error when no ActiveSupport loaded
14
+ * Fix Mail::Field#== comparison
15
+ * Fixed Regexp warning: character class has duplicated range
16
+ * Fixed encoding non-latin names in addresses
17
+ * Fixed issue with non-7bit attachment filenames
18
+ * Now define String#blank? only if not defined yet
19
+ * Decoding text parts using charset from Content-Type field
20
+ * Per RFC 5322, do not accept emails with consecutive dots
21
+ * Bunch of bug fixes from contributed pull requests
22
+ * Travis CI setup and passing on 6 rubies
23
+ * Upgrade RSpec to 2.8.0
24
+ * Lots of warnings fixed
25
+ * Version bump to 2.4.0 and gem release
26
+
1
27
  == Tue Apr 26 09:59:56 UTC 2011 Mikel Lindsaar <mikel@rubyx.com>
2
28
 
3
29
  * Remove ActiveSupport from the dependencies, load Active Support if present, or use internals if not
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,45 @@
1
+ Contributing to Mail
2
+ ====================
3
+
4
+ Hi there, I welcome pull requests! Here are some thoughts on how to get your
5
+ pull request merged quickly:
6
+
7
+ 1. Check the Reference RFCs, they are in the References directory, so no excuses.
8
+ 2. Check for a ticket on GitHub, maybe someone else has the problem too
9
+ 3. Make a fork of my GitHub repository
10
+ 4. Run the specs. We only take pull requests with passing tests, and it's great
11
+ to know that you have a clean slate: `bundle && bundle exec rake`
12
+ 5. Add a spec for your change. Only refactoring and documentation changes
13
+ require no new specs. If you are adding functionality or fixing a bug, we need
14
+ a spec!
15
+ 6. Test the spec _at_ _least_ against MRI-1.9.2 and MRI-1.8.7
16
+ 7. Update the README if needed to reflect your change / addition
17
+ 8. With all specs passing push your changes back to your fork
18
+ 9. Send me a pull request
19
+
20
+ Note, specs that break MRI 1.8.7 will not be accepted.
21
+
22
+ At this point you're waiting on us. We like to at least comment on, if not
23
+ accept, pull requests within three business days (and, typically, one business
24
+ day). We may suggest some changes or improvements or alternatives.
25
+
26
+ Some things that will increase the chance that your pull request is accepted,
27
+ taken straight from the Ruby on Rails guide:
28
+
29
+ * Tell me you have tested it against more than one version of Ruby, RVM is great for
30
+ this. I test against 7 rubies before I push into master.
31
+ * Use good, idiomatic, strcutred and modular code
32
+ * Include tests that fail without your code, and pass with it
33
+ * Update the documentation, the surrounding one, examples elsewhere, guides,
34
+ whatever is affected by your contribution
35
+
36
+ Syntax:
37
+
38
+ * Two spaces, no tabs.
39
+ * No trailing whitespace. Blank lines should not have any space.
40
+ * Prefer &&/|| over and/or.
41
+ * MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
42
+ * a = b and not a=b.
43
+ * Follow the conventions you see used in the source already.
44
+
45
+ And in case we didn't emphasize it enough: we love specs!
data/Gemfile CHANGED
@@ -11,14 +11,8 @@ if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
11
11
  end
12
12
 
13
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 "rspec-core", "~> 2.4.0"
19
- gem "rspec-mocks", "~> 2.4.0"
20
- gem "rspec-expectations", "~> 2.4.0"
21
- gem "diff-lcs"
14
+ gem "rake", "> 0.8.7"
15
+ gem "rspec", "~> 2.8.0"
22
16
  case
23
17
  when defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
24
18
  # Skip it
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.6)
5
+ diff-lcs (1.1.3)
6
+ i18n (0.5.0)
7
+ mime-types (1.16)
8
+ polyglot (0.3.1)
9
+ rake (0.9.2.2)
10
+ rspec (2.8.0)
11
+ rspec-core (~> 2.8.0)
12
+ rspec-expectations (~> 2.8.0)
13
+ rspec-mocks (~> 2.8.0)
14
+ rspec-core (2.8.0)
15
+ rspec-expectations (2.8.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.8.0)
18
+ treetop (1.4.9)
19
+ polyglot (>= 0.3.1)
20
+
21
+ PLATFORMS
22
+ java
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ activesupport (>= 2.3.6)
27
+ i18n (>= 0.4.0)
28
+ mime-types (~> 1.16)
29
+ rake (> 0.8.7)
30
+ rspec (~> 2.8.0)
31
+ treetop (~> 1.4.8)
data/README.md ADDED
@@ -0,0 +1,649 @@
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 by Travis (![Travis Build Status](https://secure.travis-ci.org/mikel/mail.png "Build Status")) and works on the following platforms:
32
+
33
+ * jruby-1.6.5.1 [ x86_64 ]
34
+ * rbx-head-d18 [ x86_64 ]
35
+ * ree-1.8.7-2011.03 [ i686 ]
36
+ * ruby-1.8.7-p357 [ i686 ]
37
+ * ruby-1.9.2-p290 [ x86_64 ]
38
+ * ruby-1.9.3-p0 [ x86_64 ]
39
+
40
+ Discussion
41
+ ----------
42
+
43
+ If you want to discuss mail with like minded individuals, please subscribe to
44
+ the [Google Group](http://groups.google.com/group/mail-ruby).
45
+
46
+ Current Capabilities of Mail
47
+ ----------------------------
48
+
49
+ * RFC2822 Support, Reading and Writing
50
+ * RFC2045-2049 Support for multipart emails
51
+ * Support for creating multipart alternate emails
52
+ * Support for reading multipart/report emails &amp; getting details from such
53
+ * Support for multibyte emails - needs quite a lot of work and testing
54
+ * Wrappers for File, Net/POP3, Net/SMTP
55
+ * Auto encoding of non US-ASCII header fields
56
+ * Auto encoding of non US-ASCII bodies
57
+
58
+ Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
59
+ emails. There are a few obsoleted syntax emails that it will have problems with, but
60
+ it also is quite robust, meaning, if it finds something it doesn't understand it will
61
+ not crash, instead, it will skip the problem and keep parsing. In the case of a header
62
+ it doesn't understand, it will initialise the header as an optional unstructured
63
+ field and continue parsing.
64
+
65
+ This means Mail won't (ever) crunch your data (I think).
66
+
67
+ You can also create MIME emails. There are helper methods for making a
68
+ multipart/alternate email for text/plain and text/html (the most common pair)
69
+ and you can manually create any other type of MIME email.
70
+
71
+ Roadmap
72
+ -------
73
+
74
+ Next TODO:
75
+
76
+ * Improve MIME support for character sets in headers, currently works, mostly, needs
77
+ refinement.
78
+
79
+ Testing Policy
80
+ --------------
81
+
82
+ Basically... we do BDD on Mail. No method gets written in Mail without a
83
+ corresponding or covering spec. We expect as a minimum 100% coverage
84
+ measured by RCov. While this is not perfect by any measure, it is pretty
85
+ good. Additionally, all functional tests from TMail are to be passing before
86
+ the gem gets released.
87
+
88
+ It also means you can be sure Mail will behave correctly.
89
+
90
+ API Policy
91
+ ----------
92
+
93
+ No API removals within a single point release. All removals to be depreciated with
94
+ warnings for at least one MINOR point release before removal.
95
+
96
+ Also, all private or protected methods to be declared as such - though this is still I/P.
97
+
98
+ Installation
99
+ ------------
100
+
101
+ Installation is fairly simple, I host mail on rubygems, so you can just do:
102
+
103
+ # gem install mail
104
+
105
+ Encodings
106
+ ---------
107
+
108
+ If you didn't know, handling encodings in Emails is not as straight forward as you
109
+ would hope.
110
+
111
+ I have tried to simplify it some:
112
+
113
+ 1. All objects that can render into an email, have an `#encoded` method. Encoded will
114
+ return the object as a complete string ready to send in the mail system, that is,
115
+ it will include the header field and value and CRLF at the end and wrapped as
116
+ needed.
117
+
118
+ 2. All objects that can render into an email, have a :decoded method. Decoded will
119
+ return the object's "value" only as a string. This means it will not include
120
+ the header fields (like 'To:' or 'Subject:').
121
+
122
+ 3. By default, calling <code>#to_s</code> on a container object will call its encoded
123
+ method, while <code>#to_s</code> on a field object will call it's decoded method.
124
+ So calling <code>#to_s</code> on a Mail object will return the mail, all encoded
125
+ ready to send, while calling <code>#to_s</code> on the From field or the body will
126
+ return the decoded value of the object. The header object of Mail is considered a
127
+ container. If you are in doubt, call <code>#encoded</code>, or <code>#decoded</code>
128
+ explicitly, this is safer if you are not sure.
129
+
130
+ 4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
131
+ provide decoded parameter values when you call the parameter names as methods against
132
+ the object.
133
+
134
+ 5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
135
+ provide encoded parameter values when you call the parameter names through the
136
+ <code>object.parameters['<parameter_name>']</code> method call.
137
+
138
+ Contributing
139
+ ------------
140
+
141
+ Please do! Contributing is easy in Mail. Please read the CONTRIBUTING.md document for more info
142
+
143
+ Usage
144
+ -----
145
+
146
+ All major mail functions should be able to happen from the Mail module.
147
+ So, you should be able to just <code>require 'mail'</code> to get started.
148
+
149
+ ### Making an email
150
+
151
+ ```ruby
152
+ mail = Mail.new do
153
+ from 'mikel@test.lindsaar.net'
154
+ to 'you@test.lindsaar.net'
155
+ subject 'This is a test email'
156
+ body File.read('body.txt')
157
+ end
158
+
159
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
160
+ ```
161
+
162
+ ### Making an email, have it your way:
163
+
164
+ ```ruby
165
+ mail = Mail.new do
166
+ body File.read('body.txt')
167
+ end
168
+
169
+ mail['from'] = 'mikel@test.lindsaar.net'
170
+ mail[:to] = 'you@test.lindsaar.net'
171
+ mail.subject = 'This is a test email'
172
+
173
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
174
+ ```
175
+
176
+ ### Don't Worry About Message IDs:
177
+
178
+ ```ruby
179
+ mail = Mail.new do
180
+ to 'you@test.lindsaar.net'
181
+ body 'Some simple body'
182
+ end
183
+
184
+ mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
185
+ ```
186
+
187
+ Mail will automatically add a Message-ID field if it is missing and
188
+ give it a unique, random Message-ID along the lines of:
189
+
190
+ <4a7ff76d7016_13a81ab802e1@local.fqdn.mail>
191
+
192
+ ### Or do worry about Message-IDs:
193
+
194
+ ```ruby
195
+ mail = Mail.new do
196
+ to 'you@test.lindsaar.net'
197
+ message_id '<ThisIsMyMessageId@some.domain.com>'
198
+ body 'Some simple body'
199
+ end
200
+
201
+ mail.to_s =~ /Message\-ID: <ThisIsMyMessageId@some.domain.com>/ #=> 27
202
+ ```
203
+
204
+ Mail will take the message_id you assign to it trusting that you know
205
+ what you are doing.
206
+
207
+ ### Sending an email:
208
+
209
+ Mail defaults to sending via SMTP to local host port 25. If you have a
210
+ sendmail or postfix daemon running on on this port, sending email is as
211
+ easy as:
212
+
213
+ ```ruby
214
+ Mail.deliver do
215
+ from 'me@test.lindsaar.net'
216
+ to 'you@test.lindsaar.net'
217
+ subject 'Here is the image you wanted'
218
+ body File.read('body.txt')
219
+ add_file '/full/path/to/somefile.png'
220
+ end
221
+ ```
222
+
223
+ or
224
+
225
+ ```ruby
226
+ mail = Mail.new do
227
+ from 'me@test.lindsaar.net'
228
+ to 'you@test.lindsaar.net'
229
+ subject 'Here is the image you wanted'
230
+ body File.read('body.txt')
231
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
232
+ end
233
+
234
+ mail.deliver!
235
+ ```
236
+
237
+ Sending via sendmail can be done like so:
238
+
239
+ ```ruby
240
+ mail = Mail.new do
241
+ from 'me@test.lindsaar.net'
242
+ to 'you@test.lindsaar.net'
243
+ subject 'Here is the image you wanted'
244
+ body File.read('body.txt')
245
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
246
+ end
247
+
248
+ mail.delivery_method :sendmail
249
+
250
+ mail.deliver
251
+ ```
252
+
253
+ Exim requires it's own delivery manager, and can be used like so:
254
+
255
+ ```ruby
256
+ mail.delivery_method :exim, :location => "/usr/bin/exim"
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
+ ### Testing and extracting attachments
341
+ ```ruby
342
+ mail.attachments.each do | attachment |
343
+ # Attachments is an AttachmentsList object containing a
344
+ # number of Part objects
345
+ if (attachment.content_type.start_with?('image/'))
346
+ # extracting images for example...
347
+ filename = attachment.filename
348
+ begin
349
+ File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
350
+ rescue Exception => e
351
+ puts "Unable to save data for #{filename} because #{e.message}"
352
+ end
353
+ end
354
+ end
355
+ ```
356
+ ### Writing and sending a multipart/alternative (html and text) email
357
+
358
+ Mail makes some basic assumptions and makes doing the common thing as
359
+ simple as possible.... (asking a lot from a mail library)
360
+
361
+ ```ruby
362
+ mail = Mail.deliver do
363
+ to 'nicolas@test.lindsaar.net.au'
364
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
365
+ subject 'First multipart email sent with Mail'
366
+
367
+ text_part do
368
+ body 'This is plain text'
369
+ end
370
+
371
+ html_part do
372
+ content_type 'text/html; charset=UTF-8'
373
+ body '<h1>This is HTML</h1>'
374
+ end
375
+ end
376
+ ```
377
+
378
+ Mail then delivers the email at the end of the block and returns the
379
+ resulting Mail::Message object, which you can then inspect if you
380
+ so desire...
381
+
382
+ ```
383
+ puts mail.to_s #=>
384
+
385
+ To: nicolas@test.lindsaar.net.au
386
+ From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
387
+ Subject: First multipart email sent with Mail
388
+ Content-Type: multipart/alternative;
389
+ boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659
390
+ Message-ID: <4a914f12ac7e_6f0f1ab80267d1@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
+
396
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659
397
+ Content-ID: <4a914f12c8c4_6f0f1ab80268d6@baci.local.mail>
398
+ Date: Mon, 24 Aug 2009 00:15:46 +1000
399
+ Mime-Version: 1.0
400
+ Content-Type: text/plain
401
+ Content-Transfer-Encoding: 7bit
402
+
403
+ This is plain text
404
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659
405
+ Content-Type: text/html; charset=UTF-8
406
+ Content-ID: <4a914f12cf86_6f0f1ab802692c@baci.local.mail>
407
+ Date: Mon, 24 Aug 2009 00:15:46 +1000
408
+ Mime-Version: 1.0
409
+ Content-Transfer-Encoding: 7bit
410
+
411
+ <h1>This is HTML</h1>
412
+ ----==_mimepart_4a914f0c911be_6f0f1ab8026659--
413
+ ```
414
+
415
+ Mail inserts the content transfer encoding, the mime version,
416
+ the content-id's and handles the content-type and boundary.
417
+
418
+ Mail assumes that if your text in the body is only us-ascii, that your
419
+ transfer encoding is 7bit and it is text/plain. You can override this
420
+ by explicitly declaring it.
421
+
422
+ ### Making Multipart/Alternate, without a block
423
+
424
+ You don't have to use a block with the text and html part included, you
425
+ can just do it declaratively. However, you need to add Mail::Parts to
426
+ an email, not Mail::Messages.
427
+
428
+ ```ruby
429
+ mail = Mail.new do
430
+ to 'nicolas@test.lindsaar.net.au'
431
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
432
+ subject 'First multipart email sent with Mail'
433
+ end
434
+
435
+ text_part = Mail::Part.new do
436
+ body 'This is plain text'
437
+ end
438
+
439
+ html_part = Mail::Part.new do
440
+ content_type 'text/html; charset=UTF-8'
441
+ body '<h1>This is HTML</h1>'
442
+ end
443
+
444
+ mail.text_part = text_part
445
+ mail.html_part = html_part
446
+ ```
447
+
448
+ Results in the same email as done using the block form
449
+
450
+ ### Getting error reports from an email:
451
+
452
+ ```ruby
453
+ @mail = Mail.read('/path/to/bounce_message.eml')
454
+
455
+ @mail.bounced? #=> true
456
+ @mail.final_recipient #=> rfc822;mikel@dont.exist.com
457
+ @mail.action #=> failed
458
+ @mail.error_status #=> 5.5.0
459
+ @mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
460
+ @mail.retryable? #=> false
461
+ ```
462
+
463
+ ### Attaching and Detaching Files
464
+
465
+ You can just read the file off an absolute path, Mail will try
466
+ to guess the mime_type and will encode the file in Base64 for you.
467
+
468
+ ```ruby
469
+ @mail = Mail.new
470
+ @mail.add_file("/path/to/file.jpg")
471
+ @mail.parts.first.attachment? #=> true
472
+ @mail.parts.first.content_transfer_encoding.to_s #=> 'base64'
473
+ @mail.attachments.first.mime_type #=> 'image/jpg'
474
+ @mail.attachments.first.filename #=> 'file.jpg'
475
+ @mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=> true
476
+ ```
477
+
478
+ Or You can pass in file_data and give it a filename, again, mail
479
+ will try and guess the mime_type for you.
480
+
481
+ ```ruby
482
+ @mail = Mail.new
483
+ @mail.attachments['myfile.pdf'] = File.read('path/to/myfile.pdf')
484
+ @mail.parts.first.attachment? #=> true
485
+ @mail.attachments.first.mime_type #=> 'application/pdf'
486
+ @mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=> true
487
+ ```
488
+
489
+ You can also override the guessed MIME media type if you really know better
490
+ than mail (this should be rarely needed)
491
+
492
+ ```ruby
493
+ @mail = Mail.new
494
+ file_data = File.read('path/to/myfile.pdf')
495
+ @mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
496
+ :content => File.read('path/to/myfile.pdf') }
497
+ @mail.parts.first.mime_type #=> 'application/x-pdf'
498
+ ```
499
+
500
+ Of course... Mail will round trip an attachment as well
501
+
502
+ ```ruby
503
+ @mail = Mail.new do
504
+ to 'nicolas@test.lindsaar.net.au'
505
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
506
+ subject 'First multipart email sent with Mail'
507
+
508
+ text_part do
509
+ body 'Here is the attachment you wanted'
510
+ end
511
+
512
+ html_part do
513
+ content_type 'text/html; charset=UTF-8'
514
+ body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
515
+ end
516
+
517
+ add_file '/path/to/myfile.pdf'
518
+ end
519
+
520
+ @round_tripped_mail = Mail.new(@mail.encoded)
521
+
522
+ @round_tripped_mail.attachments.length #=> 1
523
+ @round_tripped_mail.attachments.first.filename #=> 'myfile.pdf'
524
+ ```
525
+ See "Testing and extracting attachments" above for more details.
526
+
527
+ Using Mail with Testing or Spec'ing Libraries
528
+ ---------------------------------------------
529
+
530
+ If mail is part of your system, you'll need a way to test it without actually
531
+ sending emails, the TestMailer can do this for you.
532
+
533
+ ```
534
+ require 'mail'
535
+ => true
536
+ Mail.defaults do
537
+ delivery_method :test
538
+ end
539
+ => #<Mail::Configuration:0x19345a8 @delivery_method=Mail::TestMailer>
540
+ Mail::TestMailer.deliveries
541
+ => []
542
+ Mail.deliver do
543
+ to 'mikel@me.com'
544
+ from 'you@you.com'
545
+ subject 'testing'
546
+ body 'hello'
547
+ end
548
+ => #<Mail::Message:0x19284ec ...
549
+ Mail::TestMailer.deliveries.length
550
+ => 1
551
+ Mail::TestMailer.deliveries.first
552
+ => #<Mail::Message:0x19284ec ...
553
+ Mail::TestMailer.deliveries.clear
554
+ => []
555
+ ```
556
+
557
+ There is also a set of RSpec matchers stolen fr^H^H^H^H^H^H^H^H inspired by Shoulda's ActionMailer matchers (you'll want to set <code>delivery_method</code> as above too):
558
+
559
+ ```
560
+ Mail.defaults do
561
+ delivery_method :test # in practice you'd do this in spec_helper.rb
562
+ end
563
+
564
+ describe "sending an email" do
565
+ include Mail::Matchers
566
+
567
+ before(:each) do
568
+ Mail::TestMailer.deliveries.clear
569
+
570
+ Mail.deliver do
571
+ to ['mikel@me.com', 'mike2@me.com']
572
+ from 'you@you.com'
573
+ subject 'testing'
574
+ body 'hello'
575
+ end
576
+ end
577
+
578
+ it { should have_sent_email } # passes if any email at all was sent
579
+
580
+ it { should have_sent_email.from('you@you.com') }
581
+ it { should have_sent_email.to('mike1@me.com') }
582
+
583
+ # can specify a list of recipients...
584
+ it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
585
+
586
+ # ...or chain recipients together
587
+ it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
588
+
589
+ it { should have_sent_email.with_subject('testing') }
590
+
591
+ it { should have_sent_email.with_body('hello') }
592
+
593
+ # Can match subject or body with a regex
594
+ # (or anything that responds_to? :match)
595
+
596
+ it { should have_sent_email.matching_subject(/test(ing)?/) }
597
+ it { should have_sent_email.matching_body(/h(a|e)llo/) }
598
+
599
+ # Can chain together modifiers
600
+ # Note that apart from recipients, repeating a modifier overwrites old value.
601
+
602
+ it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
603
+ end
604
+ ```
605
+
606
+ Excerpts from TREC Spam Corpus 2005
607
+ -----------------------------------
608
+
609
+ The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
610
+ 2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
611
+ that project and license agreement. They are used in this project to verify
612
+ and describe the development of this email parser implementation.
613
+
614
+ http://plg.uwaterloo.ca/~gvcormac/treccorpus/
615
+
616
+ They are used as allowed by 'Permitted Uses, Clause 3':
617
+
618
+ "Small excerpts of the information may be displayed to others
619
+ or published in a scientific or technical context, solely for
620
+ the purpose of describing the research and development and
621
+ related issues."
622
+
623
+ -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
624
+
625
+ License
626
+ -------
627
+
628
+ (The MIT License)
629
+
630
+ Copyright (c) 2009, 2010, 2011, 2012
631
+
632
+ Permission is hereby granted, free of charge, to any person obtaining
633
+ a copy of this software and associated documentation files (the
634
+ 'Software'), to deal in the Software without restriction, including
635
+ without limitation the rights to use, copy, modify, merge, publish,
636
+ distribute, sublicense, and/or sell copies of the Software, and to
637
+ permit persons to whom the Software is furnished to do so, subject to
638
+ the following conditions:
639
+
640
+ The above copyright notice and this permission notice shall be
641
+ included in all copies or substantial portions of the Software.
642
+
643
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
644
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
645
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
646
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
647
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
648
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
649
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.