mail 2.6.4 → 2.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +208 -156
  3. data/lib/mail/attachments_list.rb +13 -10
  4. data/lib/mail/body.rb +96 -107
  5. data/lib/mail/configuration.rb +2 -0
  6. data/lib/mail/constants.rb +27 -5
  7. data/lib/mail/elements/address.rb +61 -50
  8. data/lib/mail/elements/address_list.rb +11 -19
  9. data/lib/mail/elements/content_disposition_element.rb +9 -16
  10. data/lib/mail/elements/content_location_element.rb +6 -11
  11. data/lib/mail/elements/content_transfer_encoding_element.rb +6 -11
  12. data/lib/mail/elements/content_type_element.rb +16 -23
  13. data/lib/mail/elements/date_time_element.rb +7 -15
  14. data/lib/mail/elements/envelope_from_element.rb +22 -23
  15. data/lib/mail/elements/message_ids_element.rb +18 -13
  16. data/lib/mail/elements/mime_version_element.rb +7 -15
  17. data/lib/mail/elements/phrase_list.rb +12 -10
  18. data/lib/mail/elements/received_element.rb +27 -19
  19. data/lib/mail/encodings/7bit.rb +9 -14
  20. data/lib/mail/encodings/8bit.rb +2 -21
  21. data/lib/mail/encodings/base64.rb +11 -12
  22. data/lib/mail/encodings/binary.rb +3 -22
  23. data/lib/mail/encodings/identity.rb +24 -0
  24. data/lib/mail/encodings/quoted_printable.rb +6 -6
  25. data/lib/mail/encodings/transfer_encoding.rb +38 -29
  26. data/lib/mail/encodings/unix_to_unix.rb +4 -2
  27. data/lib/mail/encodings.rb +83 -56
  28. data/lib/mail/envelope.rb +11 -14
  29. data/lib/mail/field.rb +181 -130
  30. data/lib/mail/field_list.rb +61 -8
  31. data/lib/mail/fields/bcc_field.rb +33 -52
  32. data/lib/mail/fields/cc_field.rb +27 -49
  33. data/lib/mail/fields/comments_field.rb +26 -37
  34. data/lib/mail/fields/common_address_field.rb +162 -0
  35. data/lib/mail/fields/common_date_field.rb +56 -0
  36. data/lib/mail/fields/common_field.rb +77 -0
  37. data/lib/mail/fields/common_message_id_field.rb +41 -0
  38. data/lib/mail/fields/content_description_field.rb +6 -14
  39. data/lib/mail/fields/content_disposition_field.rb +11 -38
  40. data/lib/mail/fields/content_id_field.rb +23 -51
  41. data/lib/mail/fields/content_location_field.rb +10 -25
  42. data/lib/mail/fields/content_transfer_encoding_field.rb +30 -31
  43. data/lib/mail/fields/content_type_field.rb +53 -84
  44. data/lib/mail/fields/date_field.rb +22 -52
  45. data/lib/mail/fields/from_field.rb +27 -49
  46. data/lib/mail/fields/in_reply_to_field.rb +37 -49
  47. data/lib/mail/fields/keywords_field.rb +17 -31
  48. data/lib/mail/fields/message_id_field.rb +24 -71
  49. data/lib/mail/fields/mime_version_field.rb +18 -30
  50. data/lib/mail/fields/named_structured_field.rb +10 -0
  51. data/lib/mail/fields/named_unstructured_field.rb +10 -0
  52. data/lib/mail/fields/optional_field.rb +9 -8
  53. data/lib/mail/fields/{common/parameter_hash.rb → parameter_hash.rb} +13 -11
  54. data/lib/mail/fields/received_field.rb +42 -57
  55. data/lib/mail/fields/references_field.rb +34 -49
  56. data/lib/mail/fields/reply_to_field.rb +27 -49
  57. data/lib/mail/fields/resent_bcc_field.rb +27 -49
  58. data/lib/mail/fields/resent_cc_field.rb +27 -49
  59. data/lib/mail/fields/resent_date_field.rb +4 -30
  60. data/lib/mail/fields/resent_from_field.rb +27 -49
  61. data/lib/mail/fields/resent_message_id_field.rb +4 -29
  62. data/lib/mail/fields/resent_sender_field.rb +26 -56
  63. data/lib/mail/fields/resent_to_field.rb +27 -49
  64. data/lib/mail/fields/return_path_field.rb +49 -54
  65. data/lib/mail/fields/sender_field.rb +33 -55
  66. data/lib/mail/fields/structured_field.rb +2 -30
  67. data/lib/mail/fields/subject_field.rb +8 -11
  68. data/lib/mail/fields/to_field.rb +27 -49
  69. data/lib/mail/fields/unstructured_field.rb +31 -47
  70. data/lib/mail/fields.rb +9 -0
  71. data/lib/mail/header.rb +71 -110
  72. data/lib/mail/mail.rb +34 -37
  73. data/lib/mail/matchers/attachment_matchers.rb +15 -0
  74. data/lib/mail/matchers/has_sent_mail.rb +21 -1
  75. data/lib/mail/message.rb +126 -127
  76. data/lib/mail/multibyte/chars.rb +24 -181
  77. data/lib/mail/multibyte/unicode.rb +11 -11
  78. data/lib/mail/multibyte/utils.rb +26 -43
  79. data/lib/mail/multibyte.rb +55 -16
  80. data/lib/mail/network/delivery_methods/exim.rb +8 -11
  81. data/lib/mail/network/delivery_methods/file_delivery.rb +15 -18
  82. data/lib/mail/network/delivery_methods/logger_delivery.rb +34 -0
  83. data/lib/mail/network/delivery_methods/sendmail.rb +32 -35
  84. data/lib/mail/network/delivery_methods/smtp.rb +125 -68
  85. data/lib/mail/network/delivery_methods/smtp_connection.rb +11 -16
  86. data/lib/mail/network/delivery_methods/test_mailer.rb +12 -13
  87. data/lib/mail/network/retriever_methods/base.rb +13 -13
  88. data/lib/mail/network/retriever_methods/imap.rb +25 -9
  89. data/lib/mail/network/retriever_methods/pop3.rb +25 -23
  90. data/lib/mail/network/retriever_methods/test_retriever.rb +3 -2
  91. data/lib/mail/network.rb +1 -0
  92. data/lib/mail/parser_tools.rb +15 -0
  93. data/lib/mail/parsers/address_lists_parser.rb +33228 -116
  94. data/lib/mail/parsers/address_lists_parser.rl +183 -0
  95. data/lib/mail/parsers/content_disposition_parser.rb +885 -49
  96. data/lib/mail/parsers/content_disposition_parser.rl +93 -0
  97. data/lib/mail/parsers/content_location_parser.rb +812 -23
  98. data/lib/mail/parsers/content_location_parser.rl +82 -0
  99. data/lib/mail/parsers/content_transfer_encoding_parser.rb +512 -21
  100. data/lib/mail/parsers/content_transfer_encoding_parser.rl +75 -0
  101. data/lib/mail/parsers/content_type_parser.rb +1039 -55
  102. data/lib/mail/parsers/content_type_parser.rl +94 -0
  103. data/lib/mail/parsers/date_time_parser.rb +880 -25
  104. data/lib/mail/parsers/date_time_parser.rl +73 -0
  105. data/lib/mail/parsers/envelope_from_parser.rb +3672 -40
  106. data/lib/mail/parsers/envelope_from_parser.rl +93 -0
  107. data/lib/mail/parsers/message_ids_parser.rb +5149 -25
  108. data/lib/mail/parsers/message_ids_parser.rl +97 -0
  109. data/lib/mail/parsers/mime_version_parser.rb +500 -26
  110. data/lib/mail/parsers/mime_version_parser.rl +72 -0
  111. data/lib/mail/parsers/phrase_lists_parser.rb +873 -22
  112. data/lib/mail/parsers/phrase_lists_parser.rl +94 -0
  113. data/lib/mail/parsers/received_parser.rb +8779 -43
  114. data/lib/mail/parsers/received_parser.rl +95 -0
  115. data/lib/mail/parsers/rfc2045_content_transfer_encoding.rl +13 -0
  116. data/lib/mail/parsers/rfc2045_content_type.rl +25 -0
  117. data/lib/mail/parsers/rfc2045_mime.rl +16 -0
  118. data/lib/mail/parsers/rfc2183_content_disposition.rl +15 -0
  119. data/lib/mail/parsers/rfc3629_utf8.rl +19 -0
  120. data/lib/mail/parsers/rfc5234_abnf_core_rules.rl +22 -0
  121. data/lib/mail/parsers/rfc5322.rl +74 -0
  122. data/lib/mail/parsers/rfc5322_address.rl +72 -0
  123. data/lib/mail/parsers/{ragel/date_time.rl → rfc5322_date_time.rl} +8 -1
  124. data/lib/mail/parsers/rfc5322_lexical_tokens.rl +60 -0
  125. data/lib/mail/parsers.rb +11 -25
  126. data/lib/mail/part.rb +25 -29
  127. data/lib/mail/parts_list.rb +62 -6
  128. data/lib/mail/smtp_envelope.rb +57 -0
  129. data/lib/mail/utilities.rb +361 -74
  130. data/lib/mail/version.rb +2 -2
  131. data/lib/mail/yaml.rb +30 -0
  132. data/lib/mail.rb +4 -37
  133. metadata +125 -67
  134. data/CHANGELOG.rdoc +0 -787
  135. data/CONTRIBUTING.md +0 -60
  136. data/Dependencies.txt +0 -2
  137. data/Gemfile +0 -11
  138. data/Rakefile +0 -29
  139. data/TODO.rdoc +0 -9
  140. data/lib/mail/check_delivery_params.rb +0 -21
  141. data/lib/mail/core_extensions/smtp.rb +0 -25
  142. data/lib/mail/core_extensions/string/access.rb +0 -146
  143. data/lib/mail/core_extensions/string/multibyte.rb +0 -79
  144. data/lib/mail/core_extensions/string.rb +0 -21
  145. data/lib/mail/fields/common/address_container.rb +0 -17
  146. data/lib/mail/fields/common/common_address.rb +0 -136
  147. data/lib/mail/fields/common/common_date.rb +0 -36
  148. data/lib/mail/fields/common/common_field.rb +0 -61
  149. data/lib/mail/fields/common/common_message_id.rb +0 -49
  150. data/lib/mail/multibyte/exceptions.rb +0 -9
  151. data/lib/mail/parsers/ragel/common.rl +0 -185
  152. data/lib/mail/parsers/ragel/parser_info.rb +0 -61
  153. data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb +0 -14864
  154. data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb.rl +0 -37
  155. data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb +0 -751
  156. data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb.rl +0 -37
  157. data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb +0 -614
  158. data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb.rl +0 -37
  159. data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb +0 -447
  160. data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb.rl +0 -37
  161. data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb +0 -825
  162. data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb.rl +0 -37
  163. data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb +0 -817
  164. data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb.rl +0 -37
  165. data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb +0 -2149
  166. data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb.rl +0 -37
  167. data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb +0 -1570
  168. data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb.rl +0 -37
  169. data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb +0 -440
  170. data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb.rl +0 -37
  171. data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb +0 -564
  172. data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb.rl +0 -37
  173. data/lib/mail/parsers/ragel/ruby/machines/rb_actions.rl +0 -51
  174. data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb +0 -5144
  175. data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb.rl +0 -37
  176. data/lib/mail/parsers/ragel/ruby/parser.rb.rl.erb +0 -37
  177. data/lib/mail/parsers/ragel/ruby.rb +0 -40
  178. data/lib/mail/parsers/ragel.rb +0 -18
  179. data/lib/mail/version_specific/ruby_1_8.rb +0 -126
  180. data/lib/mail/version_specific/ruby_1_9.rb +0 -223
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: f91f133d2e44832ecef1cb4bfe9cc57391aedf84
4
- data.tar.gz: 23d0cdbc09cdbaafee76b01d7c3820a6c0dfb6d1
2
+ SHA256:
3
+ metadata.gz: 19bd6b0f4442e909c23a9b7da4aecd758bc7f13f49f55b0d38cbcea5d18a91bf
4
+ data.tar.gz: 30187e6fef5ff7d5aaa6e1608bac3f74748946b6aa84f63bc0152b14f8ec0f70
5
5
  SHA512:
