mail 2.7.0.rc1 → 2.7.0.rc2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -98
  3. data/lib/mail.rb +1 -6
  4. data/lib/mail/attachments_list.rb +5 -2
  5. data/lib/mail/body.rb +39 -33
  6. data/lib/mail/check_delivery_params.rb +8 -6
  7. data/lib/mail/configuration.rb +2 -0
  8. data/lib/mail/elements/address.rb +19 -18
  9. data/lib/mail/encodings.rb +89 -31
  10. data/lib/mail/encodings/7bit.rb +5 -15
  11. data/lib/mail/encodings/8bit.rb +2 -21
  12. data/lib/mail/encodings/base64.rb +11 -12
  13. data/lib/mail/encodings/binary.rb +3 -22
  14. data/lib/mail/encodings/identity.rb +24 -0
  15. data/lib/mail/encodings/quoted_printable.rb +6 -6
  16. data/lib/mail/encodings/transfer_encoding.rb +38 -29
  17. data/lib/mail/encodings/unix_to_unix.rb +2 -1
  18. data/lib/mail/envelope.rb +1 -1
  19. data/lib/mail/field.rb +93 -61
  20. data/lib/mail/fields/bcc_field.rb +2 -2
  21. data/lib/mail/fields/cc_field.rb +1 -1
  22. data/lib/mail/fields/comments_field.rb +1 -1
  23. data/lib/mail/fields/common/common_address.rb +32 -7
  24. data/lib/mail/fields/common/common_field.rb +1 -10
  25. data/lib/mail/fields/content_description_field.rb +1 -1
  26. data/lib/mail/fields/content_disposition_field.rb +3 -3
  27. data/lib/mail/fields/content_id_field.rb +2 -2
  28. data/lib/mail/fields/content_location_field.rb +1 -1
  29. data/lib/mail/fields/content_transfer_encoding_field.rb +1 -1
  30. data/lib/mail/fields/content_type_field.rb +1 -1
  31. data/lib/mail/fields/date_field.rb +2 -3
  32. data/lib/mail/fields/from_field.rb +1 -1
  33. data/lib/mail/fields/in_reply_to_field.rb +1 -1
  34. data/lib/mail/fields/keywords_field.rb +1 -1
  35. data/lib/mail/fields/message_id_field.rb +1 -1
  36. data/lib/mail/fields/mime_version_field.rb +1 -1
  37. data/lib/mail/fields/optional_field.rb +4 -1
  38. data/lib/mail/fields/received_field.rb +1 -1
  39. data/lib/mail/fields/references_field.rb +1 -1
  40. data/lib/mail/fields/reply_to_field.rb +1 -1
  41. data/lib/mail/fields/resent_bcc_field.rb +1 -1
  42. data/lib/mail/fields/resent_cc_field.rb +1 -1
  43. data/lib/mail/fields/resent_date_field.rb +0 -1
  44. data/lib/mail/fields/resent_from_field.rb +1 -1
  45. data/lib/mail/fields/resent_message_id_field.rb +1 -1
  46. data/lib/mail/fields/resent_sender_field.rb +1 -1
  47. data/lib/mail/fields/resent_to_field.rb +1 -1
  48. data/lib/mail/fields/return_path_field.rb +1 -1
  49. data/lib/mail/fields/sender_field.rb +1 -1
  50. data/lib/mail/fields/subject_field.rb +1 -1
  51. data/lib/mail/fields/to_field.rb +1 -1
  52. data/lib/mail/fields/unstructured_field.rb +19 -2
  53. data/lib/mail/header.rb +9 -8
  54. data/lib/mail/mail.rb +2 -10
  55. data/lib/mail/matchers/has_sent_mail.rb +21 -1
  56. data/lib/mail/message.rb +64 -51
  57. data/lib/mail/multibyte.rb +14 -16
  58. data/lib/mail/multibyte/chars.rb +2 -1
  59. data/lib/mail/network.rb +1 -0
  60. data/lib/mail/network/delivery_methods/exim.rb +6 -10
  61. data/lib/mail/network/delivery_methods/logger_delivery.rb +37 -0
  62. data/lib/mail/network/delivery_methods/sendmail.rb +8 -4
  63. data/lib/mail/network/delivery_methods/smtp.rb +56 -55
  64. data/lib/mail/network/delivery_methods/smtp_connection.rb +9 -1
  65. data/lib/mail/network/retriever_methods/imap.rb +18 -5
  66. data/lib/mail/network/retriever_methods/pop3.rb +3 -1
  67. data/lib/mail/parser_tools.rb +15 -0
  68. data/lib/mail/parsers/address_lists_parser.rb +30462 -12597
  69. data/lib/mail/parsers/address_lists_parser.rl +18 -12
  70. data/lib/mail/parsers/content_disposition_parser.rb +405 -215
  71. data/lib/mail/parsers/content_disposition_parser.rl +11 -5
  72. data/lib/mail/parsers/content_location_parser.rb +443 -208
  73. data/lib/mail/parsers/content_location_parser.rl +9 -3
  74. data/lib/mail/parsers/content_transfer_encoding_parser.rb +180 -80
  75. data/lib/mail/parsers/content_transfer_encoding_parser.rl +8 -2
  76. data/lib/mail/parsers/content_type_parser.rb +436 -245
  77. data/lib/mail/parsers/content_type_parser.rl +12 -6
  78. data/lib/mail/parsers/date_time_parser.rb +172 -72
  79. data/lib/mail/parsers/date_time_parser.rl +10 -4
  80. data/lib/mail/parsers/envelope_from_parser.rb +2833 -1320
  81. data/lib/mail/parsers/envelope_from_parser.rl +9 -3
  82. data/lib/mail/parsers/message_ids_parser.rb +2325 -976
  83. data/lib/mail/parsers/message_ids_parser.rl +9 -3
  84. data/lib/mail/parsers/mime_version_parser.rb +164 -64
  85. data/lib/mail/parsers/mime_version_parser.rl +9 -3
  86. data/lib/mail/parsers/phrase_lists_parser.rb +582 -237
  87. data/lib/mail/parsers/phrase_lists_parser.rl +9 -3
  88. data/lib/mail/parsers/received_parser.rb +7036 -3004
  89. data/lib/mail/parsers/received_parser.rl +11 -5
  90. data/lib/mail/parsers/rfc2045_content_transfer_encoding.rl +1 -0
  91. data/lib/mail/parsers/rfc2045_content_type.rl +1 -0
  92. data/lib/mail/parsers/rfc2045_mime.rl +1 -0
  93. data/lib/mail/parsers/rfc2183_content_disposition.rl +1 -0
  94. data/lib/mail/parsers/rfc3629_utf8.rl +19 -0
  95. data/lib/mail/parsers/rfc5234_abnf_core_rules.rl +7 -1
  96. data/lib/mail/parsers/rfc5322.rl +3 -1
  97. data/lib/mail/parsers/rfc5322_address.rl +3 -1
  98. data/lib/mail/parsers/rfc5322_date_time.rl +1 -0
  99. data/lib/mail/parsers/rfc5322_lexical_tokens.rl +9 -5
  100. data/lib/mail/part.rb +1 -1
  101. data/lib/mail/utilities.rb +44 -15
  102. data/lib/mail/version.rb +1 -1
  103. data/lib/mail/version_specific/ruby_1_8.rb +12 -1
  104. data/lib/mail/version_specific/ruby_1_9.rb +13 -1
  105. metadata +7 -13
  106. data/CHANGELOG.rdoc +0 -822
  107. data/CONTRIBUTING.md +0 -60
  108. data/Dependencies.txt +0 -1
  109. data/Gemfile +0 -11
  110. data/Rakefile +0 -23
  111. data/TODO.rdoc +0 -9
  112. data/lib/mail/multibyte/exceptions.rb +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93c47855c2c3b7c479ab1b3bfc7814fec87708c4
