mail 2.5.5 → 2.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (191) hide show
  1. checksums.yaml +5 -5
  2. data/MIT-LICENSE +1 -1
  3. data/README.md +170 -108
  4. data/lib/mail/attachments_list.rb +13 -10
  5. data/lib/mail/body.rb +105 -91
  6. data/lib/mail/check_delivery_params.rb +30 -22
  7. data/lib/mail/configuration.rb +3 -0
  8. data/lib/mail/constants.rb +79 -0
  9. data/lib/mail/elements/address.rb +118 -174
  10. data/lib/mail/elements/address_list.rb +16 -56
  11. data/lib/mail/elements/content_disposition_element.rb +12 -22
  12. data/lib/mail/elements/content_location_element.rb +9 -17
  13. data/lib/mail/elements/content_transfer_encoding_element.rb +8 -19
  14. data/lib/mail/elements/content_type_element.rb +20 -30
  15. data/lib/mail/elements/date_time_element.rb +10 -21
  16. data/lib/mail/elements/envelope_from_element.rb +23 -31
  17. data/lib/mail/elements/message_ids_element.rb +22 -20
  18. data/lib/mail/elements/mime_version_element.rb +10 -21
  19. data/lib/mail/elements/phrase_list.rb +13 -15
  20. data/lib/mail/elements/received_element.rb +26 -21
  21. data/lib/mail/elements.rb +1 -0
  22. data/lib/mail/encodings/7bit.rb +10 -14
  23. data/lib/mail/encodings/8bit.rb +5 -18
  24. data/lib/mail/encodings/base64.rb +15 -10
  25. data/lib/mail/encodings/binary.rb +4 -22
  26. data/lib/mail/encodings/identity.rb +24 -0
  27. data/lib/mail/encodings/quoted_printable.rb +13 -7
  28. data/lib/mail/encodings/transfer_encoding.rb +47 -28
  29. data/lib/mail/encodings/unix_to_unix.rb +20 -0
  30. data/lib/mail/encodings.rb +102 -93
  31. data/lib/mail/envelope.rb +12 -19
  32. data/lib/mail/field.rb +143 -71
  33. data/lib/mail/field_list.rb +73 -19
  34. data/lib/mail/fields/bcc_field.rb +42 -48
  35. data/lib/mail/fields/cc_field.rb +29 -50
  36. data/lib/mail/fields/comments_field.rb +28 -37
  37. data/lib/mail/fields/common_address_field.rb +170 -0
  38. data/lib/mail/fields/common_date_field.rb +58 -0
  39. data/lib/mail/fields/common_field.rb +77 -0
  40. data/lib/mail/fields/common_message_id_field.rb +42 -0
  41. data/lib/mail/fields/content_description_field.rb +8 -14
  42. data/lib/mail/fields/content_disposition_field.rb +20 -44
  43. data/lib/mail/fields/content_id_field.rb +25 -51
  44. data/lib/mail/fields/content_location_field.rb +12 -25
  45. data/lib/mail/fields/content_transfer_encoding_field.rb +31 -36
  46. data/lib/mail/fields/content_type_field.rb +51 -80
  47. data/lib/mail/fields/date_field.rb +24 -52
  48. data/lib/mail/fields/from_field.rb +29 -50
  49. data/lib/mail/fields/in_reply_to_field.rb +39 -49
  50. data/lib/mail/fields/keywords_field.rb +19 -32
  51. data/lib/mail/fields/message_id_field.rb +26 -71
  52. data/lib/mail/fields/mime_version_field.rb +20 -30
  53. data/lib/mail/fields/named_structured_field.rb +11 -0
  54. data/lib/mail/fields/named_unstructured_field.rb +11 -0
  55. data/lib/mail/fields/optional_field.rb +10 -7
  56. data/lib/mail/fields/{common/parameter_hash.rb → parameter_hash.rb} +16 -13
  57. data/lib/mail/fields/received_field.rb +44 -57
  58. data/lib/mail/fields/references_field.rb +36 -49
  59. data/lib/mail/fields/reply_to_field.rb +29 -50
  60. data/lib/mail/fields/resent_bcc_field.rb +29 -50
  61. data/lib/mail/fields/resent_cc_field.rb +29 -50
  62. data/lib/mail/fields/resent_date_field.rb +6 -30
  63. data/lib/mail/fields/resent_from_field.rb +29 -50
  64. data/lib/mail/fields/resent_message_id_field.rb +6 -29
  65. data/lib/mail/fields/resent_sender_field.rb +28 -57
  66. data/lib/mail/fields/resent_to_field.rb +29 -50
  67. data/lib/mail/fields/return_path_field.rb +51 -55
  68. data/lib/mail/fields/sender_field.rb +35 -56
  69. data/lib/mail/fields/structured_field.rb +4 -30
  70. data/lib/mail/fields/subject_field.rb +10 -11
  71. data/lib/mail/fields/to_field.rb +29 -50
  72. data/lib/mail/fields/unstructured_field.rb +43 -51
  73. data/lib/mail/fields.rb +1 -0
  74. data/lib/mail/header.rb +78 -129
  75. data/lib/mail/indifferent_hash.rb +1 -0
  76. data/lib/mail/mail.rb +18 -11
  77. data/lib/mail/matchers/attachment_matchers.rb +44 -0
  78. data/lib/mail/matchers/has_sent_mail.rb +81 -4
  79. data/lib/mail/message.rb +142 -139
  80. data/lib/mail/multibyte/chars.rb +24 -180
  81. data/lib/mail/multibyte/unicode.rb +32 -27
  82. data/lib/mail/multibyte/utils.rb +27 -43
  83. data/lib/mail/multibyte.rb +56 -16
  84. data/lib/mail/network/delivery_methods/exim.rb +6 -4
  85. data/lib/mail/network/delivery_methods/file_delivery.rb +12 -10
  86. data/lib/mail/network/delivery_methods/logger_delivery.rb +34 -0
  87. data/lib/mail/network/delivery_methods/sendmail.rb +63 -21
  88. data/lib/mail/network/delivery_methods/smtp.rb +76 -50
  89. data/lib/mail/network/delivery_methods/smtp_connection.rb +4 -4
  90. data/lib/mail/network/delivery_methods/test_mailer.rb +5 -2
  91. data/lib/mail/network/retriever_methods/base.rb +9 -8
  92. data/lib/mail/network/retriever_methods/imap.rb +37 -18
  93. data/lib/mail/network/retriever_methods/pop3.rb +6 -3
  94. data/lib/mail/network/retriever_methods/test_retriever.rb +4 -2
  95. data/lib/mail/network.rb +2 -0
  96. data/lib/mail/parser_tools.rb +15 -0
  97. data/lib/mail/parsers/address_lists_parser.rb +33242 -0
  98. data/lib/mail/parsers/address_lists_parser.rl +179 -0
  99. data/lib/mail/parsers/content_disposition_parser.rb +901 -0
  100. data/lib/mail/parsers/content_disposition_parser.rl +89 -0
  101. data/lib/mail/parsers/content_location_parser.rb +822 -0
  102. data/lib/mail/parsers/content_location_parser.rl +78 -0
  103. data/lib/mail/parsers/content_transfer_encoding_parser.rb +522 -0
  104. data/lib/mail/parsers/content_transfer_encoding_parser.rl +71 -0
  105. data/lib/mail/parsers/content_type_parser.rb +1048 -0
  106. data/lib/mail/parsers/content_type_parser.rl +90 -0
  107. data/lib/mail/parsers/date_time_parser.rb +891 -0
  108. data/lib/mail/parsers/date_time_parser.rl +69 -0
  109. data/lib/mail/parsers/envelope_from_parser.rb +3675 -0
  110. data/lib/mail/parsers/envelope_from_parser.rl +89 -0
  111. data/lib/mail/parsers/message_ids_parser.rb +5161 -0
  112. data/lib/mail/parsers/message_ids_parser.rl +93 -0
  113. data/lib/mail/parsers/mime_version_parser.rb +513 -0
  114. data/lib/mail/parsers/mime_version_parser.rl +68 -0
  115. data/lib/mail/parsers/phrase_lists_parser.rb +884 -0
  116. data/lib/mail/parsers/phrase_lists_parser.rl +90 -0
  117. data/lib/mail/parsers/received_parser.rb +8782 -0
  118. data/lib/mail/parsers/received_parser.rl +91 -0
  119. data/lib/mail/parsers/rfc2045_content_transfer_encoding.rl +13 -0
  120. data/lib/mail/parsers/rfc2045_content_type.rl +25 -0
  121. data/lib/mail/parsers/rfc2045_mime.rl +16 -0
  122. data/lib/mail/parsers/rfc2183_content_disposition.rl +15 -0
  123. data/lib/mail/parsers/rfc3629_utf8.rl +19 -0
  124. data/lib/mail/parsers/rfc5234_abnf_core_rules.rl +22 -0
  125. data/lib/mail/parsers/rfc5322.rl +74 -0
  126. data/lib/mail/parsers/rfc5322_address.rl +72 -0
  127. data/lib/mail/parsers/rfc5322_date_time.rl +37 -0
  128. data/lib/mail/parsers/rfc5322_lexical_tokens.rl +60 -0
  129. data/lib/mail/parsers.rb +13 -0
  130. data/lib/mail/part.rb +11 -12
  131. data/lib/mail/parts_list.rb +90 -14
  132. data/lib/mail/smtp_envelope.rb +57 -0
  133. data/lib/mail/utilities.rb +415 -76
  134. data/lib/mail/values/unicode_tables.dat +0 -0
  135. data/lib/mail/version.rb +8 -15
  136. data/lib/mail/yaml.rb +30 -0
  137. data/lib/mail.rb +9 -32
  138. metadata +127 -79
  139. data/CHANGELOG.rdoc +0 -742
  140. data/CONTRIBUTING.md +0 -45
  141. data/Dependencies.txt +0 -3
  142. data/Gemfile +0 -32
  143. data/Rakefile +0 -21
  144. data/TODO.rdoc +0 -9
  145. data/lib/VERSION +0 -4
  146. data/lib/load_parsers.rb +0 -35
  147. data/lib/mail/core_extensions/nil.rb +0 -19
  148. data/lib/mail/core_extensions/object.rb +0 -13
  149. data/lib/mail/core_extensions/smtp.rb +0 -24
  150. data/lib/mail/core_extensions/string/access.rb +0 -145
  151. data/lib/mail/core_extensions/string/multibyte.rb +0 -78
  152. data/lib/mail/core_extensions/string.rb +0 -33
  153. data/lib/mail/fields/common/address_container.rb +0 -16
  154. data/lib/mail/fields/common/common_address.rb +0 -140
  155. data/lib/mail/fields/common/common_date.rb +0 -42
  156. data/lib/mail/fields/common/common_field.rb +0 -57
  157. data/lib/mail/fields/common/common_message_id.rb +0 -48
  158. data/lib/mail/multibyte/exceptions.rb +0 -8
  159. data/lib/mail/parsers/address_lists.rb +0 -64
  160. data/lib/mail/parsers/address_lists.treetop +0 -19
  161. data/lib/mail/parsers/content_disposition.rb +0 -535
  162. data/lib/mail/parsers/content_disposition.treetop +0 -46
  163. data/lib/mail/parsers/content_location.rb +0 -139
  164. data/lib/mail/parsers/content_location.treetop +0 -20
  165. data/lib/mail/parsers/content_transfer_encoding.rb +0 -201
  166. data/lib/mail/parsers/content_transfer_encoding.treetop +0 -18
  167. data/lib/mail/parsers/content_type.rb +0 -971
  168. data/lib/mail/parsers/content_type.treetop +0 -68
  169. data/lib/mail/parsers/date_time.rb +0 -114
  170. data/lib/mail/parsers/date_time.treetop +0 -11
  171. data/lib/mail/parsers/envelope_from.rb +0 -194
  172. data/lib/mail/parsers/envelope_from.treetop +0 -32
  173. data/lib/mail/parsers/message_ids.rb +0 -45
  174. data/lib/mail/parsers/message_ids.treetop +0 -15
  175. data/lib/mail/parsers/mime_version.rb +0 -144
  176. data/lib/mail/parsers/mime_version.treetop +0 -19
  177. data/lib/mail/parsers/phrase_lists.rb +0 -45
  178. data/lib/mail/parsers/phrase_lists.treetop +0 -15
  179. data/lib/mail/parsers/received.rb +0 -71
  180. data/lib/mail/parsers/received.treetop +0 -11
  181. data/lib/mail/parsers/rfc2045.rb +0 -421
  182. data/lib/mail/parsers/rfc2045.treetop +0 -35
  183. data/lib/mail/parsers/rfc2822.rb +0 -5397
  184. data/lib/mail/parsers/rfc2822.treetop +0 -408
  185. data/lib/mail/parsers/rfc2822_obsolete.rb +0 -3768
  186. data/lib/mail/parsers/rfc2822_obsolete.treetop +0 -241
  187. data/lib/mail/patterns.rb +0 -35
  188. data/lib/mail/version_specific/ruby_1_8.rb +0 -119
  189. data/lib/mail/version_specific/ruby_1_9.rb +0 -147
  190. data/lib/tasks/corpus.rake +0 -125
  191. data/lib/tasks/treetop.rake +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 483ee321c6ec26a7fb20deb57c50254f916c5055