6
- metadata.gz: c788407da309561bee6a59ec746efeea7c0cc8633206eb2085f08ad6b161df92475109a988de748e554248d2ca47e6cd31c3fd0394d0eddac5950e7bb28783a8
7
- data.tar.gz: 55b70f9d539420d254373c7abfccf6bc85228f34c38696086be363a36fb4f966415238aaa8e9ec9e6eed02207af2a3940b9f85c5f3076e735cd19e095764abc2
6
+ metadata.gz: 1d0fca43599584febff0aee7cc250a9280005080a9274024fa011b99ab27e4d40ebf72161c60d7115641d844d560f34b87e07994b485c6c0ae2cbd102cd04b11
7
+ data.tar.gz: 47735a46f1117a0d9709a3b72db5030ea71c645418a60b8fb62637777c9cdd687079fca33f77dfc4388aa5f8d4136768a555890d1f88768b351b81aaea9ab486
data/README.md CHANGED
@@ -1,89 +1,93 @@
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://github.com/mikel/mail/actions/workflows/test.yml/badge.svg)](https://github.com/mikel/mail/actions/workflows/test.yml)
3
2
 
4
- Introduction
5
- ------------
3
+ ## Introduction
6
4
 
7
- Mail is an internet library for Ruby that is designed to handle emails
5
+ Mail is an internet library for Ruby that is designed to handle email
8
6
  generation, parsing and sending in a simple, rubyesque manner.
