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.
- checksums.yaml +5 -5
- data/README.md +208 -156
- data/lib/mail/attachments_list.rb +13 -10
- data/lib/mail/body.rb +96 -107
- data/lib/mail/configuration.rb +2 -0
- data/lib/mail/constants.rb +27 -5
- data/lib/mail/elements/address.rb +61 -50
- data/lib/mail/elements/address_list.rb +11 -19
- data/lib/mail/elements/content_disposition_element.rb +9 -16
- data/lib/mail/elements/content_location_element.rb +6 -11
- data/lib/mail/elements/content_transfer_encoding_element.rb +6 -11
- data/lib/mail/elements/content_type_element.rb +16 -23
- data/lib/mail/elements/date_time_element.rb +7 -15
- data/lib/mail/elements/envelope_from_element.rb +22 -23
- data/lib/mail/elements/message_ids_element.rb +18 -13
- data/lib/mail/elements/mime_version_element.rb +7 -15
- data/lib/mail/elements/phrase_list.rb +12 -10
- data/lib/mail/elements/received_element.rb +27 -19
- data/lib/mail/encodings/7bit.rb +9 -14
- data/lib/mail/encodings/8bit.rb +2 -21
- data/lib/mail/encodings/base64.rb +11 -12
- data/lib/mail/encodings/binary.rb +3 -22
- data/lib/mail/encodings/identity.rb +24 -0
- data/lib/mail/encodings/quoted_printable.rb +6 -6
- data/lib/mail/encodings/transfer_encoding.rb +38 -29
- data/lib/mail/encodings/unix_to_unix.rb +4 -2
- data/lib/mail/encodings.rb +83 -56
- data/lib/mail/envelope.rb +11 -14
- data/lib/mail/field.rb +181 -130
- data/lib/mail/field_list.rb +61 -8
- data/lib/mail/fields/bcc_field.rb +33 -52
- data/lib/mail/fields/cc_field.rb +27 -49
- data/lib/mail/fields/comments_field.rb +26 -37
- data/lib/mail/fields/common_address_field.rb +162 -0
- data/lib/mail/fields/common_date_field.rb +56 -0
- data/lib/mail/fields/common_field.rb +77 -0
- data/lib/mail/fields/common_message_id_field.rb +41 -0
- data/lib/mail/fields/content_description_field.rb +6 -14
- data/lib/mail/fields/content_disposition_field.rb +11 -38
- data/lib/mail/fields/content_id_field.rb +23 -51
- data/lib/mail/fields/content_location_field.rb +10 -25
- data/lib/mail/fields/content_transfer_encoding_field.rb +30 -31
- data/lib/mail/fields/content_type_field.rb +53 -84
- data/lib/mail/fields/date_field.rb +22 -52
- data/lib/mail/fields/from_field.rb +27 -49
- data/lib/mail/fields/in_reply_to_field.rb +37 -49
- data/lib/mail/fields/keywords_field.rb +17 -31
- data/lib/mail/fields/message_id_field.rb +24 -71
- data/lib/mail/fields/mime_version_field.rb +18 -30
- data/lib/mail/fields/named_structured_field.rb +10 -0
- data/lib/mail/fields/named_unstructured_field.rb +10 -0
- data/lib/mail/fields/optional_field.rb +9 -8
- data/lib/mail/fields/{common/parameter_hash.rb → parameter_hash.rb} +13 -11
- data/lib/mail/fields/received_field.rb +42 -57
- data/lib/mail/fields/references_field.rb +34 -49
- data/lib/mail/fields/reply_to_field.rb +27 -49
- data/lib/mail/fields/resent_bcc_field.rb +27 -49
- data/lib/mail/fields/resent_cc_field.rb +27 -49
- data/lib/mail/fields/resent_date_field.rb +4 -30
- data/lib/mail/fields/resent_from_field.rb +27 -49
- data/lib/mail/fields/resent_message_id_field.rb +4 -29
- data/lib/mail/fields/resent_sender_field.rb +26 -56
- data/lib/mail/fields/resent_to_field.rb +27 -49
- data/lib/mail/fields/return_path_field.rb +49 -54
- data/lib/mail/fields/sender_field.rb +33 -55
- data/lib/mail/fields/structured_field.rb +2 -30
- data/lib/mail/fields/subject_field.rb +8 -11
- data/lib/mail/fields/to_field.rb +27 -49
- data/lib/mail/fields/unstructured_field.rb +31 -47
- data/lib/mail/fields.rb +9 -0
- data/lib/mail/header.rb +71 -110
- data/lib/mail/mail.rb +34 -37
- data/lib/mail/matchers/attachment_matchers.rb +15 -0
- data/lib/mail/matchers/has_sent_mail.rb +21 -1
- data/lib/mail/message.rb +126 -127
- data/lib/mail/multibyte/chars.rb +24 -181
- data/lib/mail/multibyte/unicode.rb +11 -11
- data/lib/mail/multibyte/utils.rb +26 -43
- data/lib/mail/multibyte.rb +55 -16
- data/lib/mail/network/delivery_methods/exim.rb +8 -11
- data/lib/mail/network/delivery_methods/file_delivery.rb +15 -18
- data/lib/mail/network/delivery_methods/logger_delivery.rb +34 -0
- data/lib/mail/network/delivery_methods/sendmail.rb +32 -35
- data/lib/mail/network/delivery_methods/smtp.rb +125 -68
- data/lib/mail/network/delivery_methods/smtp_connection.rb +11 -16
- data/lib/mail/network/delivery_methods/test_mailer.rb +12 -13
- data/lib/mail/network/retriever_methods/base.rb +13 -13
- data/lib/mail/network/retriever_methods/imap.rb +25 -9
- data/lib/mail/network/retriever_methods/pop3.rb +25 -23
- data/lib/mail/network/retriever_methods/test_retriever.rb +3 -2
- data/lib/mail/network.rb +1 -0
- data/lib/mail/parser_tools.rb +15 -0
- data/lib/mail/parsers/address_lists_parser.rb +33228 -116
- data/lib/mail/parsers/address_lists_parser.rl +183 -0
- data/lib/mail/parsers/content_disposition_parser.rb +885 -49
- data/lib/mail/parsers/content_disposition_parser.rl +93 -0
- data/lib/mail/parsers/content_location_parser.rb +812 -23
- data/lib/mail/parsers/content_location_parser.rl +82 -0
- data/lib/mail/parsers/content_transfer_encoding_parser.rb +512 -21
- data/lib/mail/parsers/content_transfer_encoding_parser.rl +75 -0
- data/lib/mail/parsers/content_type_parser.rb +1039 -55
- data/lib/mail/parsers/content_type_parser.rl +94 -0
- data/lib/mail/parsers/date_time_parser.rb +880 -25
- data/lib/mail/parsers/date_time_parser.rl +73 -0
- data/lib/mail/parsers/envelope_from_parser.rb +3672 -40
- data/lib/mail/parsers/envelope_from_parser.rl +93 -0
- data/lib/mail/parsers/message_ids_parser.rb +5149 -25
- data/lib/mail/parsers/message_ids_parser.rl +97 -0
- data/lib/mail/parsers/mime_version_parser.rb +500 -26
- data/lib/mail/parsers/mime_version_parser.rl +72 -0
- data/lib/mail/parsers/phrase_lists_parser.rb +873 -22
- data/lib/mail/parsers/phrase_lists_parser.rl +94 -0
- data/lib/mail/parsers/received_parser.rb +8779 -43
- data/lib/mail/parsers/received_parser.rl +95 -0
- data/lib/mail/parsers/rfc2045_content_transfer_encoding.rl +13 -0
- data/lib/mail/parsers/rfc2045_content_type.rl +25 -0
- data/lib/mail/parsers/rfc2045_mime.rl +16 -0
- data/lib/mail/parsers/rfc2183_content_disposition.rl +15 -0
- data/lib/mail/parsers/rfc3629_utf8.rl +19 -0
- data/lib/mail/parsers/rfc5234_abnf_core_rules.rl +22 -0
- data/lib/mail/parsers/rfc5322.rl +74 -0
- data/lib/mail/parsers/rfc5322_address.rl +72 -0
- data/lib/mail/parsers/{ragel/date_time.rl → rfc5322_date_time.rl} +8 -1
- data/lib/mail/parsers/rfc5322_lexical_tokens.rl +60 -0
- data/lib/mail/parsers.rb +11 -25
- data/lib/mail/part.rb +25 -29
- data/lib/mail/parts_list.rb +62 -6
- data/lib/mail/smtp_envelope.rb +57 -0
- data/lib/mail/utilities.rb +361 -74
- data/lib/mail/version.rb +2 -2
- data/lib/mail/yaml.rb +30 -0
- data/lib/mail.rb +4 -37
- metadata +125 -67
- data/CHANGELOG.rdoc +0 -787
- data/CONTRIBUTING.md +0 -60
- data/Dependencies.txt +0 -2
- data/Gemfile +0 -11
- data/Rakefile +0 -29
- data/TODO.rdoc +0 -9
- data/lib/mail/check_delivery_params.rb +0 -21
- data/lib/mail/core_extensions/smtp.rb +0 -25
- data/lib/mail/core_extensions/string/access.rb +0 -146
- data/lib/mail/core_extensions/string/multibyte.rb +0 -79
- data/lib/mail/core_extensions/string.rb +0 -21
- data/lib/mail/fields/common/address_container.rb +0 -17
- data/lib/mail/fields/common/common_address.rb +0 -136
- data/lib/mail/fields/common/common_date.rb +0 -36
- data/lib/mail/fields/common/common_field.rb +0 -61
- data/lib/mail/fields/common/common_message_id.rb +0 -49
- data/lib/mail/multibyte/exceptions.rb +0 -9
- data/lib/mail/parsers/ragel/common.rl +0 -185
- data/lib/mail/parsers/ragel/parser_info.rb +0 -61
- data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb +0 -14864
- data/lib/mail/parsers/ragel/ruby/machines/address_lists_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb +0 -751
- data/lib/mail/parsers/ragel/ruby/machines/content_disposition_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb +0 -614
- data/lib/mail/parsers/ragel/ruby/machines/content_location_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb +0 -447
- data/lib/mail/parsers/ragel/ruby/machines/content_transfer_encoding_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb +0 -825
- data/lib/mail/parsers/ragel/ruby/machines/content_type_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb +0 -817
- data/lib/mail/parsers/ragel/ruby/machines/date_time_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb +0 -2149
- data/lib/mail/parsers/ragel/ruby/machines/envelope_from_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb +0 -1570
- data/lib/mail/parsers/ragel/ruby/machines/message_ids_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb +0 -440
- data/lib/mail/parsers/ragel/ruby/machines/mime_version_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb +0 -564
- data/lib/mail/parsers/ragel/ruby/machines/phrase_lists_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/machines/rb_actions.rl +0 -51
- data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb +0 -5144
- data/lib/mail/parsers/ragel/ruby/machines/received_machine.rb.rl +0 -37
- data/lib/mail/parsers/ragel/ruby/parser.rb.rl.erb +0 -37
- data/lib/mail/parsers/ragel/ruby.rb +0 -40
- data/lib/mail/parsers/ragel.rb +0 -18
- data/lib/mail/version_specific/ruby_1_8.rb +0 -126
- data/lib/mail/version_specific/ruby_1_9.rb +0 -223
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 19bd6b0f4442e909c23a9b7da4aecd758bc7f13f49f55b0d38cbcea5d18a91bf
|
|
4
|
+
data.tar.gz: 30187e6fef5ff7d5aaa6e1608bac3f74748946b6aa84f63bc0152b14f8ec0f70
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1d0fca43599584febff0aee7cc250a9280005080a9274024fa011b99ab27e4d40ebf72161c60d7115641d844d560f34b87e07994b485c6c0ae2cbd102cd04b11
|
|
7
|
+
data.tar.gz: 47735a46f1117a0d9709a3b72db5030ea71c645418a60b8fb62637777c9cdd687079fca33f77dfc4388aa5f8d4136768a555890d1f88768b351b81aaea9ab486
|
data/README.md
CHANGED
|
@@ -1,89 +1,93 @@
|
|
|
1
|
-
Mail [](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
|
|
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
|
|
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
|
|
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.
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* Support for
|
|
75
|
-
* Support for
|
|
76
|
-
* Support for
|
|
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
|
|
77
82
|
* Wrappers for File, Net/POP3, Net/SMTP
|
|
78
|
-
* Auto
|
|
79
|
-
* Auto encoding of non US-ASCII bodies
|
|
83
|
+
* Auto-encoding of non-US-ASCII bodies and header fields
|
|
80
84
|
|
|
81
|
-
Mail is
|
|
82
|
-
|
|
83
|
-
it also is quite robust, meaning, if it finds something
|
|
84
|
-
not crash, instead, it will skip the problem and keep
|
|
85
|
-
it doesn't understand, it will initialise the header
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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.
|
|
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
|
|
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.
|
|
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
|
|
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
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
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
|
-
|
|
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
|
-
|
|
426
|
-
|
|
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
|
-
----==
|
|
433
|
-
Content-
|
|
434
|
-
|
|
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
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
Content-
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
448
|
-
|
|
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-
|
|
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,
|
|
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
|
|
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 {
|
|
678
|
+
it { is_expected.to have_sent_email } # passes if any email at all was sent
|
|
615
679
|
|
|
616
|
-
it {
|
|
617
|
-
it {
|
|
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 {
|
|
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 {
|
|
687
|
+
it { is_expected.to have_sent_email.to('mike1@me.com').to('mike2@me.com') }
|
|
624
688
|
|
|
625
|
-
it {
|
|
689
|
+
it { is_expected.to have_sent_email.with_subject('testing') }
|
|
626
690
|
|
|
627
|
-
it {
|
|
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 {
|
|
633
|
-
it {
|
|
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 {
|
|
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 {
|
|
707
|
+
it { is_expected.to have_sent_email.with_attachments(my_attachment) }
|
|
644
708
|
|
|
645
709
|
# ... or any attachment
|
|
646
|
-
it {
|
|
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 {
|
|
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 {
|
|
722
|
+
it { is_expected.to have_sent_email.with_any_attachments }
|
|
653
723
|
|
|
654
724
|
#... or by absence
|
|
655
|
-
it {
|
|
725
|
+
it { is_expected.to have_sent_email.with_no_attachments }
|
|
656
726
|
|
|
657
727
|
end
|
|
658
728
|
```
|
|
659
729
|
|
|
660
|
-
|
|
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.
|
|
10
|
-
|
|
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?(
|
|
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 =
|
|
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].
|
|
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
|
-
|
|
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 =
|
|
104
|
+
@mime_type = MiniMime.lookup_by_filename(filename)
|
|
105
|
+
@mime_type && @mime_type.content_type
|
|
103
106
|
end
|
|
104
107
|
|
|
105
108
|
end
|