4
- data.tar.gz: 49bbe456db7b901b8f9a17cc404ed6cdd4ac84f2
2
+ SHA256:
3
+ metadata.gz: ea4015fe2858f6c1b123e04d4d407169567b1aeea1155b7b145fe75d9d3781b5
4
+ data.tar.gz: 20cdb386c2f878560b9f61ab63b4f0c264c9f9115af5c835b8c66f49486cb274
5
5
  SHA512:
6
- metadata.gz: bc79fcc391b2358f8aecd403453c934399a561bd2cf0c3cb32bf850815a6c99086c47104e0680a7d77a62585453bef8ca27d885f0345d102e0191a1f5354973d
7
- data.tar.gz: cd1ea707e1b61aa29bb452d3b2eaa7a245721b28d17448061cef63beff93c794f3a05d0c104afa79a9cb7026f0b20376f422b76747170d35fe72a59ed64e40e6
6
+ metadata.gz: b2eb55bc6f51b2d93e6d6abed5bb74876b3f96fd1eea00105047b3a211007b2b906ac89bcd1bed52603c211c595d439b6642bd897943decbcc9ac541f88e2049
7
+ data.tar.gz: 063dd68ef655b93d96412bdcf66ab6f191eaf0bfbafa10730ecfa99a447eb3d2a4469d6d1de67ae144bbaada65eaec4c7d8dbb5fe04509146dc1434a5c557fff
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-2013 Mikel Lindsaar
1
+ Copyright (c) 2009-2016 Mikel Lindsaar
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,81 +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
- It is also designed form 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.
16
+ It is also designed from the ground up to work with the more modern versions
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
- Mail supports Ruby 1.8.7+, including JRuby and Rubinius.
46
-
47
- Every Mail commit is tested by Travis on [all supported Ruby versions](https://github.com/mikel/mail/blob/master/.travis.yml).
48
-
49
- Testing a specific version of mime-types (needed for Ruby 1.8.7, for example) can be done manually with:
50
-
51
- ```sh
52
- BUNDLE_GEMFILE=gemfiles/mime_types_1.16.gemfile bundle && rake
53
- ```
54
-
55
- Discussion
56
- ----------
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 Span Corpus 2005](#excerpts-from-trec-span-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
57
71
 
58
72
  If you want to discuss mail with like minded individuals, please subscribe to
59
73
  the [Google Group](http://groups.google.com/group/mail-ruby).
60
74
 
61
- Current Capabilities of Mail
62
- ----------------------------
75
+ ## Current Capabilities of Mail
63
76
 
64
- * RFC2822 Support, Reading and Writing
65
- * RFC2045-2049 Support for multipart emails
66
- * Support for creating multipart alternate emails
67
- * Support for reading multipart/report emails &amp; getting details from such
68
- * 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
69
82
  * Wrappers for File, Net/POP3, Net/SMTP
70
- * Auto encoding of non US-ASCII header fields
71
- * Auto encoding of non US-ASCII bodies
83
+ * Auto-encoding of non-US-ASCII bodies and header fields
72
84
 
73
- Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
74
- emails. There are a few obsoleted syntax emails that it will have problems with, but
75
- it also is quite robust, meaning, if it finds something it doesn't understand it will
76
- not crash, instead, it will skip the problem and keep parsing. In the case of a header
77
- it doesn't understand, it will initialise the header as an optional unstructured
78
- 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.
79
91
 
80
92
  This means Mail won't (ever) crunch your data (I think).
81
93
 
@@ -83,16 +95,14 @@ You can also create MIME emails. There are helper methods for making a
83
95
  multipart/alternate email for text/plain and text/html (the most common pair)
84
96
  and you can manually create any other type of MIME email.
85
97
 
86
- Roadmap
87
- -------
98
+ ## Roadmap
88
99
 
89
100
  Next TODO:
90
101
 
91
102
  * Improve MIME support for character sets in headers, currently works, mostly, needs
92
103
  refinement.
93
104
 
94
- Testing Policy
95
- --------------
105
+ ## Testing Policy
96
106
 
97
107
  Basically... we do BDD on Mail. No method gets written in Mail without a
98
108
  corresponding or covering spec. We expect as a minimum 100% coverage
@@ -102,23 +112,24 @@ the gem gets released.
102
112
 
103
113
  It also means you can be sure Mail will behave correctly.
104
114
 
105
- API Policy
106
- ----------
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).
118
+
119
+ ## API Policy
107
120
 
108
121
  No API removals within a single point release. All removals to be deprecated with
109
122
  warnings for at least one MINOR point release before removal.
110
123
 
111
124
  Also, all private or protected methods to be declared as such - though this is still I/P.
112
125
 
113
- Installation
114
- ------------
126
+ ## Installation
115
127
 
116
128
  Installation is fairly simple, I host mail on rubygems, so you can just do:
117
129
 
118
130
  # gem install mail
119
131
 
120
- Encodings
121
- ---------
132
+ ## Encodings
122
133
 
123
134
  If you didn't know, handling encodings in Emails is not as straight forward as you
124
135
  would hope.
@@ -150,17 +161,17 @@ I have tried to simplify it some:
150
161
  provide encoded parameter values when you call the parameter names through the
151
162
  <code>object.parameters['<parameter_name>']</code> method call.
152
163
 
153
- Contributing
154
- ------------
164
+ ## Contributing
155
165
 
156
- 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.
157
167
 
158
- Usage
159
- -----
168
+ ## Usage
160
169
 
161
170
  All major mail functions should be able to happen from the Mail module.
162
171
  So, you should be able to just <code>require 'mail'</code> to get started.
163
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
+
164
175
  ### Making an email
165
176
 
166
177
  ```ruby
@@ -185,6 +196,8 @@ mail['from'] = 'mikel@test.lindsaar.net'
185
196
  mail[:to] = 'you@test.lindsaar.net'
186
197
  mail.subject = 'This is a test email'
187
198
 
199
+ mail.header['X-Custom-Header'] = 'custom value'
200
+
188
201
  mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
189
202
  ```
190
203
 
@@ -222,15 +235,15 @@ what you are doing.
222
235
  ### Sending an email:
223
236
 
224
237
  Mail defaults to sending via SMTP to local host port 25. If you have a
225
- 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
226
239
  easy as:
227
240
 
228
241
  ```ruby
229
242
  Mail.deliver do
230
- from 'me@test.lindsaar.net'
231
- to 'you@test.lindsaar.net'
232
- subject 'Here is the image you wanted'
233
- body File.read('body.txt')
243
+ from 'me@test.lindsaar.net'
244
+ to 'you@test.lindsaar.net'
245
+ subject 'Here is the image you wanted'
246
+ body File.read('body.txt')
234
247
  add_file '/full/path/to/somefile.png'
235
248
  end
236
249
  ```
@@ -265,6 +278,15 @@ mail.delivery_method :sendmail
265
278
  mail.deliver
266
279
  ```
267
280
 
281
+ Sending via smtp (for example to [mailcatcher](https://github.com/sj26/mailcatcher))
282
+ ```ruby
283
+
284
+ Mail.defaults do
285
+ delivery_method :smtp, address: "localhost", port: 1025
286
+ end
287
+ ```
288
+
289
+
268
290
  Exim requires its own delivery manager, and can be used like so:
269
291
 
270
292
  ```ruby
@@ -273,12 +295,23 @@ mail.delivery_method :exim, :location => "/usr/bin/exim"
273
295
  mail.deliver
274
296
  ```
275
297
 
276
- ### 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:
277
309
 
278
310
  You can configure Mail to receive email using <code>retriever_method</code>
279
311
  within <code>Mail.defaults</code>:
280
312
 
281
313
  ```ruby
314
+ # e.g. POP3
282
315
  Mail.defaults do
283
316
  retriever_method :pop3, :address => "pop.gmail.com",
284
317
  :port => 995,
@@ -286,6 +319,15 @@ Mail.defaults do
286
319
  :password => '<password>',
287
320
  :enable_ssl => true
288
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
289
331
  ```
290
332
 
291
333
  You can access incoming email in a number of ways.
@@ -318,7 +360,7 @@ emails.length #=> LOTS!
318
360
  ```ruby
319
361
  mail = Mail.read('/path/to/message.eml')
320
362
 
321
- mail.envelope.from #=> 'mikel@test.lindsaar.net'
363
+ mail.envelope_from #=> 'mikel@test.lindsaar.net'
322
364
  mail.from.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
323
365
  mail.sender.address #=> 'mikel@test.lindsaar.net'
324
366
  mail.to #=> 'bob@test.lindsaar.net'
@@ -326,7 +368,7 @@ mail.cc #=> 'sam@test.lindsaar.net'
326
368
  mail.subject #=> "This is the subject"
327
369
  mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
328
370
  mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
329
- mail.body.decoded #=> 'This is the body of the email...
371
+ mail.decoded #=> 'This is the body of the email...
330
372
  ```
331
373
 
332
374
  Many more methods available.
@@ -349,10 +391,10 @@ mail.parts[1].content_type_parameters #=> {'name' => 'my.pdf'}
349
391
  Mail generates a tree of parts. Each message has many or no parts. Each part
350
392
  is another message which can have many or no parts.
351
393
 
352
- A message will only have parts if it is a multipart/mixed or related/mixed
394
+ A message will only have parts if it is a multipart/mixed or multipart/related
353
395
  content type and has a boundary defined.
354
396
 
355
- ### Testing and extracting attachments
397
+ ### Testing and Extracting Attachments
356
398
  ```ruby
357
399
  mail.attachments.each do | attachment |
358
400
  # Attachments is an AttachmentsList object containing a
@@ -361,14 +403,14 @@ mail.attachments.each do | attachment |
361
403
  # extracting images for example...
362
404
  filename = attachment.filename
363
405
  begin
364
- File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
365
- rescue Exception => e
406
+ File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.decoded}
407
+ rescue => e
366
408
  puts "Unable to save data for #{filename} because #{e.message}"