9
7
 
10
8
  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
9
+ all email functions, including sending and receiving email. All network
12
10
  type actions are done through proxy methods to Net::SMTP, Net::POP3 etc.
13
11
 
14
12
  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
13
+ implementation that makes generating, sending and parsing email a no
16
14
  brainer.
17
15
 
18
16
  It is also designed from the ground up to work with the more modern versions
19
- of Ruby. This is because Ruby > 1.9 handles text encodings much more wonderfully
20
- than Ruby 1.8.x and so these features have been taken full advantage of in this
21
- library allowing Mail to handle a lot more messages more cleanly than TMail.
22
- Mail does run on Ruby 1.8.x... it's just not as fun to code.
17
+ of Ruby. Modern Rubies handle text encodings much more wonderfully than before
18
+ so these features have been taken full advantage of in this library allowing
19
+ Mail to handle a lot more messages more cleanly than TMail.
23
20
 
24
21
  Finally, Mail has been designed with a very simple object oriented system
25
22
  that really opens up the email messages you are parsing, if you know what
26
23
  you are doing, you can fiddle with every last bit of your email directly.
27
24
 
28
- Donations
29
- -------------
30
-
31
- Mail has been downloaded millions of times, by people around the world, in fact,
32
- it represents more than 1% of *all* gems downloaded.
33
-
34
- It is (like all open source software) a labour of love and something I am doing
35
- with my own free time. If you would like to say thanks, please feel free to
36
- [make a donation](http://www.pledgie.com/campaigns/8790) and feel free to send
37
- me a nice email :)
38
-
39
- <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
-
41
-
42
- Compatibility
43
- -------------
44
-
45
- Every Mail commit is tested by Travis on the [following platforms](https://github.com/mikel/mail/blob/master/.travis.yml)
46
-
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.2 [ x86_64 ]
52
- * ruby-head [ x86_64 ]
53
- * jruby [ x86_64 ]
54
- * jruby-head [ x86_64 ]
55
- * rbx-2 [ x86_64 ]
56
-
57
- Testing a specific mime type (needed for 1.8.7 for example) can be done manually with:
58
-
59
- ```sh
60
- BUNDLE_GEMFILE=gemfiles/mime_types_1.16.gemfile (bundle check || bundle) && rake
61
- ```
62
-
63
- Discussion
64
- ----------
25
+ ## You can contribute to this library
26
+
27
+ Yes, you! Mail is used in countless apps by people around the world. It is,
28
+ like all open source software, a labour of love borne from our free time.
29
+ If you would like to say thanks, please dig in and contribute alongside us!
30
+ Triage and fix [GitHub issues](https://github.com/mikel/mail/issues), improve
31
+ our documentation, add new features—up to you! Thank you for pitching in.
32
+
33
+
34
+ # Contents
35
+ * [Compatibility](#compatibility)
36
+ * [Discussion](#discussion)
37
+ * [Current Capabilities of Mail](#current-capabilities-of-mail)
38
+ * [Roadmap](#roadmap)
39
+ * [Testing Policy](#testing-policy)
40
+ * [API Policy](#api-policy)
41
+ * [Installation](#installation)
42
+ * [Encodings](#encodings)
43
+ * [Contributing](#contributing)
44
+ * [Usage](#usage)
45
+ * [Excerpts from TREC Spam Corpus 2005](#excerpts-from-trec-spam-corpus-2005)
46
+ * [License](#license)
47
+
48
+ ## Compatibility
49
+
50
+ Mail is tested against:
51
+
52
+ * Ruby: 2.5
53
+ * Ruby: 2.6
54
+ * Ruby: 2.7
55
+ * Ruby: 3.0
56
+ * Ruby: 3.1
57
+ * Ruby: 3.2
58
+ * JRuby: 9.2
59
+ * JRuby: 9.3
60
+ * JRuby: 9.4
61
+ * JRuby: stable
62
+ * JRuby: head
63
+ * Truffleruby: stable
64
+ * Truffleruby: head
65
+
66
+ As new versions of Ruby are released, Mail will be compatible with support for the "preview" and all "normal maintenance", "security maintenance" and the two most recent "end of life" versions listed at the [Ruby Maintenance Branches](https://www.ruby-lang.org/en/downloads/branches/) page. Pull requests to assist in adding support for new preview releases are more than welcome.
67
+
68
+ Every Mail commit is tested by GitHub Actions on [all supported Ruby versions](https://github.com/mikel/mail/blob/master/.github/workflows/test.yml).
69
+
70
+ ## Discussion
65
71
 
66
72
  If you want to discuss mail with like minded individuals, please subscribe to
67
73
  the [Google Group](http://groups.google.com/group/mail-ruby).
68
74
 
69
- Current Capabilities of Mail
70
- ----------------------------
75
+ ## Current Capabilities of Mail
71
76
 
72
- * RFC2822 Support, Reading and Writing
73
- * RFC2045-2049 Support for multipart emails
74
- * Support for creating multipart alternate emails
75
- * Support for reading multipart/report emails &amp; getting details from such
76
- * Support for multibyte emails - needs quite a lot of work and testing
77
+ * RFC5322 Support, Reading and Writing
78
+ * RFC6532 Support, reading UTF-8 headers
79
+ * RFC2045-2049 Support for multipart email
80
+ * Support for creating multipart alternate email
81
+ * Support for reading multipart/report email &amp; getting details from such
77
82
  * Wrappers for File, Net/POP3, Net/SMTP
78
- * Auto encoding of non US-ASCII header fields
79
- * Auto encoding of non US-ASCII bodies
83
+ * Auto-encoding of non-US-ASCII bodies and header fields
80
84
 
81
- Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
82
- emails. There are a few obsoleted syntax emails that it will have problems with, but
83
- it also is quite robust, meaning, if it finds something it doesn't understand it will
84
- not crash, instead, it will skip the problem and keep parsing. In the case of a header
85
- it doesn't understand, it will initialise the header as an optional unstructured
86
- field and continue parsing.
85
+ Mail is RFC5322 and RFC6532 compliant now, that is, it can parse US-ASCII and UTF-8
86
+ email and generate US-ASCII email. There are a few obsoleted email syntax that
87
+ it will have problems with, but it also is quite robust, meaning, if it finds something
88
+ it doesn't understand it will not crash, instead, it will skip the problem and keep
89
+ parsing. In the case of a header it doesn't understand, it will initialise the header
90
+ as an optional unstructured field and continue parsing.
87
91
 
88
92
  This means Mail won't (ever) crunch your data (I think).
89
93
 
@@ -91,16 +95,14 @@ You can also create MIME emails. There are helper methods for making a
91
95
  multipart/alternate email for text/plain and text/html (the most common pair)
92
96
  and you can manually create any other type of MIME email.
93
97
 
94
- Roadmap
95
- -------
98
+ ## Roadmap
96
99
 
97
100
  Next TODO:
98
101
 
99
102
  * Improve MIME support for character sets in headers, currently works, mostly, needs
100
103
  refinement.
101
104
 
102
- Testing Policy
103
- --------------
105
+ ## Testing Policy
104
106
 
105
107
  Basically... we do BDD on Mail. No method gets written in Mail without a
106
108
  corresponding or covering spec. We expect as a minimum 100% coverage
@@ -110,25 +112,24 @@ the gem gets released.
110
112
 
111
113
  It also means you can be sure Mail will behave correctly.
112
114
 
113
- Note: If you care about core extensions (aka "monkey-patching"), please read the Core Extensions section near the end of this README.
115
+ You can run tests locally by running `bundle exec rspec`.
116
+
117
+ You can run tests on all supported Ruby versions by using [act](https://github.com/nektos/act).
114
118
 
115
- API Policy
116
- ----------
119
+ ## API Policy
117
120
 
118
121
  No API removals within a single point release. All removals to be deprecated with
119
122
  warnings for at least one MINOR point release before removal.
120
123
 
121
124
  Also, all private or protected methods to be declared as such - though this is still I/P.
122
125
 
123
- Installation
124
- ------------
126
+ ## Installation
125
127
 
126
128
  Installation is fairly simple, I host mail on rubygems, so you can just do:
127
129
 
128
130
  # gem install mail
129
131
 
130
- Encodings
131
- ---------
132
+ ## Encodings
132
133
 
133
134
  If you didn't know, handling encodings in Emails is not as straight forward as you
134
135
  would hope.
@@ -160,17 +161,17 @@ I have tried to simplify it some:
160
161
  provide encoded parameter values when you call the parameter names through the
161
162
  <code>object.parameters['<parameter_name>']</code> method call.
162
163
 
163
- Contributing
164
- ------------
164
+ ## Contributing
165
165
 
166
- Please do! Contributing is easy in Mail. Please read the CONTRIBUTING.md document for more info
166
+ Please do! Contributing is easy in Mail. Please read the [CONTRIBUTING.md](CONTRIBUTING.md) document for more info.
167
167
 
168
- Usage
169
- -----
168
+ ## Usage
170
169
 
171
170
  All major mail functions should be able to happen from the Mail module.
172
171
  So, you should be able to just <code>require 'mail'</code> to get started.
173
172
 
173
+ `mail` is pretty well documented in its Ruby code. You can look it up e.g. at [rubydoc.info](https://www.rubydoc.info/gems/mail).
174
+
174
175
  ### Making an email
175
176
 
176
177
  ```ruby
@@ -234,7 +235,7 @@ what you are doing.
234
235
  ### Sending an email:
235
236
 
236
237
  Mail defaults to sending via SMTP to local host port 25. If you have a
237
- sendmail or postfix daemon running on on this port, sending email is as
238
+ sendmail or postfix daemon running on this port, sending email is as
238
239
  easy as:
239
240
 
240
241
  ```ruby
@@ -294,12 +295,23 @@ mail.delivery_method :exim, :location => "/usr/bin/exim"
294
295
  mail.deliver
295
296
  ```
296
297
 
297
- ### Getting emails from a pop server:
298
+ Mail may be "delivered" to a logfile, too, for development and testing:
299
+
300
+ ```ruby
301
+ # Delivers by logging the encoded message to $stdout
302
+ mail.delivery_method :logger
303
+
304
+ # Delivers to an existing logger at :debug severity
305
+ mail.delivery_method :logger, logger: other_logger, severity: :debug
306
+ ```
307
+
308
+ ### Getting Emails from a POP or IMAP Server:
298
309
 
299
310
  You can configure Mail to receive email using <code>retriever_method</code>
300
311
  within <code>Mail.defaults</code>:
301
312
 
302
313
  ```ruby
314
+ # e.g. POP3
303
315
  Mail.defaults do
304
316
  retriever_method :pop3, :address => "pop.gmail.com",
305
317
  :port => 995,
@@ -307,6 +319,15 @@ Mail.defaults do
307
319
  :password => '<password>',
308
320
  :enable_ssl => true
309
321
  end
322
+
323
+ # IMAP
324
+ Mail.defaults do
325
+ retriever_method :imap, :address => "imap.mailbox.org",
326
+ :port => 993,
327
+ :user_name => '<username>',
328
+ :password => '<password>',
329
+ :enable_ssl => true
330
+ end
310
331
  ```
311
332
 
312
333
  You can access incoming email in a number of ways.
@@ -347,7 +368,7 @@ mail.cc #=> 'sam@test.lindsaar.net'
347
368
  mail.subject #=> "This is the subject"
348
369
  mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
349
370
  mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
350
- mail.body.decoded #=> 'This is the body of the email...
371
+ mail.decoded #=> 'This is the body of the email...
351
372
  ```
352
373
 
353
374
  Many more methods available.
@@ -373,7 +394,7 @@ is another message which can have many or no parts.
373
394
  A message will only have parts if it is a multipart/mixed or multipart/related
374
395
  content type and has a boundary defined.
375
396
 
376
- ### Testing and extracting attachments
397
+ ### Testing and Extracting Attachments
377
398
  ```ruby
378
399
  mail.attachments.each do | attachment |
379
400
  # Attachments is an AttachmentsList object containing a
@@ -382,32 +403,35 @@ mail.attachments.each do | attachment |
382
403
  # extracting images for example...
383
404
  filename = attachment.filename
384
405
  begin
385
- File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
406
+ File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.decoded}
386
407
  rescue => e
387
408
  puts "Unable to save data for #{filename} because #{e.message}"
388
409
  end
389
410
  end
390
411
  end
391
412
  ```
392
- ### Writing and sending a multipart/alternative (html and text) email
413
+ ### Writing and Sending a Multipart/Alternative (HTML and Text) Email
393
414
 
394
415
  Mail makes some basic assumptions and makes doing the common thing as
395
416
  simple as possible.... (asking a lot from a mail library)
396
417
 
397
418
  ```ruby
398
419
  mail = Mail.deliver do
399
- to 'nicolas@test.lindsaar.net.au'
400
- from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
401
- subject 'First multipart email sent with Mail'
402
-
403
- text_part do
404
- body 'This is plain text'
405
- end
406
-
407
- html_part do
408
- content_type 'text/html; charset=UTF-8'
409
- body '<h1>This is HTML</h1>'
420
+ part :content_type => "multipart/mixed" do |p1|
421
+ p1.part :content_type => "multipart/related" do |p2|
422
+ p2.part :content_type => "multipart/alternative",
423
+ :content_disposition => "inline" do |p3|
424
+ p3.part :content_type => "text/plain; charset=utf-8",
425
+ :body => "Here is the attachment you wanted\n"
426
+ p3.part :content_type => "text/html; charset=utf-8",
427
+ :body => "<h1>Funky Title</h1><p>Here is the attachment you wanted</p>\n"
428
+ end
429
+ end
430
+ add_file '/path/to/myfile.pdf'
410
431
  end
432
+ from "Mikel Lindsaar <mikel@test.lindsaar.net.au>"
433
+ to "nicolas@test.lindsaar.net.au"
434
+ subject "First multipart email sent with Mail"
411
435
  end
412
436
  ```
413
437
 
@@ -418,44 +442,86 @@ so desire...
418
442
  ```
419
443
  puts mail.to_s #=>
420
444
 
421
- To: nicolas@test.lindsaar.net.au
445
+ Date: Tue, 26 Apr 2022 20:12:07 +0200
422
446
  From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
447
+ To: nicolas@test.lindsaar.net.au
448
+ Message-ID: <626835f736e19_10873fdfa3c2ffd4947a3@sender.at.mail>
423
449
  Subject: First multipart email sent with Mail
450
+ MIME-Version: 1.0
451
+ Content-Type: multipart/mixed;
452
+ boundary=\"--==_mimepart_626835f733867_10873fdfa3c2ffd494636\";
453
+ charset=UTF-8
454
+ Content-Transfer-Encoding: 7bit
455
+
456
+
457
+ ----==_mimepart_626835f733867_10873fdfa3c2ffd494636
458
+ Content-Type: multipart/mixed;
459
+ boundary=\"--==_mimepart_626835f73382a_10873fdfa3c2ffd494518\";
460
+ charset=UTF-8
461
+ Content-Transfer-Encoding: 7bit
462
+
463
+
464
+ ----==_mimepart_626835f73382a_10873fdfa3c2ffd494518
465
+ Content-Type: multipart/related;
466
+ boundary=\"--==_mimepart_626835f7337f5_10873fdfa3c2ffd494438\";
467
+ charset=UTF-8
468
+ Content-Transfer-Encoding: 7bit
469
+
470
+
471
+ ----==_mimepart_626835f7337f5_10873fdfa3c2ffd494438
424
472
  Content-Type: multipart/alternative;
425
- boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659
426
- Message-ID: <4a914f12ac7e_6f0f1ab80267d1@baci.local.mail>
427
- Date: Mon, 24 Aug 2009 00:15:46 +1000
428
- Mime-Version: 1.0
473
+ boundary=\"--==_mimepart_626835f733702_10873fdfa3c2ffd494376\";
474
+ charset=UTF-8
429
475
  Content-Transfer-Encoding: 7bit
476
+ Content-Disposition: inline
477
+ Content-ID: <626835f738373_10873fdfa3c2ffd49488b@sender.at.mail>
430
478
 
431
479
 
432
- ----==_mimepart_4a914f0c911be_6f0f1ab8026659
433
- Content-ID: <4a914f12c8c4_6f0f1ab80268d6@baci.local.mail>
434
- Date: Mon, 24 Aug 2009 00:15:46 +1000
435
- Mime-Version: 1.0
436
- Content-Type: text/plain
480
+ ----==_mimepart_626835f733702_10873fdfa3c2ffd494376
481
+ Content-Type: text/plain;
482
+ charset=utf-8
437
483
  Content-Transfer-Encoding: 7bit
438
484
 
439
- This is plain text
440
- ----==_mimepart_4a914f0c911be_6f0f1ab8026659
441
- Content-Type: text/html; charset=UTF-8
442
- Content-ID: <4a914f12cf86_6f0f1ab802692c@baci.local.mail>
443
- Date: Mon, 24 Aug 2009 00:15:46 +1000
444
- Mime-Version: 1.0
485
+ Here is the attachment you wanted
486
+
487
+ ----==_mimepart_626835f733702_10873fdfa3c2ffd494376
488
+ Content-Type: text/html;
489
+ charset=utf-8
490
+ Content-Transfer-Encoding: 7bit
491
+
492
+ <h1>Funky Title</h1><p>Here is the attachment you wanted</p>
493
+
494
+ ----==_mimepart_626835f733702_10873fdfa3c2ffd494376--
495
+
496
+ ----==_mimepart_626835f7337f5_10873fdfa3c2ffd494438--
497
+
498
+ ----==_mimepart_626835f73382a_10873fdfa3c2ffd494518--
499
+
500
+ ----==_mimepart_626835f733867_10873fdfa3c2ffd494636
501
+ Content-Type: text/plain;
502
+ charset=UTF-8;
503
+ filename=myfile.txt
445
504
  Content-Transfer-Encoding: 7bit
505
+ Content-Disposition: attachment;
506
+ filename=myfile.txt
507
+ Content-ID: <6
508
+ 26835f7386ab_10873fdfa3c2ffd4949b8@sender.at.mail>
446
509
 
447
- <h1>This is HTML</h1>
448
- ----==_mimepart_4a914f0c911be_6f0f1ab8026659--
510
+ Hallo,
511
+ Test
512
+ End
513
+
514
+ ----==_mimepart_626835f733867_10873fdfa3c2ffd494636--
449
515
  ```
450
516
 
451
517
  Mail inserts the content transfer encoding, the mime version,
452
- the content-id's and handles the content-type and boundary.
518
+ the content-IDs and handles the content-type and boundary.
453
519
 
454
520
  Mail assumes that if your text in the body is only us-ascii, that your
455
521
  transfer encoding is 7bit and it is text/plain. You can override this
456
522
  by explicitly declaring it.
457
523
 
458
- ### Making Multipart/Alternate, without a block
524
+ ### Making Multipart/Alternate, Without a Block
459
525
 
460
526
  You don't have to use a block with the text and html part included, you
461
527
  can just do it declaratively. However, you need to add Mail::Parts to
@@ -483,7 +549,7 @@ mail.html_part = html_part
483
549
 
484
550
  Results in the same email as done using the block form
485
551
 
486
- ### Getting error reports from an email:
552
+ ### Getting Error Reports from an Email:
487
553
 
488
554
  ```ruby
489
555
  @mail = Mail.read('/path/to/bounce_message.eml')
@@ -527,7 +593,6 @@ than mail (this should be rarely needed)
527
593
 
528
594
  ```ruby
529
595
  @mail = Mail.new
530
- file_data = File.read('path/to/myfile.pdf')
531
596
  @mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
532
597
  :content => File.read('path/to/myfile.pdf') }
533
598
  @mail.parts.first.mime_type #=> 'application/x-pdf'
@@ -560,8 +625,7 @@ end
560
625
  ```
561
626
  See "Testing and extracting attachments" above for more details.
562
627
 
563
- Using Mail with Testing or Spec'ing Libraries
564
- ---------------------------------------------
628
+ ## Using Mail with Testing or Spec'ing Libraries
565
629
 
566
630
  If mail is part of your system, you'll need a way to test it without actually
567
631
  sending emails, the TestMailer can do this for you.
@@ -597,7 +661,7 @@ Mail.defaults do
597
661
  delivery_method :test # in practice you'd do this in spec_helper.rb
598
662
  end
599
663
 
600
- describe "sending an email" do
664
+ RSpec.describe "sending an email" do
601
665
  include Mail::Matchers
602
666
 
603
667
  before(:each) do
@@ -611,70 +675,59 @@ describe "sending an email" do
611
675
  end
612
676
  end
613
677
 
614
- it { should have_sent_email } # passes if any email at all was sent
678
+ it { is_expected.to have_sent_email } # passes if any email at all was sent
615
679
 
616
- it { should have_sent_email.from('you@you.com') }
617
- it { should have_sent_email.to('mike1@me.com') }
680
+ it { is_expected.to have_sent_email.from('you@you.com') }
681
+ it { is_expected.to have_sent_email.to('mike1@me.com') }
618
682
 
619
683
  # can specify a list of recipients...
620
- it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
684
+ it { is_expected.to have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
621
685
 
622
686
  # ...or chain recipients together
623
- it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
687
+ it { is_expected.to have_sent_email.to('mike1@me.com').to('mike2@me.com') }
624
688
 
625
- it { should have_sent_email.with_subject('testing') }
689
+ it { is_expected.to have_sent_email.with_subject('testing') }
626
690
 
627
- it { should have_sent_email.with_body('hello') }
691
+ it { is_expected.to have_sent_email.with_body('hello') }
628
692
 
629
693
  # Can match subject or body with a regex
630
694
  # (or anything that responds_to? :match)
631
695
 
632
- it { should have_sent_email.matching_subject(/test(ing)?/) }
633
- it { should have_sent_email.matching_body(/h(a|e)llo/) }
696
+ it { is_expected.to have_sent_email.matching_subject(/test(ing)?/) }
697
+ it { is_expected.to have_sent_email.matching_body(/h(a|e)llo/) }
634
698
 
635
699
  # Can chain together modifiers
636
700
  # Note that apart from recipients, repeating a modifier overwrites old value.
637
701
 
638
- it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
702
+ it { is_expected.to have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
639
703
 
640
704
  # test for attachments
641
705
 
642
706
  # ... by specific attachment
643
- it { should_have_sent_email.with_attachments(my_attachment) }
707
+ it { is_expected.to have_sent_email.with_attachments(my_attachment) }
644
708
 
645
709
  # ... or any attachment
646
- it { should_have_sent_email.with_attachments(any_attachment) }
710
+ it { is_expected.to have_sent_email.with_attachments(any_attachment) }
711
+
712
+ # ... or attachment with filename
713
+ it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename('file.txt')) }
714
+
715
+ # ... or attachment with mime_type
716
+ it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type('application/pdf')) }
647
717
 
648
718
  # ... by array of attachments
649
- it { should_have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
719
+ it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
650
720
 
651
721
  #... by presence
652
- it { should_have_sent_email.with_any_attachments }
722
+ it { is_expected.to have_sent_email.with_any_attachments }
653
723
 
654
724
  #... or by absence
655
- it { should_have_sent_email.with_no_attachments }
725
+ it { is_expected.to have_sent_email.with_no_attachments }
656
726
 
657
727
  end
658
728
  ```
659
729
 
660
- Core Extensions
661
- ---------------
662
-
663
- The mail gem adds several constants and methods to Ruby's core objects (similar to the activesupport gem from the Rails project). For example:
664
-
665
- NilClass::blank?
666
- NilClass::to_crlf
667
- NilClass::to_lf
668
- Object::blank?
669
- String::to_crlf
670
- String::to_lf
671
- String::blank?
672
- ...etc...
673
-
674
- For all the details, check out lib/mail/core_extensions/.
675
-
676
- Excerpts from TREC Spam Corpus 2005
677
- -----------------------------------
730
+ ## Excerpts from TREC Spam Corpus 2005
678
731
 
679
732
  The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
680
733
  2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
@@ -692,8 +745,7 @@ They are used as allowed by 'Permitted Uses, Clause 3':
692
745
 
693
746
  -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
694
747
 
695
- License
696
- -------
748
+ ## License
697
749
 
698
750
  (The MIT License)
699
751
 
@@ -6,8 +6,10 @@ 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
+ parts = []
11
+ parts << p if p.attachment?
12
+ parts.concat Mail.new(p.body.encoded).attachments
11
13
  elsif p.parts.empty?
12
14
  p if p.attachment?
13
15
  else
@@ -30,7 +32,7 @@ module Mail
30
32
  # mail.attachments['test.png'].filename #=> 'test.png'
31
33
  # mail.attachments[1].filename #=> 'test.jpg'
32
34
  def [](index_value)
33
- if index_value.is_a?(Fixnum)
35
+ if index_value.is_a?(Integer)
34
36
  self.fetch(index_value)
35
37
  else
36
38
  self.select { |a| a.filename == index_value }.first
@@ -44,6 +46,9 @@ module Mail
44
46
  :content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" }
45
47
 
46
48
  if value.is_a?(Hash)
49
+ if path = value.delete(:filename)
50
+ value[:content] ||= File.open(path, 'rb') { |f| f.read }
51
+ end
47
52
 
48
53
  default_values[:body] = value.delete(:content) if value[:content]
49
54
 
@@ -60,7 +65,7 @@ module Mail
60
65
 
61
66
  if value[:mime_type]
62
67
  default_values[:content_type] = value.delete(:mime_type)
63
- @mime_type = MIME::Types[default_values[:content_type]].first
68
+ @mime_type = MiniMime.lookup_by_content_type(default_values[:content_type])
64
69
  default_values[:content_transfer_encoding] ||= guess_encoding
65
70
  end
66
71
 
@@ -71,7 +76,7 @@ module Mail
71
76
  end
72
77
 
73
78
  if hash[:body].respond_to? :force_encoding and hash[:body].respond_to? :valid_encoding?
74
- if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].downcase == "binary"
79
+ if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].casecmp('binary').zero?
75
80
  hash[:body] = hash[:body].dup if hash[:body].frozen?
76
81
  hash[:body].force_encoding("BINARY")
77
82
  end
@@ -94,12 +99,10 @@ module Mail
94
99
  end
95
100
 
96
101
  def set_mime_type(filename)
97
- # Have to do this because MIME::Types is not Ruby 1.9 safe yet
98
- if RUBY_VERSION >= '1.9'
99
- filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)
100
- end
102
+ filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)
101
103
 
102
- @mime_type = MIME::Types.type_for(filename).first
104
+ @mime_type = MiniMime.lookup_by_filename(filename)
105
+ @mime_type && @mime_type.content_type
103
106
  end
104
107
 
105
108
  end