nano-fast-tool 0.0.1

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 (138) hide show
  1. checksums.yaml +7 -0
  2. data/mail-2.9.1/MIT-LICENSE +20 -0
  3. data/mail-2.9.1/README.md +771 -0
  4. data/mail-2.9.1/lib/mail/attachments_list.rb +109 -0
  5. data/mail-2.9.1/lib/mail/body.rb +305 -0
  6. data/mail-2.9.1/lib/mail/configuration.rb +78 -0
  7. data/mail-2.9.1/lib/mail/constants.rb +79 -0
  8. data/mail-2.9.1/lib/mail/elements/address.rb +258 -0
  9. data/mail-2.9.1/lib/mail/elements/address_list.rb +34 -0
  10. data/mail-2.9.1/lib/mail/elements/content_disposition_element.rb +20 -0
  11. data/mail-2.9.1/lib/mail/elements/content_location_element.rb +17 -0
  12. data/mail-2.9.1/lib/mail/elements/content_transfer_encoding_element.rb +13 -0
  13. data/mail-2.9.1/lib/mail/elements/content_type_element.rb +25 -0
  14. data/mail-2.9.1/lib/mail/elements/date_time_element.rb +15 -0
  15. data/mail-2.9.1/lib/mail/elements/envelope_from_element.rb +39 -0
  16. data/mail-2.9.1/lib/mail/elements/message_ids_element.rb +31 -0
  17. data/mail-2.9.1/lib/mail/elements/mime_version_element.rb +15 -0
  18. data/mail-2.9.1/lib/mail/elements/phrase_list.rb +19 -0
  19. data/mail-2.9.1/lib/mail/elements/received_element.rb +35 -0
  20. data/mail-2.9.1/lib/mail/elements.rb +15 -0
  21. data/mail-2.9.1/lib/mail/encodings/7bit.rb +27 -0
  22. data/mail-2.9.1/lib/mail/encodings/8bit.rb +18 -0
  23. data/mail-2.9.1/lib/mail/encodings/base64.rb +38 -0
  24. data/mail-2.9.1/lib/mail/encodings/binary.rb +13 -0
  25. data/mail-2.9.1/lib/mail/encodings/identity.rb +24 -0
  26. data/mail-2.9.1/lib/mail/encodings/quoted_printable.rb +45 -0
  27. data/mail-2.9.1/lib/mail/encodings/transfer_encoding.rb +77 -0
  28. data/mail-2.9.1/lib/mail/encodings/unix_to_unix.rb +20 -0
  29. data/mail-2.9.1/lib/mail/encodings.rb +314 -0
  30. data/mail-2.9.1/lib/mail/envelope.rb +28 -0
  31. data/mail-2.9.1/lib/mail/field.rb +313 -0
  32. data/mail-2.9.1/lib/mail/field_list.rb +87 -0
  33. data/mail-2.9.1/lib/mail/fields/bcc_field.rb +49 -0
  34. data/mail-2.9.1/lib/mail/fields/cc_field.rb +33 -0
  35. data/mail-2.9.1/lib/mail/fields/comments_field.rb +31 -0
  36. data/mail-2.9.1/lib/mail/fields/common_address_field.rb +162 -0
  37. data/mail-2.9.1/lib/mail/fields/common_date_field.rb +56 -0
  38. data/mail-2.9.1/lib/mail/fields/common_field.rb +77 -0
  39. data/mail-2.9.1/lib/mail/fields/common_message_id_field.rb +41 -0
  40. data/mail-2.9.1/lib/mail/fields/content_description_field.rb +12 -0
  41. data/mail-2.9.1/lib/mail/fields/content_disposition_field.rb +44 -0
  42. data/mail-2.9.1/lib/mail/fields/content_id_field.rb +35 -0
  43. data/mail-2.9.1/lib/mail/fields/content_location_field.rb +28 -0
  44. data/mail-2.9.1/lib/mail/fields/content_transfer_encoding_field.rb +44 -0
  45. data/mail-2.9.1/lib/mail/fields/content_type_field.rb +171 -0
  46. data/mail-2.9.1/lib/mail/fields/date_field.rb +28 -0
  47. data/mail-2.9.1/lib/mail/fields/from_field.rb +33 -0
  48. data/mail-2.9.1/lib/mail/fields/in_reply_to_field.rb +45 -0
  49. data/mail-2.9.1/lib/mail/fields/keywords_field.rb +30 -0
  50. data/mail-2.9.1/lib/mail/fields/message_id_field.rb +36 -0
  51. data/mail-2.9.1/lib/mail/fields/mime_version_field.rb +42 -0
  52. data/mail-2.9.1/lib/mail/fields/named_structured_field.rb +10 -0
  53. data/mail-2.9.1/lib/mail/fields/named_unstructured_field.rb +10 -0
  54. data/mail-2.9.1/lib/mail/fields/optional_field.rb +15 -0
  55. data/mail-2.9.1/lib/mail/fields/parameter_hash.rb +61 -0
  56. data/mail-2.9.1/lib/mail/fields/received_field.rb +61 -0
  57. data/mail-2.9.1/lib/mail/fields/references_field.rb +42 -0
  58. data/mail-2.9.1/lib/mail/fields/reply_to_field.rb +33 -0
  59. data/mail-2.9.1/lib/mail/fields/resent_bcc_field.rb +33 -0
  60. data/mail-2.9.1/lib/mail/fields/resent_cc_field.rb +33 -0
  61. data/mail-2.9.1/lib/mail/fields/resent_date_field.rb +10 -0
  62. data/mail-2.9.1/lib/mail/fields/resent_from_field.rb +33 -0
  63. data/mail-2.9.1/lib/mail/fields/resent_message_id_field.rb +10 -0
  64. data/mail-2.9.1/lib/mail/fields/resent_sender_field.rb +32 -0
  65. data/mail-2.9.1/lib/mail/fields/resent_to_field.rb +33 -0
  66. data/mail-2.9.1/lib/mail/fields/return_path_field.rb +60 -0
  67. data/mail-2.9.1/lib/mail/fields/sender_field.rb +45 -0
  68. data/mail-2.9.1/lib/mail/fields/structured_field.rb +24 -0
  69. data/mail-2.9.1/lib/mail/fields/subject_field.rb +14 -0
  70. data/mail-2.9.1/lib/mail/fields/to_field.rb +33 -0
  71. data/mail-2.9.1/lib/mail/fields/unstructured_field.rb +189 -0
  72. data/mail-2.9.1/lib/mail/fields.rb +45 -0
  73. data/mail-2.9.1/lib/mail/header.rb +237 -0
  74. data/mail-2.9.1/lib/mail/indifferent_hash.rb +147 -0
  75. data/mail-2.9.1/lib/mail/mail.rb +267 -0
  76. data/mail-2.9.1/lib/mail/matchers/attachment_matchers.rb +44 -0
  77. data/mail-2.9.1/lib/mail/matchers/has_sent_mail.rb +201 -0
  78. data/mail-2.9.1/lib/mail/message.rb +2159 -0
  79. data/mail-2.9.1/lib/mail/multibyte/chars.rb +318 -0
  80. data/mail-2.9.1/lib/mail/multibyte/unicode.rb +405 -0
  81. data/mail-2.9.1/lib/mail/multibyte/utils.rb +44 -0
  82. data/mail-2.9.1/lib/mail/multibyte.rb +82 -0
  83. data/mail-2.9.1/lib/mail/network/delivery_methods/exim.rb +50 -0
  84. data/mail-2.9.1/lib/mail/network/delivery_methods/file_delivery.rb +43 -0
  85. data/mail-2.9.1/lib/mail/network/delivery_methods/logger_delivery.rb +34 -0
  86. data/mail-2.9.1/lib/mail/network/delivery_methods/sendmail.rb +87 -0
  87. data/mail-2.9.1/lib/mail/network/delivery_methods/smtp.rb +200 -0
  88. data/mail-2.9.1/lib/mail/network/delivery_methods/smtp_connection.rb +57 -0
  89. data/mail-2.9.1/lib/mail/network/delivery_methods/test_mailer.rb +44 -0
  90. data/mail-2.9.1/lib/mail/network/retriever_methods/base.rb +64 -0
  91. data/mail-2.9.1/lib/mail/network/retriever_methods/imap.rb +190 -0
  92. data/mail-2.9.1/lib/mail/network/retriever_methods/pop3.rb +143 -0
  93. data/mail-2.9.1/lib/mail/network/retriever_methods/test_retriever.rb +45 -0
  94. data/mail-2.9.1/lib/mail/network.rb +16 -0
  95. data/mail-2.9.1/lib/mail/parser_tools.rb +15 -0
  96. data/mail-2.9.1/lib/mail/parsers/address_lists_parser.rb +33245 -0
  97. data/mail-2.9.1/lib/mail/parsers/address_lists_parser.rl +183 -0
  98. data/mail-2.9.1/lib/mail/parsers/content_disposition_parser.rb +904 -0
  99. data/mail-2.9.1/lib/mail/parsers/content_disposition_parser.rl +93 -0
  100. data/mail-2.9.1/lib/mail/parsers/content_location_parser.rb +825 -0
  101. data/mail-2.9.1/lib/mail/parsers/content_location_parser.rl +82 -0
  102. data/mail-2.9.1/lib/mail/parsers/content_transfer_encoding_parser.rb +525 -0
  103. data/mail-2.9.1/lib/mail/parsers/content_transfer_encoding_parser.rl +75 -0
  104. data/mail-2.9.1/lib/mail/parsers/content_type_parser.rb +1051 -0
  105. data/mail-2.9.1/lib/mail/parsers/content_type_parser.rl +94 -0
  106. data/mail-2.9.1/lib/mail/parsers/date_time_parser.rb +894 -0
  107. data/mail-2.9.1/lib/mail/parsers/date_time_parser.rl +73 -0
  108. data/mail-2.9.1/lib/mail/parsers/envelope_from_parser.rb +3678 -0
  109. data/mail-2.9.1/lib/mail/parsers/envelope_from_parser.rl +93 -0
  110. data/mail-2.9.1/lib/mail/parsers/message_ids_parser.rb +5164 -0
  111. data/mail-2.9.1/lib/mail/parsers/message_ids_parser.rl +97 -0
  112. data/mail-2.9.1/lib/mail/parsers/mime_version_parser.rb +516 -0
  113. data/mail-2.9.1/lib/mail/parsers/mime_version_parser.rl +72 -0
  114. data/mail-2.9.1/lib/mail/parsers/phrase_lists_parser.rb +887 -0
  115. data/mail-2.9.1/lib/mail/parsers/phrase_lists_parser.rl +94 -0
  116. data/mail-2.9.1/lib/mail/parsers/received_parser.rb +8785 -0
  117. data/mail-2.9.1/lib/mail/parsers/received_parser.rl +95 -0
  118. data/mail-2.9.1/lib/mail/parsers/rfc2045_content_transfer_encoding.rl +13 -0
  119. data/mail-2.9.1/lib/mail/parsers/rfc2045_content_type.rl +25 -0
  120. data/mail-2.9.1/lib/mail/parsers/rfc2045_mime.rl +16 -0
  121. data/mail-2.9.1/lib/mail/parsers/rfc2183_content_disposition.rl +15 -0
  122. data/mail-2.9.1/lib/mail/parsers/rfc3629_utf8.rl +19 -0
  123. data/mail-2.9.1/lib/mail/parsers/rfc5234_abnf_core_rules.rl +22 -0
  124. data/mail-2.9.1/lib/mail/parsers/rfc5322.rl +74 -0
  125. data/mail-2.9.1/lib/mail/parsers/rfc5322_address.rl +72 -0
  126. data/mail-2.9.1/lib/mail/parsers/rfc5322_date_time.rl +37 -0
  127. data/mail-2.9.1/lib/mail/parsers/rfc5322_lexical_tokens.rl +60 -0
  128. data/mail-2.9.1/lib/mail/parsers.rb +13 -0
  129. data/mail-2.9.1/lib/mail/part.rb +119 -0
  130. data/mail-2.9.1/lib/mail/parts_list.rb +131 -0
  131. data/mail-2.9.1/lib/mail/smtp_envelope.rb +57 -0
  132. data/mail-2.9.1/lib/mail/utilities.rb +576 -0
  133. data/mail-2.9.1/lib/mail/values/unicode_tables.dat +0 -0
  134. data/mail-2.9.1/lib/mail/version.rb +17 -0
  135. data/mail-2.9.1/lib/mail/yaml.rb +30 -0
  136. data/mail-2.9.1/lib/mail.rb +65 -0
  137. data/nano-fast-tool.gemspec +12 -0
  138. metadata +177 -0