367
409
  end
368
410
  end
369
411
  end
370
412
  ```
371
- ### Writing and sending a multipart/alternative (html and text) email
413
+ ### Writing and Sending a Multipart/Alternative (HTML and Text) Email
372
414
 
373
415
  Mail makes some basic assumptions and makes doing the common thing as
374
416
  simple as possible.... (asking a lot from a mail library)
@@ -428,13 +470,13 @@ Content-Transfer-Encoding: 7bit
428
470
  ```
429
471
 
430
472
  Mail inserts the content transfer encoding, the mime version,
431
- the content-id's and handles the content-type and boundary.
473
+ the content-IDs and handles the content-type and boundary.
432
474
 
433
475
  Mail assumes that if your text in the body is only us-ascii, that your
434
476
  transfer encoding is 7bit and it is text/plain. You can override this
435
477
  by explicitly declaring it.
436
478
 
437
- ### Making Multipart/Alternate, without a block
479
+ ### Making Multipart/Alternate, Without a Block
438
480
 
439
481
  You don't have to use a block with the text and html part included, you
440
482
  can just do it declaratively. However, you need to add Mail::Parts to
@@ -462,7 +504,7 @@ mail.html_part = html_part
462
504
 
463
505
  Results in the same email as done using the block form
464
506
 