4
- data.tar.gz: 7fd451474a8d296b33732d0db9184945a31d4061
3
+ metadata.gz: f88c2fe5a5eee5c8e1eafb55dc781c70b41c8e40
4
+ data.tar.gz: ddbda463854308dffa531f1b50243ff6e58518ea
5
5
  SHA512:
6
- metadata.gz: 505cbad915ae907563530b378162b0850510f973f4ec7a5d9867ecf582900fb45dfa725c8682f2b9e1848a9cdb3497a1c313178f2fdb81c1909f63adc24bf736
7
- data.tar.gz: 0ad2330236098fa81cac6675c17144f6b072b43d8908e57d884f833472817511ec4e7764e9bc5c7aac490e2a76680ff0c6b1bbdf95a6ec478d319b16b76686ba
6
+ metadata.gz: f5d80a2185f9204421b432587024c3745758d51864b6462157d8c4ce538e7d18a92ae0d86e52033133e1a1367f2ea64488c11d448c8544d724dbac43e74e0072
7
+ data.tar.gz: de7db5c6ea57774e63e38008b8ff6977d3a24ca6fe611a8b019ab1d8e45ff66dd345846cdca761da6a26e998773e11c2270598033561496d148243a77efcc8d7
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
- Mail [![Build Status](https://travis-ci.org/mikel/mail.png?branch=master)](https://travis-ci.org/mikel/mail)
2
- ====
1
+ # Mail [![Build Status](https://travis-ci.org/mikel/mail.png?branch=master)](https://travis-ci.org/mikel/mail)
3
2
 
4
- Introduction
5
- ------------
3
+ ## Introduction
6
4
 
7
5
  Mail is an internet library for Ruby that is designed to handle emails
8
6
  generation, parsing and sending in a simple, rubyesque manner.
@@ -25,8 +23,7 @@ Finally, Mail has been designed with a very simple object oriented system
25
23
  that really opens up the email messages you are parsing, if you know what
26
24
  you are doing, you can fiddle with every last bit of your email directly.
27
25
 
28
- Donations
29
- -------------
26
+ ## Donations
30
27
 
31
28
  Mail has been downloaded millions of times, by people around the world, in fact,
32
29
  it represents more than 1% of *all* gems downloaded.
@@ -38,56 +35,48 @@ me a nice email :)
38
35
 
39
36
  <a href='http://www.pledgie.com/campaigns/8790'><img alt='Click here to lend your support to: mail and make a donation at www.pledgie.com !' src='http://www.pledgie.com/campaigns/8790.png?skin_name=chrome' border='0' /></a>
40
37
 
38
+ # Contents
39
+ * [Compatibility](#compatibility)
40
+ * [Discussion](#discussion)
41
+ * [Current Capabilities of Mail](#current-capabilities-of-mail)
42
+ * [Roadmap](#roadmap)
43
+ * [Testing Policy](#testing-policy)
44
+ * [API Policy](#api-policy)
45
+ * [Installation](#installation)
46
+ * [Encodings](#encodings)
47
+ * [Contributing](#contributing)
48
+ * [Usage](#usage)
49
+ * [Core Extensions](#core-extensions)
50
+ * [Excerpts from TREC Span Corpus 2005](#excerpts-from-trec-span-corpus-2005)
51
+ * [License](#license)
41
52
 
42
- Compatibility
43
- -------------
53
+ ## Compatibility
44
54
 
45
- Every Mail commit is tested by Travis on the [following platforms](https://github.com/mikel/mail/blob/master/.travis.yml)
55
+ Mail supports Ruby 1.8.7+, including JRuby and Rubinius.
46
56
 
47
- * ruby-1.8.7 [ i686 ]
48
- * ruby-1.9.2 [ x86_64 ]
49
- * ruby-1.9.3 [ x86_64 ]
50
- * ruby-2.0.0 [ x86_64 ]
51
- * ruby-2.1.10 [ x86_64 ]
52
- * ruby-2.2.6 [ x86_64 ]
53
- * ruby-2.3.3 [ x86_64 ]
54
- * ruby-2.4.0 [ x86_64 ]
55
- * ruby-head [ x86_64 ]
56
- * jruby [ x86_64 ]
57
- * jruby-9.1.6.0 [ x86_64 ]
58
- * jruby-head [ x86_64 ]
59
- * rbx-2 [ x86_64 ]
57
+ Every Mail commit is tested by Travis on [all supported Ruby versions](https://github.com/mikel/mail/blob/master/.travis.yml).
60
58
 
61
- Testing a specific mime type (needed for 1.8.7 for example) can be done manually with:
62
-
63
- ```sh
64
- BUNDLE_GEMFILE=gemfiles/mime_types_1.16.gemfile (bundle check || bundle) && rake
65
- ```
66
-
67
- Discussion
68
- ----------
59
+ ## Discussion
69
60
 
70
61
  If you want to discuss mail with like minded individuals, please subscribe to
71
62
  the [Google Group](http://groups.google.com/group/mail-ruby).
72
63
 
73
- Current Capabilities of Mail
74
- ----------------------------
64
+ ## Current Capabilities of Mail
75
65
 
76
- * RFC2822 Support, Reading and Writing
66
+ * RFC5322 Support, Reading and Writing
67
+ * RFC6532 Support, reading UTF-8 headers
77
68
  * RFC2045-2049 Support for multipart emails
78
69
  * Support for creating multipart alternate emails
79
70
  * Support for reading multipart/report emails &amp; getting details from such
80
- * Support for multibyte emails - needs quite a lot of work and testing
81
71
  * Wrappers for File, Net/POP3, Net/SMTP
82
- * Auto encoding of non US-ASCII header fields
83
- * Auto encoding of non US-ASCII bodies
72
+ * Auto-encoding of non-US-ASCII bodies and header fields
84
73
 
85
- Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
86
- emails. There are a few obsoleted syntax emails that it will have problems with, but
87
- it also is quite robust, meaning, if it finds something it doesn't understand it will
88
- not crash, instead, it will skip the problem and keep parsing. In the case of a header
89
- it doesn't understand, it will initialise the header as an optional unstructured
90
- field and continue parsing.
74
+ Mail is RFC5322 and RFC6532 compliant now, that is, it can parse US-ASCII and UTF-8
75
+ emails and generate US-ASCII emails. There are a few obsoleted syntax emails that
76
+ it will have problems with, but it also is quite robust, meaning, if it finds something
77
+ it doesn't understand it will not crash, instead, it will skip the problem and keep
78
+ parsing. In the case of a header it doesn't understand, it will initialise the header
79
+ as an optional unstructured field and continue parsing.
91
80
 
92
81
  This means Mail won't (ever) crunch your data (I think).
93
82
 
@@ -95,16 +84,14 @@ You can also create MIME emails. There are helper methods for making a
95
84
  multipart/alternate email for text/plain and text/html (the most common pair)
96
85
  and you can manually create any other type of MIME email.
97
86
 
98
- Roadmap
99
- -------
87
+ ## Roadmap
100
88
 
101
89
  Next TODO:
102
90
 
103
91
  * Improve MIME support for character sets in headers, currently works, mostly, needs
104
92
  refinement.
105
93
 
106
- Testing Policy
107
- --------------
94
+ ## Testing Policy
108
95
 
109
96
  Basically... we do BDD on Mail. No method gets written in Mail without a
110
97
  corresponding or covering spec. We expect as a minimum 100% coverage
@@ -116,23 +103,20 @@ It also means you can be sure Mail will behave correctly.
116
103
 
117
104
  Note: If you care about core extensions (aka "monkey-patching"), please read the Core Extensions section near the end of this README.
118
105
 
119
- API Policy
120
- ----------
106
+ ## API Policy
121
107
 
122
108
  No API removals within a single point release. All removals to be deprecated with
123
109
  warnings for at least one MINOR point release before removal.
124
110
 
125
111
  Also, all private or protected methods to be declared as such - though this is still I/P.
126
112
 
127
- Installation
128
- ------------
113
+ ## Installation
129
114
 
130
115
  Installation is fairly simple, I host mail on rubygems, so you can just do:
131
116
 
132
117
  # gem install mail
133
118
 
134
- Encodings
135
- ---------
119
+ ## Encodings
136
120
 
137
121
  If you didn't know, handling encodings in Emails is not as straight forward as you
138
122
  would hope.
@@ -164,13 +148,11 @@ I have tried to simplify it some:
164
148
  provide encoded parameter values when you call the parameter names through the
165
149
  <code>object.parameters['<parameter_name>']</code> method call.
166
150
 
167
- Contributing
168
- ------------
151
+ ## Contributing
169
152
 
170
153
  Please do! Contributing is easy in Mail. Please read the CONTRIBUTING.md document for more info
171
154
 
172
- Usage
173
- -----
155
+ ## Usage
174
156
 
175
157
  All major mail functions should be able to happen from the Mail module.
176
158
  So, you should be able to just <code>require 'mail'</code> to get started.
@@ -298,7 +280,17 @@ mail.delivery_method :exim, :location => "/usr/bin/exim"
298
280
  mail.deliver
299
281
  ```
300
282
 
301
- ### Getting emails from a pop server:
283
+ Mail may be "delivered" to a logfile, too, for development and testing:
284
+
285
+ ```ruby
286
+ # Delivers by logging the encoded message to $stdout
287
+ mail.delivery_method :logger
288
+
289
+ # Delivers to an existing logger at :debug severity
290
+ mail.delivery_method :logger, logger: other_logger, severity: :debug
291
+ ```
292
+
293
+ ### Getting Emails from a POP Server:
302
294
 
303
295
  You can configure Mail to receive email using <code>retriever_method</code>
304
296
  within <code>Mail.defaults</code>:
@@ -377,7 +369,7 @@ is another message which can have many or no parts.
377
369
  A message will only have parts if it is a multipart/mixed or multipart/related
378
370
  content type and has a boundary defined.
379
371
 
380
- ### Testing and extracting attachments
372
+ ### Testing and Extracting Attachments
381
373
  ```ruby
382
374
  mail.attachments.each do | attachment |
383
375
  # Attachments is an AttachmentsList object containing a
@@ -393,7 +385,7 @@ mail.attachments.each do | attachment |
393
385
  end
394
386
  end
395
387
  ```
396
- ### Writing and sending a multipart/alternative (html and text) email
388
+ ### Writing and Sending a Multipart/Alternative (HTML and Text) Email
397
389
 
398
390
  Mail makes some basic assumptions and makes doing the common thing as
399
391
  simple as possible.... (asking a lot from a mail library)
@@ -459,7 +451,7 @@ Mail assumes that if your text in the body is only us-ascii, that your
459
451
  transfer encoding is 7bit and it is text/plain. You can override this
460
452
  by explicitly declaring it.
461
453
 
462
- ### Making Multipart/Alternate, without a block
454
+ ### Making Multipart/Alternate, Without a Block
463
455
 
464
456
  You don't have to use a block with the text and html part included, you
465
457
  can just do it declaratively. However, you need to add Mail::Parts to
@@ -487,7 +479,7 @@ mail.html_part = html_part
487
479
 
488
480
  Results in the same email as done using the block form
489
481
 
490
- ### Getting error reports from an email:
482
+ ### Getting Error Reports from an Email:
491
483
 
492
484
  ```ruby
493
485
  @mail = Mail.read('/path/to/bounce_message.eml')
@@ -563,8 +555,7 @@ end
563
555
  ```
564
556
  See "Testing and extracting attachments" above for more details.
565
557
 
566
- Using Mail with Testing or Spec'ing Libraries
567
- ---------------------------------------------
558
+ ## Using Mail with Testing or Spec'ing Libraries
568
559
 
569
560
  If mail is part of your system, you'll need a way to test it without actually
570
561
  sending emails, the TestMailer can do this for you.
@@ -614,70 +605,53 @@ describe "sending an email" do
614
605
  end
615
606
  end
616
607
 
617
- it { should have_sent_email } # passes if any email at all was sent
608
+ it { is_expected.to have_sent_email } # passes if any email at all was sent
618
609
 
619
- it { should have_sent_email.from('you@you.com') }
620
- it { should have_sent_email.to('mike1@me.com') }
610
+ it { is_expected.to have_sent_email.from('you@you.com') }
611
+ it { is_expected.to have_sent_email.to('mike1@me.com') }
621
612
 
622
613
  # can specify a list of recipients...
623
- it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
614
+ it { is_expected.to have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
624
615
 
625
616
  # ...or chain recipients together
626
- it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
617
+ it { is_expected.to have_sent_email.to('mike1@me.com').to('mike2@me.com') }
627
618
 
628
- it { should have_sent_email.with_subject('testing') }
619
+ it { is_expected.to have_sent_email.with_subject('testing') }
629
620
 
630
- it { should have_sent_email.with_body('hello') }
621
+ it { is_expected.to have_sent_email.with_body('hello') }
631
622
 
632
623
  # Can match subject or body with a regex
633
624
  # (or anything that responds_to? :match)
634
625
 
635
- it { should have_sent_email.matching_subject(/test(ing)?/) }
636
- it { should have_sent_email.matching_body(/h(a|e)llo/) }
626
+ it { is_expected.to have_sent_email.matching_subject(/test(ing)?/) }
627
+ it { is_expected.to have_sent_email.matching_body(/h(a|e)llo/) }
637
628
 
638
629
  # Can chain together modifiers
639
630
  # Note that apart from recipients, repeating a modifier overwrites old value.
640
631
 
641
- it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
632
+ it { is_expected.to have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
642
633
 
643
634
  # test for attachments
644
635
 
645
636
  # ... by specific attachment
646
- it { should_have_sent_email.with_attachments(my_attachment) }
637
+ it { is_expected.to have_sent_email.with_attachments(my_attachment) }
647
638
 
648
639
  # ... or any attachment
649
- it { should_have_sent_email.with_attachments(any_attachment) }
640
+ it { is_expected.to have_sent_email.with_attachments(any_attachment) }
650
641
 
651
642
  # ... by array of attachments
652
- it { should_have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
643
+ it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
653
644
 
654
645
  #... by presence
655
- it { should_have_sent_email.with_any_attachments }
646
+ it { is_expected.to have_sent_email.with_any_attachments }
656
647
 
657
648
  #... or by absence
658
- it { should_have_sent_email.with_no_attachments }
649
+ it { is_expected.to have_sent_email.with_no_attachments }
659
650
 
660
651
  end
661
652
  ```
662
653
 
663
- Core Extensions
664
- ---------------
665
-
666
- The mail gem adds several constants and methods to Ruby's core objects (similar to the activesupport gem from the Rails project). For example:
667
-
668
- NilClass::blank?
669
- NilClass::to_crlf
670
- NilClass::to_lf
671
- Object::blank?
672
- String::to_crlf
673
- String::to_lf
674
- String::blank?
675
- ...etc...
676
-
677
- For all the details, check out lib/mail/core_extensions/.
678
-
679
- Excerpts from TREC Spam Corpus 2005
680
- -----------------------------------
654
+ ## Excerpts from TREC Spam Corpus 2005
681
655
 
682
656
  The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
683
657
  2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
@@ -695,8 +669,7 @@ They are used as allowed by 'Permitted Uses, Clause 3':
695
669
 
696
670
  -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
697
671
 
698
- License
699
- -------
672
+ ## License
700
673
 
701
674
  (The MIT License)
702
675
 
@@ -31,12 +31,7 @@ module Mail # :doc:
31
31
  require 'mail/core_extensions/smtp'
32
32
  require 'mail/indifferent_hash'
33
33
 
34
- # Only load our multibyte extensions if AS is not already loaded
35
- if defined?(ActiveSupport)
36
- require 'active_support/inflector'
37
- else
38
- require 'mail/multibyte'
39
- end
34
+ require 'mail/multibyte'
40
35
 
41
36
  require 'mail/constants'
42
37
  require 'mail/utilities'
@@ -6,8 +6,8 @@ module Mail
6
6
  @parts_list = parts_list
7
7
  @content_disposition_type = 'attachment'
8
8
  parts_list.map { |p|
9
- if p.content_type == "message/rfc822"
10
- Mail.new(p.body).attachments
9
+ if p.mime_type == 'message/rfc822'
10
+ Mail.new(p.body.encoded).attachments
11
11
  elsif p.parts.empty?
12
12
  p if p.attachment?
13
13
  else
@@ -44,6 +44,9 @@ module Mail
44
44
  :content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" }
45
45
 
46
46
  if value.is_a?(Hash)
47
+ if path = value.delete(:filename)
48
+ value[:content] ||= File.open(path, 'rb') { |f| f.read }
49
+ end
47
50
 
48
51
  default_values[:body] = value.delete(:content) if value[:content]
49
52
 
@@ -46,10 +46,10 @@ module Mail
46
46
  raise "You can only assign a string or an object that responds_to? :join or :to_s to a body."
47
47
  end
48
48
  end
49
- @encoding = (only_us_ascii? ? '7bit' : '8bit')
49
+ @encoding = default_encoding
50
50
  set_charset
51
51
  end
52
-
52
+
53
53
  # Matches this body with another body. Also matches the decoded value of this
54
54
  # body with a string.
55
55
  #
@@ -138,41 +138,39 @@ module Mail
138
138
  def raw_source
139
139
  @raw_source
140
140
  end
141
-
142
- def get_best_encoding(target)
143
- target_encoding = Mail::Encodings.get_encoding(target)
144
- target_encoding.get_best_compatible(encoding, raw_source)
141
+
142
+ def negotiate_best_encoding(message_encoding, allowed_encodings = nil)
143
+ Mail::Encodings::TransferEncoding.negotiate(message_encoding, encoding, raw_source, allowed_encodings)
145
144
  end
146
-
145
+
147
146
  # Returns a body encoded using transfer_encoding. Multipart always uses an
148
147
  # identiy encoding (i.e. no encoding).
149
148
  # Calling this directly is not a good idea, but supported for compatibility
150
149
  # TODO: Validate that preamble and epilogue are valid for requested encoding
151
- def encoded(transfer_encoding = '8bit')
150
+ def encoded(transfer_encoding = nil)
152
151
  if multipart?
153
152
  self.sort_parts!
154
153
  encoded_parts = parts.map { |p| p.encoded }
155
154
  ([preamble] + encoded_parts).join(crlf_boundary) + end_boundary + epilogue.to_s
156
155
  else
157
- be = get_best_encoding(transfer_encoding)
158
- dec = Mail::Encodings::get_encoding(encoding)
159
- enc = Mail::Encodings::get_encoding(be)
156
+ dec = Mail::Encodings.get_encoding(encoding)
157
+ enc = negotiate_best_encoding(transfer_encoding)
160
158
  if dec.nil?
161
- # Cannot decode, so skip normalization
162
- raw_source
159
+ # Cannot decode, so skip normalization
160
+ raw_source
163
161
  else
164
- # Decode then encode to normalize and allow transforming
165
- # from base64 to Q-P and vice versa
166
- decoded = dec.decode(raw_source)
167
- if defined?(Encoding) && charset && charset != "US-ASCII"
168
- decoded.encode!(charset)
169
- decoded.force_encoding('BINARY') unless Encoding.find(charset).ascii_compatible?
170
- end
171
- enc.encode(decoded)
162
+ # Decode then encode to normalize and allow transforming
163
+ # from base64 to Q-P and vice versa
164
+ decoded = dec.decode(raw_source)
165
+ if defined?(Encoding) && charset && charset != "US-ASCII"
166
+ decoded = decoded.encode(charset)
167
+ decoded.force_encoding('BINARY') unless Encoding.find(charset).ascii_compatible?
168
+ end
169
+ enc.encode(decoded)
172
170
  end
173
171
  end
174
172
  end
175
-
173
+
176
174
  def decoded
177
175
  if !Encodings.defined?(encoding)
178
176
  raise UnknownEncodingType, "Don't know how to decode #{encoding}, please call #encoded and decode it yourself."
@@ -200,13 +198,14 @@ module Mail
200
198
  @encoding
201
199
  end
202
200
  end
203
-
201
+
204
202
  def encoding=( val )
205
- @encoding = if val == "text" || Utilities.blank?(val)
206
- (only_us_ascii? ? '7bit' : '8bit')
207
- else
203
+ @encoding =
204
+ if val == "text" || Utilities.blank?(val)
205
+ default_encoding
206
+ else
208
207
  val
209
- end
208
+ end
210
209
  end
211
210
 
212
211
  # Returns the preamble (any text that is before the first MIME boundary)
@@ -268,14 +267,21 @@ module Mail
268
267
  self
269
268
  end
270
269
 
271
- def only_us_ascii?
272
- !(raw_source =~ /[^\x01-\x7f]/)
270
+ def ascii_only?
271
+ unless defined? @ascii_only
272
+ @ascii_only = raw_source.ascii_only?
273
+ end
274
+ @ascii_only
273
275
  end
274
-
276
+
275
277
  def empty?
276
278
  !!raw_source.to_s.empty?
277
279
  end
278
-
280
+
281
+ def default_encoding
282
+ ascii_only? ? '7bit' : '8bit'
283
+ end
284
+
279
285
  private
280
286
 
281
287
  # split parts by boundary, ignore first part if empty, append final part when closing boundary was missing
@@ -308,9 +314,9 @@ module Mail
308
314
  def end_boundary
309
315
  "\r\n--#{boundary}--\r\n"
310
316
  end
311
-
317
+
312
318
  def set_charset
313
- only_us_ascii? ? @charset = 'US-ASCII' : @charset = nil
319
+ @charset = ascii_only? ? 'US-ASCII' : nil
314
320
  end
315
321
  end
316
322
  end