@@ -0,0 +1,771 @@
1
+ # Mail [![Build Status](https://github.com/mikel/mail/actions/workflows/test.yml/badge.svg)](https://github.com/mikel/mail/actions/workflows/test.yml)
2
+
3
+ ## Introduction
4
+
5
+ Mail is an internet library for Ruby that is designed to handle email
6
+ generation, parsing and sending in a simple, rubyesque manner.
7
+
8
+ The purpose of this library is to provide a single point of access to handle
9
+ all email functions, including sending and receiving email. All network
10
+ type actions are done through proxy methods to Net::SMTP, Net::POP3 etc.
11
+
12
+ Built from my experience with TMail, it is designed to be a pure ruby
13
+ implementation that makes generating, sending and parsing email a no
14
+ brainer.
15
+
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.
20
+
21
+ Finally, Mail has been designed with a very simple object oriented system
22
+ that really opens up the email messages you are parsing, if you know what
23
+ you are doing, you can fiddle with every last bit of your email directly.
24
+
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
71
+
72
+ If you want to discuss mail with like minded individuals, please subscribe to
73
+ the [Google Group](http://groups.google.com/group/mail-ruby).
74
+
75
+ ## Current Capabilities of Mail
76
+
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 & getting details from such
82
+ * Wrappers for File, Net/POP3, Net/SMTP
83
+ * Auto-encoding of non-US-ASCII bodies and header fields
84
+
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.
91
+
92
+ This means Mail won't (ever) crunch your data (I think).
93
+
94
+ You can also create MIME emails. There are helper methods for making a
95
+ multipart/alternate email for text/plain and text/html (the most common pair)
96
+ and you can manually create any other type of MIME email.
97
+
98
+ ## Roadmap
99
+
100
+ Next TODO:
101
+
102
+ * Improve MIME support for character sets in headers, currently works, mostly, needs
103
+ refinement.
104
+
105
+ ## Testing Policy
106
+
107
+ Basically... we do BDD on Mail. No method gets written in Mail without a
108
+ corresponding or covering spec. We expect as a minimum 100% coverage
109
+ measured by RCov. While this is not perfect by any measure, it is pretty
110
+ good. Additionally, all functional tests from TMail are to be passing before
111
+ the gem gets released.
112
+
113
+ It also means you can be sure Mail will behave correctly.
114
+
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
120
+
121
+ No API removals within a single point release. All removals to be deprecated with
122
+ warnings for at least one MINOR point release before removal.
123
+
124
+ Also, all private or protected methods to be declared as such - though this is still I/P.
125
+
126
+ ## Installation
127
+
128
+ Installation is fairly simple, I host mail on rubygems, so you can just do:
129
+
130
+ # gem install mail
131
+
132
+ ## Encodings
133
+
134
+ If you didn't know, handling encodings in Emails is not as straight forward as you
135
+ would hope.
136
+
137
+ I have tried to simplify it some:
138
+
139
+ 1. All objects that can render into an email, have an `#encoded` method. Encoded will
140
+ return the object as a complete string ready to send in the mail system, that is,
141
+ it will include the header field and value and CRLF at the end and wrapped as
142
+ needed.
143
+
144
+ 2. All objects that can render into an email, have a `#decoded` method. Decoded will
145
+ return the object's "value" only as a string. This means it will not include
146
+ the header fields (like 'To:' or 'Subject:').
147
+
148
+ 3. By default, calling <code>#to_s</code> on a container object will call its encoded
149
+ method, while <code>#to_s</code> on a field object will call its decoded method.
150
+ So calling <code>#to_s</code> on a Mail object will return the mail, all encoded
151
+ ready to send, while calling <code>#to_s</code> on the From field or the body will
152
+ return the decoded value of the object. The header object of Mail is considered a
153
+ container. If you are in doubt, call <code>#encoded</code>, or <code>#decoded</code>
154
+ explicitly, this is safer if you are not sure.
155
+
156
+ 4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
157
+ provide decoded parameter values when you call the parameter names as methods against
158
+ the object.
159
+
160
+ 5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
161
+ provide encoded parameter values when you call the parameter names through the
162
+ <code>object.parameters['<parameter_name>']</code> method call.
163
+
164
+ ## Contributing
165
+
166
+ Please do! Contributing is easy in Mail. Please read the [CONTRIBUTING.md](CONTRIBUTING.md) document for more info.
167
+
168
+ ## Usage
169
+
170
+ All major mail functions should be able to happen from the Mail module.
171
+ So, you should be able to just <code>require 'mail'</code> to get started.
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
+
175
+ ### Making an email
176
+
177
+ ```ruby
178
+ mail = Mail.new do
179
+ from 'mikel@test.lindsaar.net'
180
+ to 'you@test.lindsaar.net'
181
+ subject 'This is a test email'
182
+ body File.read('body.txt')
183
+ end
184
+
185
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
186
+ ```
187
+
188
+ ### Making an email, have it your way:
189
+
190
+ ```ruby
191
+ mail = Mail.new do
192
+ body File.read('body.txt')
193
+ end
194
+
195
+ mail['from'] = 'mikel@test.lindsaar.net'
196
+ mail[:to] = 'you@test.lindsaar.net'
197
+ mail.subject = 'This is a test email'
198
+
199
+ mail.header['X-Custom-Header'] = 'custom value'
200
+
201
+ mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
202
+ ```
203
+
204
+ ### Don't Worry About Message IDs:
205
+
206
+ ```ruby
207
+ mail = Mail.new do
208
+ to 'you@test.lindsaar.net'
209
+ body 'Some simple body'
210
+ end
211
+
212
+ mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
213
+ ```
214
+
215
+ Mail will automatically add a Message-ID field if it is missing and
216
+ give it a unique, random Message-ID along the lines of:
217
+
218
+ <4a7ff76d7016_13a81ab802e1@local.host.mail>
219
+
220
+ ### Or do worry about Message-IDs:
221
+
222
+ ```ruby
223
+ mail = Mail.new do
224
+ to 'you@test.lindsaar.net'
225
+ message_id '<ThisIsMyMessageId@some.domain.com>'
226
+ body 'Some simple body'
227
+ end
228
+
229
+ mail.to_s =~ /Message\-ID: <ThisIsMyMessageId@some.domain.com>/ #=> 27
230
+ ```
231
+
232
+ Mail will take the message_id you assign to it trusting that you know
233
+ what you are doing.
234
+
235
+ ### Sending an email:
236
+
237
+ Mail defaults to sending via SMTP to local host port 25. If you have a
238
+ sendmail or postfix daemon running on this port, sending email is as
239
+ easy as:
240
+
241
+ ```ruby
242
+ Mail.deliver do
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')
247
+ add_file '/full/path/to/somefile.png'
248
+ end
249
+ ```
250
+
251
+ or
252
+
253
+ ```ruby
254
+ mail = Mail.new do
255
+ from 'me@test.lindsaar.net'
256
+ to 'you@test.lindsaar.net'
257
+ subject 'Here is the image you wanted'
258
+ body File.read('body.txt')
259
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
260
+ end
261
+
262
+ mail.deliver!
263
+ ```
264
+
265
+ Sending via sendmail can be done like so:
266
+
267
+ ```ruby
268
+ mail = Mail.new do
269
+ from 'me@test.lindsaar.net'
270
+ to 'you@test.lindsaar.net'
271
+ subject 'Here is the image you wanted'
272
+ body File.read('body.txt')
273
+ add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
274
+ end
275
+
276
+ mail.delivery_method :sendmail
277
+
278
+ mail.deliver
279
+ ```
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
+
290
+ Exim requires its own delivery manager, and can be used like so:
291
+
292
+ ```ruby
293
+ mail.delivery_method :exim, :location => "/usr/bin/exim"
294
+
295
+ mail.deliver
296
+ ```
297
+
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:
309
+
310
+ You can configure Mail to receive email using <code>retriever_method</code>
311
+ within <code>Mail.defaults</code>:
312
+
313
+ ```ruby
314
+ # e.g. POP3
315
+ Mail.defaults do
316
+ retriever_method :pop3, :address => "pop.gmail.com",
317
+ :port => 995,
318
+ :user_name => '<username>',
319
+ :password => '<password>',
320
+ :enable_ssl => true
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
331
+ ```
332
+
333
+ You can access incoming email in a number of ways.
334
+
335
+ The most recent email:
336
+
337
+ ```ruby
338
+ Mail.all #=> Returns an array of all emails
339
+ Mail.first #=> Returns the first unread email
340
+ Mail.last #=> Returns the last unread email
341
+ ```
342
+
343
+ The first 10 emails sorted by date in ascending order:
344
+
345
+ ```ruby
346
+ emails = Mail.find(:what => :first, :count => 10, :order => :asc)
347
+ emails.length #=> 10
348
+ ```
349
+
350
+ Or even all emails:
351
+
352
+ ```ruby
353
+ emails = Mail.all
354
+ emails.length #=> LOTS!
355
+ ```
356
+
357
+
358
+ ### Reading an Email
359
+
360
+ ```ruby
361
+ mail = Mail.read('/path/to/message.eml')
362
+
363
+ mail.envelope_from #=> 'mikel@test.lindsaar.net'
364
+ mail.from.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
365
+ mail.sender.address #=> 'mikel@test.lindsaar.net'
366
+ mail.to #=> 'bob@test.lindsaar.net'
367
+ mail.cc #=> 'sam@test.lindsaar.net'
368
+ mail.subject #=> "This is the subject"
369
+ mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
370
+ mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
371
+ mail.decoded #=> 'This is the body of the email...
372
+ ```
373
+
374
+ Many more methods available.
375
+
376
+ ### Reading a Multipart Email
377
+
378
+ ```ruby
379
+ mail = Mail.read('multipart_email')
380
+
381
+ mail.multipart? #=> true
382
+ mail.parts.length #=> 2
383
+ mail.body.preamble #=> "Text before the first part"
384
+ mail.body.epilogue #=> "Text after the last part"
385
+ mail.parts.map { |p| p.content_type } #=> ['text/plain', 'application/pdf']
386
+ mail.parts.map { |p| p.class } #=> [Mail::Message, Mail::Message]
387
+ mail.parts[0].content_type_parameters #=> {'charset' => 'ISO-8859-1'}
388
+ mail.parts[1].content_type_parameters #=> {'name' => 'my.pdf'}
389
+ ```
390
+
391
+ Mail generates a tree of parts. Each message has many or no parts. Each part
392
+ is another message which can have many or no parts.
393
+
394
+ A message will only have parts if it is a multipart/mixed or multipart/related
395
+ content type and has a boundary defined.
396
+
397
+ ### Testing and Extracting Attachments
398
+ ```ruby
399
+ mail.attachments.each do | attachment |
400
+ # Attachments is an AttachmentsList object containing a
401
+ # number of Part objects
402
+ if (attachment.content_type.start_with?('image/'))
403
+ # extracting images for example...
404
+ filename = attachment.filename
405
+ begin
406
+ File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.decoded}
407
+ rescue => e
408
+ puts "Unable to save data for #{filename} because #{e.message}"
409
+ end
410
+ end
411
+ end
412
+ ```
413
+ ### Writing and Sending a Multipart/Alternative (HTML and Text) Email
414
+
415
+ Mail makes some basic assumptions and makes doing the common thing as
416
+ simple as possible.... (asking a lot from a mail library)
417
+
418
+ ```ruby
419
+ mail = Mail.deliver do
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'
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"
435
+ end
436
+ ```
437
+
438
+ Mail then delivers the email at the end of the block and returns the
439
+ resulting Mail::Message object, which you can then inspect if you
440
+ so desire...
441
+
442
+ ```
443
+ puts mail.to_s #=>
444
+
445
+ Date: Tue, 26 Apr 2022 20:12:07 +0200
446
+ From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
447
+ To: nicolas@test.lindsaar.net.au
448
+ Message-ID: <626835f736e19_10873fdfa3c2ffd4947a3@sender.at.mail>
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
472
+ Content-Type: multipart/alternative;
473
+ boundary=\"--==_mimepart_626835f733702_10873fdfa3c2ffd494376\";
474
+ charset=UTF-8
475
+ Content-Transfer-Encoding: 7bit
476
+ Content-Disposition: inline
477
+ Content-ID: <626835f738373_10873fdfa3c2ffd49488b@sender.at.mail>
478
+
479
+
480
+ ----==_mimepart_626835f733702_10873fdfa3c2ffd494376
481
+ Content-Type: text/plain;
482
+ charset=utf-8
483
+ Content-Transfer-Encoding: 7bit
484
+
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
504
+ Content-Transfer-Encoding: 7bit
505
+ Content-Disposition: attachment;
506
+ filename=myfile.txt
507
+ Content-ID: <6
508
+ 26835f7386ab_10873fdfa3c2ffd4949b8@sender.at.mail>
509
+
510
+ Hallo,
511
+ Test
512
+ End
513
+
514
+ ----==_mimepart_626835f733867_10873fdfa3c2ffd494636--
515
+ ```
516
+
517
+ Mail inserts the content transfer encoding, the mime version,
518
+ the content-IDs and handles the content-type and boundary.
519
+
520
+ Mail assumes that if your text in the body is only us-ascii, that your
521
+ transfer encoding is 7bit and it is text/plain. You can override this
522
+ by explicitly declaring it.
523
+
524
+ ### Making Multipart/Alternate, Without a Block
525
+
526
+ You don't have to use a block with the text and html part included, you
527
+ can just do it declaratively. However, you need to add Mail::Parts to
528
+ an email, not Mail::Messages.
529
+
530
+ ```ruby
531
+ mail = Mail.new do
532
+ to 'nicolas@test.lindsaar.net.au'
533
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
534
+ subject 'First multipart email sent with Mail'
535
+ end
536
+
537
+ text_part = Mail::Part.new do
538
+ body 'This is plain text'
539
+ end
540
+
541
+ html_part = Mail::Part.new do
542
+ content_type 'text/html; charset=UTF-8'
543
+ body '<h1>This is HTML</h1>'
544
+ end
545
+
546
+ mail.text_part = text_part
547
+ mail.html_part = html_part
548
+ ```
549
+
550
+ Results in the same email as done using the block form
551
+
552
+ ### Getting Error Reports from an Email:
553
+
554
+ ```ruby
555
+ @mail = Mail.read('/path/to/bounce_message.eml')
556
+
557
+ @mail.bounced? #=> true
558
+ @mail.final_recipient #=> rfc822;mikel@dont.exist.com
559
+ @mail.action #=> failed
560
+ @mail.error_status #=> 5.5.0
561
+ @mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
562
+ @mail.retryable? #=> false
563
+ ```
564
+
565
+ ### Attaching and Detaching Files
566
+
567
+ You can just read the file off an absolute path, Mail will try
568
+ to guess the mime_type and will encode the file in Base64 for you.
569
+
570
+ ```ruby
571
+ @mail = Mail.new
572
+ @mail.add_file("/path/to/file.jpg")
573
+ @mail.parts.first.attachment? #=> true
574
+ @mail.parts.first.content_transfer_encoding.to_s #=> 'base64'
575
+ @mail.attachments.first.mime_type #=> 'image/jpg'
576
+ @mail.attachments.first.filename #=> 'file.jpg'
577
+ @mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=> true
578
+ ```
579
+
580
+ Or You can pass in file_data and give it a filename, again, mail
581
+ will try and guess the mime_type for you.
582
+
583
+ ```ruby
584
+ @mail = Mail.new
585
+ @mail.attachments['myfile.pdf'] = File.read('path/to/myfile.pdf')
586
+ @mail.parts.first.attachment? #=> true
587
+ @mail.attachments.first.mime_type #=> 'application/pdf'
588
+ @mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=> true
589
+ ```
590
+
591
+ You can also override the guessed MIME media type if you really know better
592
+ than mail (this should be rarely needed)
593
+
594
+ ```ruby
595
+ @mail = Mail.new
596
+ @mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
597
+ :content => File.read('path/to/myfile.pdf') }
598
+ @mail.parts.first.mime_type #=> 'application/x-pdf'
599
+ ```
600
+
601
+ Of course... Mail will round trip an attachment as well
602
+
603
+ ```ruby
604
+ @mail = Mail.new do
605
+ to 'nicolas@test.lindsaar.net.au'
606
+ from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
607
+ subject 'First multipart email sent with Mail'
608
+
609
+ text_part do
610
+ body 'Here is the attachment you wanted'
611
+ end
612
+
613
+ html_part do
614
+ content_type 'text/html; charset=UTF-8'
615
+ body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
616
+ end
617
+
618
+ add_file '/path/to/myfile.pdf'
619
+ end
620
+
621
+ @round_tripped_mail = Mail.new(@mail.encoded)
622
+
623
+ @round_tripped_mail.attachments.length #=> 1
624
+ @round_tripped_mail.attachments.first.filename #=> 'myfile.pdf'
625
+ ```
626
+ See "Testing and extracting attachments" above for more details.
627
+
628
+ ## Using Mail with Testing or Spec'ing Libraries
629
+
630
+ If mail is part of your system, you'll need a way to test it without actually
631
+ sending emails, the TestMailer can do this for you.
632
+
633
+ ```ruby
634
+ require 'mail'
635
+ => true
636
+ Mail.defaults do
637
+ delivery_method :test
638
+ end
639
+ => #<Mail::Configuration:0x19345a8 @delivery_method=Mail::TestMailer>
640
+ Mail::TestMailer.deliveries
641
+ => []
642
+ Mail.deliver do
643
+ to 'mikel@me.com'
644
+ from 'you@you.com'
645
+ subject 'testing'
646
+ body 'hello'
647
+ end
648
+ => #<Mail::Message:0x19284ec ...
649
+ Mail::TestMailer.deliveries.length
650
+ => 1
651
+ Mail::TestMailer.deliveries.first
652
+ => #<Mail::Message:0x19284ec ...
653
+ Mail::TestMailer.deliveries.clear
654
+ => []
655
+ ```
656
+
657
+ 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):
658
+
659
+ ```ruby
660
+ Mail.defaults do
661
+ delivery_method :test # in practice you'd do this in spec_helper.rb
662
+ end
663
+
664
+ RSpec.describe "sending an email" do
665
+ include Mail::Matchers
666
+
667
+ before(:each) do
668
+ Mail::TestMailer.deliveries.clear
669
+
670
+ Mail.deliver do
671
+ to ['mikel@me.com', 'mike2@me.com']
672
+ from 'you@you.com'
673
+ subject 'testing'
674
+ body 'hello'
675
+ end
676
+ end
677
+
678
+ it { is_expected.to have_sent_email } # passes if any email at all was sent
679
+
680
+ it { is_expected.to have_sent_email.from('you@you.com') }
681
+ it { is_expected.to have_sent_email.to('mike1@me.com') }
682
+
683
+ # can specify a list of recipients...
684
+ it { is_expected.to have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
685
+
686
+ # ...or chain recipients together
687
+ it { is_expected.to have_sent_email.to('mike1@me.com').to('mike2@me.com') }
688
+
689
+ it { is_expected.to have_sent_email.with_subject('testing') }
690
+
691
+ it { is_expected.to have_sent_email.with_body('hello') }
692
+
693
+ # Can match subject or body with a regex
694
+ # (or anything that responds_to? :match)
695
+
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/) }
698
+
699
+ # Can chain together modifiers
700
+ # Note that apart from recipients, repeating a modifier overwrites old value.
701
+
702
+ it { is_expected.to have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
703
+
704
+ # test for attachments
705
+
706
+ # ... by specific attachment
707
+ it { is_expected.to have_sent_email.with_attachments(my_attachment) }
708
+
709
+ # ... or 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')) }
717
+
718
+ # ... by array of attachments
719
+ it { is_expected.to have_sent_email.with_attachments([my_attachment1, my_attachment2]) } #note that order is important
720
+
721
+ #... by presence
722
+ it { is_expected.to have_sent_email.with_any_attachments }
723
+
724
+ #... or by absence
725
+ it { is_expected.to have_sent_email.with_no_attachments }
726
+
727
+ end
728
+ ```
729
+
730
+ ## Excerpts from TREC Spam Corpus 2005
731
+
732
+ The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
733
+ 2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
734
+ that project and license agreement. They are used in this project to verify
735
+ and describe the development of this email parser implementation.
736
+
737
+ http://plg.uwaterloo.ca/~gvcormac/treccorpus/
738
+
739
+ They are used as allowed by 'Permitted Uses, Clause 3':
740
+
741
+ "Small excerpts of the information may be displayed to others
742
+ or published in a scientific or technical context, solely for
743
+ the purpose of describing the research and development and
744
+ related issues."
745
+
746
+ -- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
747
+
748
+ ## License
749
+
750
+ (The MIT License)
751
+
752
+ Copyright (c) 2009-2016 Mikel Lindsaar
753
+
754
+ Permission is hereby granted, free of charge, to any person obtaining
755
+ a copy of this software and associated documentation files (the
756
+ 'Software'), to deal in the Software without restriction, including
757
+ without limitation the rights to use, copy, modify, merge, publish,
758
+ distribute, sublicense, and/or sell copies of the Software, and to
759
+ permit persons to whom the Software is furnished to do so, subject to
760
+ the following conditions:
761
+
762
+ The above copyright notice and this permission notice shall be
763
+ included in all copies or substantial portions of the Software.
764
+
765
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
766
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
767
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
768
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
769
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
770
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
771
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.