465
- ### Getting error reports from an email:
507
+ ### Getting Error Reports from an Email:
466
508
 
467
509
  ```ruby
468
510
  @mail = Mail.read('/path/to/bounce_message.eml')
@@ -506,7 +548,6 @@ than mail (this should be rarely needed)
506
548
 
507
549
  ```ruby
508
550
  @mail = Mail.new
509
- file_data = File.read('path/to/myfile.pdf')
510
551
  @mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
511
552
  :content => File.read('path/to/myfile.pdf') }
512
553
  @mail.parts.first.mime_type #=> 'application/x-pdf'
@@ -539,13 +580,12 @@ end
539
580
  ```
540
581
  See "Testing and extracting attachments" above for more details.
541
582
 
542
- Using Mail with Testing or Spec'ing Libraries
543
- ---------------------------------------------
583
+ ## Using Mail with Testing or Spec'ing Libraries
544
584
 
545
585
  If mail is part of your system, you'll need a way to test it without actually
546
586
  sending emails, the TestMailer can do this for you.
547
587
 
548
- ```
588
+ ```ruby
549
589
  require 'mail'
550
590
  => true
551
591
  Mail.defaults do
@@ -569,9 +609,9 @@ Mail::TestMailer.deliveries.clear
569
609
  => []
570
610
  ```
571
611
 
572
- 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):
612
+ There is also a set of RSpec matchers stolen/inspired by Shoulda's ActionMailer matchers (you'll want to set <code>delivery_method</code> as above too):
573
613
 
574
- ```
614
+ ```ruby
575
615
  Mail.defaults do
576
616
  delivery_method :test # in practice you'd do this in spec_helper.rb
577
617
  end
@@ -590,36 +630,59 @@ describe "sending an email" do
590
630
  end
591
631
  end
592
632
 
593
- it { should have_sent_email } # passes if any email at all was sent
633
+ it { is_expected.to have_sent_email } # passes if any email at all was sent
594
634
 
595
- it { should have_sent_email.from('you@you.com') }
596
- it { should have_sent_email.to('mike1@me.com') }
635
+ it { is_expected.to have_sent_email.from('you@you.com') }
636
+ it { is_expected.to have_sent_email.to('mike1@me.com') }
597
637
 
598
638
  # can specify a list of recipients...
599
- it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
639
+ it { is_expected.to have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
600
640
 
601
641
  # ...or chain recipients together
602
- it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
642
+ it { is_expected.to have_sent_email.to('mike1@me.com').to('mike2@me.com') }
603
643
 
604
- it { should have_sent_email.with_subject('testing') }
644
+ it { is_expected.to have_sent_email.with_subject('testing') }
605
645
 
606
- it { should have_sent_email.with_body('hello') }
646
+ it { is_expected.to have_sent_email.with_body('hello') }
607
647
 
608
648
  # Can match subject or body with a regex
609
649
  # (or anything that responds_to? :match)
610
650
 
611
- it { should have_sent_email.matching_subject(/test(ing)?/) }
612
- it { should have_sent_email.matching_body(/h(a|e)llo/) }
651
+ it { is_expected.to have_sent_email.matching_subject(/test(ing)?/) }
652
+ it { is_expected.to have_sent_email.matching_body(/h(a|e)llo/) }
613
653
 
614
654
  # Can chain together modifiers
615
655
  # Note that apart from recipients, repeating a modifier overwrites old value.
616
656
 
617
- it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
657
+ it { is_expected.to have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
658
+
659
+ # test for attachments
660
+
661
+ # ... by specific attachment
662
+ it { is_expected.to have_sent_email.with_attachments(my_attachment) }
663
+
664
+ # ... or any attachment
665
+ it { is_expected.to have_sent_email.with_attachments(any_attachment) }
666
+
667
+ # ... or attachment with filename
668
+ it { is_expected.to have_sent_email.with_attachments(an_attachment_with_filename('file.txt')) }
669
+
670
+ # ... or attachment with mime_type
671
+ it { is_expected.to have_sent_email.with_attachments(an_attachment_with_mime_type('application/pdf')) }
672
+
673
+ # ... by array of attachments
674
+ it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
675
+
676
+ #... by presence
677
+ it { is_expected.to have_sent_email.with_any_attachments }
678
+
679
+ #... or by absence
680
+ it { is_expected.to have_sent_email.with_no_attachments }
681
+
618
682
  end
619
683
  ```
620
684
 
621
- Excerpts from TREC Spam Corpus 2005
622
- -----------------------------------
685
+ ## Excerpts from TREC Spam Corpus 2005
623
686
 
624
687
  The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
625
688
  2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
@@ -637,12 +700,11 @@ They are used as allowed by 'Permitted Uses, Clause 3':
637
700
 
638
701
  -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
639
702
 
640
- License
641
- -------
703
+ ## License
642
704
 
643
705
  (The MIT License)
644
706
 
645
- Copyright (c) 2009-2013 Mikel Lindsaar
707
+ Copyright (c) 2009-2016 Mikel Lindsaar
646
708
 
647
709
  Permission is hereby granted, free of charge, to any person obtaining
648
710
  a copy of this software and associated documentation files (the
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Mail
2
3
  class AttachmentsList < Array
3
4
 
@@ -5,8 +6,8 @@ module Mail
5
6
  @parts_list = parts_list
6
7
  @content_disposition_type = 'attachment'
7
8
  parts_list.map { |p|
8
- if p.content_type == "message/rfc822"
9
- Mail.new(p.body).attachments
9
+ if p.mime_type == 'message/rfc822'
10
+ Mail.new(p.body.encoded).attachments
10
11
  elsif p.parts.empty?
11
12
  p if p.attachment?
12
13
  else
@@ -29,7 +30,7 @@ module Mail
29
30
  # mail.attachments['test.png'].filename #=> 'test.png'
30
31
  # mail.attachments[1].filename #=> 'test.jpg'
31
32
  def [](index_value)
32
- if index_value.is_a?(Fixnum)
33
+ if index_value.is_a?(Integer)
33
34
  self.fetch(index_value)
34
35
  else
35
36
  self.select { |a| a.filename == index_value }.first
@@ -43,6 +44,9 @@ module Mail
43
44
  :content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" }
44
45
 
45
46
  if value.is_a?(Hash)
47
+ if path = value.delete(:filename)
48
+ value[:content] ||= File.open(path, 'rb') { |f| f.read }
49
+ end
46
50
 
47
51
  default_values[:body] = value.delete(:content) if value[:content]
48
52
 
@@ -59,7 +63,7 @@ module Mail
59
63
 
60
64
  if value[:mime_type]
61
65
  default_values[:content_type] = value.delete(:mime_type)
62
- @mime_type = MIME::Types[default_values[:content_type]].first
66
+ @mime_type = MiniMime.lookup_by_content_type(default_values[:content_type])
63
67
  default_values[:content_transfer_encoding] ||= guess_encoding
64
68
  end
65
69
 
@@ -70,7 +74,8 @@ module Mail
70
74
  end
71
75
 
72
76
  if hash[:body].respond_to? :force_encoding and hash[:body].respond_to? :valid_encoding?
73
- if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].downcase == "binary"
77
+ if not hash[:body].valid_encoding? and default_values[:content_transfer_encoding].casecmp('binary').zero?
78
+ hash[:body] = hash[:body].dup if hash[:body].frozen?
74
79
  hash[:body].force_encoding("BINARY")
75
80
  end
76
81
  end
@@ -92,12 +97,10 @@ module Mail
92
97
  end
93
98
 
94
99
  def set_mime_type(filename)
95
- # Have to do this because MIME::Types is not Ruby 1.9 safe yet
96
- if RUBY_VERSION >= '1.9'
97
- filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)
98
- end
100
+ filename = filename.encode(Encoding::UTF_8) if filename.respond_to?(:encode)
99
101
 
100
- @mime_type = MIME::Types.type_for(filename).first
102
+ @mime_type = MiniMime.lookup_by_filename(filename)
103
+ @mime_type && @mime_type.content_type
101
104
  end
102
105
 
103
106
  end