otherinbox-mail 2.4.4
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +607 -0
- data/CONTRIBUTING.md +45 -0
- data/Dependencies.txt +3 -0
- data/Gemfile +26 -0
- data/Gemfile.lock +44 -0
- data/README.md +663 -0
- data/Rakefile +40 -0
- data/TODO.rdoc +9 -0
- data/lib/VERSION +4 -0
- data/lib/mail.rb +101 -0
- data/lib/mail/attachments_list.rb +104 -0
- data/lib/mail/body.rb +291 -0
- data/lib/mail/configuration.rb +75 -0
- data/lib/mail/core_extensions/nil.rb +17 -0
- data/lib/mail/core_extensions/object.rb +13 -0
- data/lib/mail/core_extensions/shell_escape.rb +56 -0
- data/lib/mail/core_extensions/smtp.rb +25 -0
- data/lib/mail/core_extensions/string.rb +33 -0
- data/lib/mail/core_extensions/string/access.rb +145 -0
- data/lib/mail/core_extensions/string/multibyte.rb +78 -0
- data/lib/mail/elements.rb +14 -0
- data/lib/mail/elements/address.rb +306 -0
- data/lib/mail/elements/address_list.rb +74 -0
- data/lib/mail/elements/content_disposition_element.rb +30 -0
- data/lib/mail/elements/content_location_element.rb +25 -0
- data/lib/mail/elements/content_transfer_encoding_element.rb +24 -0
- data/lib/mail/elements/content_type_element.rb +35 -0
- data/lib/mail/elements/date_time_element.rb +26 -0
- data/lib/mail/elements/envelope_from_element.rb +34 -0
- data/lib/mail/elements/message_ids_element.rb +29 -0
- data/lib/mail/elements/mime_version_element.rb +26 -0
- data/lib/mail/elements/phrase_list.rb +21 -0
- data/lib/mail/elements/received_element.rb +30 -0
- data/lib/mail/encodings.rb +274 -0
- data/lib/mail/encodings/7bit.rb +31 -0
- data/lib/mail/encodings/8bit.rb +31 -0
- data/lib/mail/encodings/base64.rb +33 -0
- data/lib/mail/encodings/binary.rb +31 -0
- data/lib/mail/encodings/quoted_printable.rb +38 -0
- data/lib/mail/encodings/transfer_encoding.rb +58 -0
- data/lib/mail/envelope.rb +35 -0
- data/lib/mail/field.rb +234 -0
- data/lib/mail/field_list.rb +33 -0
- data/lib/mail/fields.rb +35 -0
- data/lib/mail/fields/bcc_field.rb +56 -0
- data/lib/mail/fields/cc_field.rb +55 -0
- data/lib/mail/fields/comments_field.rb +41 -0
- data/lib/mail/fields/common/address_container.rb +16 -0
- data/lib/mail/fields/common/common_address.rb +125 -0
- data/lib/mail/fields/common/common_date.rb +42 -0
- data/lib/mail/fields/common/common_field.rb +51 -0
- data/lib/mail/fields/common/common_message_id.rb +44 -0
- data/lib/mail/fields/common/parameter_hash.rb +58 -0
- data/lib/mail/fields/content_description_field.rb +19 -0
- data/lib/mail/fields/content_disposition_field.rb +69 -0
- data/lib/mail/fields/content_id_field.rb +63 -0
- data/lib/mail/fields/content_location_field.rb +42 -0
- data/lib/mail/fields/content_transfer_encoding_field.rb +50 -0
- data/lib/mail/fields/content_type_field.rb +198 -0
- data/lib/mail/fields/date_field.rb +57 -0
- data/lib/mail/fields/from_field.rb +55 -0
- data/lib/mail/fields/in_reply_to_field.rb +55 -0
- data/lib/mail/fields/keywords_field.rb +44 -0
- data/lib/mail/fields/message_id_field.rb +83 -0
- data/lib/mail/fields/mime_version_field.rb +53 -0
- data/lib/mail/fields/optional_field.rb +13 -0
- data/lib/mail/fields/received_field.rb +75 -0
- data/lib/mail/fields/references_field.rb +55 -0
- data/lib/mail/fields/reply_to_field.rb +55 -0
- data/lib/mail/fields/resent_bcc_field.rb +55 -0
- data/lib/mail/fields/resent_cc_field.rb +55 -0
- data/lib/mail/fields/resent_date_field.rb +35 -0
- data/lib/mail/fields/resent_from_field.rb +55 -0
- data/lib/mail/fields/resent_message_id_field.rb +34 -0
- data/lib/mail/fields/resent_sender_field.rb +62 -0
- data/lib/mail/fields/resent_to_field.rb +55 -0
- data/lib/mail/fields/return_path_field.rb +65 -0
- data/lib/mail/fields/sender_field.rb +67 -0
- data/lib/mail/fields/structured_field.rb +51 -0
- data/lib/mail/fields/subject_field.rb +16 -0
- data/lib/mail/fields/to_field.rb +55 -0
- data/lib/mail/fields/unstructured_field.rb +191 -0
- data/lib/mail/header.rb +265 -0
- data/lib/mail/indifferent_hash.rb +146 -0
- data/lib/mail/mail.rb +255 -0
- data/lib/mail/matchers/has_sent_mail.rb +124 -0
- data/lib/mail/message.rb +2059 -0
- data/lib/mail/multibyte.rb +42 -0
- data/lib/mail/multibyte/chars.rb +474 -0
- data/lib/mail/multibyte/exceptions.rb +8 -0
- data/lib/mail/multibyte/unicode.rb +392 -0
- data/lib/mail/multibyte/utils.rb +60 -0
- data/lib/mail/network.rb +14 -0
- data/lib/mail/network/delivery_methods/exim.rb +53 -0
- data/lib/mail/network/delivery_methods/file_delivery.rb +40 -0
- data/lib/mail/network/delivery_methods/sendmail.rb +62 -0
- data/lib/mail/network/delivery_methods/smtp.rb +153 -0
- data/lib/mail/network/delivery_methods/smtp_connection.rb +74 -0
- data/lib/mail/network/delivery_methods/test_mailer.rb +40 -0
- data/lib/mail/network/retriever_methods/base.rb +63 -0
- data/lib/mail/network/retriever_methods/imap.rb +168 -0
- data/lib/mail/network/retriever_methods/pop3.rb +140 -0
- data/lib/mail/network/retriever_methods/test_retriever.rb +47 -0
- data/lib/mail/parsers/address_lists.rb +64 -0
- data/lib/mail/parsers/address_lists.treetop +19 -0
- data/lib/mail/parsers/content_disposition.rb +535 -0
- data/lib/mail/parsers/content_disposition.treetop +46 -0
- data/lib/mail/parsers/content_location.rb +139 -0
- data/lib/mail/parsers/content_location.treetop +20 -0
- data/lib/mail/parsers/content_transfer_encoding.rb +162 -0
- data/lib/mail/parsers/content_transfer_encoding.treetop +20 -0
- data/lib/mail/parsers/content_type.rb +967 -0
- data/lib/mail/parsers/content_type.treetop +68 -0
- data/lib/mail/parsers/date_time.rb +114 -0
- data/lib/mail/parsers/date_time.treetop +11 -0
- data/lib/mail/parsers/envelope_from.rb +194 -0
- data/lib/mail/parsers/envelope_from.treetop +32 -0
- data/lib/mail/parsers/message_ids.rb +45 -0
- data/lib/mail/parsers/message_ids.treetop +15 -0
- data/lib/mail/parsers/mime_version.rb +144 -0
- data/lib/mail/parsers/mime_version.treetop +19 -0
- data/lib/mail/parsers/phrase_lists.rb +45 -0
- data/lib/mail/parsers/phrase_lists.treetop +15 -0
- data/lib/mail/parsers/received.rb +71 -0
- data/lib/mail/parsers/received.treetop +11 -0
- data/lib/mail/parsers/rfc2045.rb +464 -0
- data/lib/mail/parsers/rfc2045.treetop +36 -0
- data/lib/mail/parsers/rfc2822.rb +5341 -0
- data/lib/mail/parsers/rfc2822.treetop +410 -0
- data/lib/mail/parsers/rfc2822_obsolete.rb +3768 -0
- data/lib/mail/parsers/rfc2822_obsolete.treetop +241 -0
- data/lib/mail/part.rb +116 -0
- data/lib/mail/parts_list.rb +55 -0
- data/lib/mail/patterns.rb +34 -0
- data/lib/mail/utilities.rb +215 -0
- data/lib/mail/version.rb +24 -0
- data/lib/mail/version_specific/ruby_1_8.rb +98 -0
- data/lib/mail/version_specific/ruby_1_9.rb +113 -0
- data/lib/tasks/corpus.rake +125 -0
- data/lib/tasks/treetop.rake +10 -0
- metadata +253 -0
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
Contributing to Mail
|
2
|
+
====================
|
3
|
+
|
4
|
+
Hi there, I welcome pull requests! Here are some thoughts on how to get your
|
5
|
+
pull request merged quickly:
|
6
|
+
|
7
|
+
1. Check the Reference RFCs, they are in the References directory, so no excuses.
|
8
|
+
2. Check for a ticket on GitHub, maybe someone else has the problem too
|
9
|
+
3. Make a fork of my GitHub repository
|
10
|
+
4. Run the specs. We only take pull requests with passing tests, and it's great
|
11
|
+
to know that you have a clean slate: `bundle && bundle exec rake`
|
12
|
+
5. Add a spec for your change. Only refactoring and documentation changes
|
13
|
+
require no new specs. If you are adding functionality or fixing a bug, we need
|
14
|
+
a spec!
|
15
|
+
6. Test the spec _at_ _least_ against MRI-1.9.2 and MRI-1.8.7
|
16
|
+
7. Update the README if needed to reflect your change / addition
|
17
|
+
8. With all specs passing push your changes back to your fork
|
18
|
+
9. Send me a pull request
|
19
|
+
|
20
|
+
Note, specs that break MRI 1.8.7 will not be accepted.
|
21
|
+
|
22
|
+
At this point you're waiting on us. We like to at least comment on, if not
|
23
|
+
accept, pull requests within three business days (and, typically, one business
|
24
|
+
day). We may suggest some changes or improvements or alternatives.
|
25
|
+
|
26
|
+
Some things that will increase the chance that your pull request is accepted,
|
27
|
+
taken straight from the Ruby on Rails guide:
|
28
|
+
|
29
|
+
* Tell me you have tested it against more than one version of Ruby, RVM is great for
|
30
|
+
this. I test against 7 rubies before I push into master.
|
31
|
+
* Use good, idiomatic, strcutred and modular code
|
32
|
+
* Include tests that fail without your code, and pass with it
|
33
|
+
* Update the documentation, the surrounding one, examples elsewhere, guides,
|
34
|
+
whatever is affected by your contribution
|
35
|
+
|
36
|
+
Syntax:
|
37
|
+
|
38
|
+
* Two spaces, no tabs.
|
39
|
+
* No trailing whitespace. Blank lines should not have any space.
|
40
|
+
* Prefer &&/|| over and/or.
|
41
|
+
* MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
42
|
+
* a = b and not a=b.
|
43
|
+
* Follow the conventions you see used in the source already.
|
44
|
+
|
45
|
+
And in case we didn't emphasize it enough: we love specs!
|
data/Dependencies.txt
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
source :rubygems
|
2
|
+
|
3
|
+
gem "activesupport", ">= 2.3.6"
|
4
|
+
gem "tlsmail" if RUBY_VERSION <= '1.8.6'
|
5
|
+
gem "mime-types", "~> 1.16"
|
6
|
+
gem "treetop", "~> 1.4.10"
|
7
|
+
gem "i18n", ">= 0.4.0"
|
8
|
+
|
9
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
|
10
|
+
gem 'jruby-openssl'
|
11
|
+
end
|
12
|
+
|
13
|
+
group :test do
|
14
|
+
gem "rake", "> 0.8.7"
|
15
|
+
gem "rspec", "~> 2.8.0"
|
16
|
+
case
|
17
|
+
when defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
|
18
|
+
# Skip it
|
19
|
+
when RUBY_PLATFORM == 'java'
|
20
|
+
# Skip it
|
21
|
+
when RUBY_VERSION < '1.9'
|
22
|
+
gem "ruby-debug"
|
23
|
+
else
|
24
|
+
# Skip it
|
25
|
+
end
|
26
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.2.5)
|
5
|
+
i18n (~> 0.6)
|
6
|
+
multi_json (~> 1.0)
|
7
|
+
columnize (0.3.6)
|
8
|
+
diff-lcs (1.1.3)
|
9
|
+
i18n (0.6.0)
|
10
|
+
linecache (0.46)
|
11
|
+
rbx-require-relative (> 0.0.4)
|
12
|
+
mime-types (1.18)
|
13
|
+
multi_json (1.3.6)
|
14
|
+
polyglot (0.3.3)
|
15
|
+
rake (0.9.2.2)
|
16
|
+
rbx-require-relative (0.0.9)
|
17
|
+
rspec (2.8.0)
|
18
|
+
rspec-core (~> 2.8.0)
|
19
|
+
rspec-expectations (~> 2.8.0)
|
20
|
+
rspec-mocks (~> 2.8.0)
|
21
|
+
rspec-core (2.8.0)
|
22
|
+
rspec-expectations (2.8.0)
|
23
|
+
diff-lcs (~> 1.1.2)
|
24
|
+
rspec-mocks (2.8.0)
|
25
|
+
ruby-debug (0.10.4)
|
26
|
+
columnize (>= 0.1)
|
27
|
+
ruby-debug-base (~> 0.10.4.0)
|
28
|
+
ruby-debug-base (0.10.4)
|
29
|
+
linecache (>= 0.3)
|
30
|
+
treetop (1.4.10)
|
31
|
+
polyglot
|
32
|
+
polyglot (>= 0.3.1)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
activesupport (>= 2.3.6)
|
39
|
+
i18n (>= 0.4.0)
|
40
|
+
mime-types (~> 1.16)
|
41
|
+
rake (> 0.8.7)
|
42
|
+
rspec (~> 2.8.0)
|
43
|
+
ruby-debug
|
44
|
+
treetop (~> 1.4.10)
|
data/README.md
ADDED
@@ -0,0 +1,663 @@
|
|
1
|
+
Mail
|
2
|
+
====
|
3
|
+
|
4
|
+
Introduction
|
5
|
+
------------
|
6
|
+
|
7
|
+
Mail is an internet library for Ruby that is designed to handle emails
|
8
|
+
generation, parsing and sending in a simple, rubyesque manner.
|
9
|
+
|
10
|
+
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
|
12
|
+
type actions are done through proxy methods to Net::SMTP, Net::POP3 etc.
|
13
|
+
|
14
|
+
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
|
16
|
+
brainer.
|
17
|
+
|
18
|
+
It is also designed form the ground up to work with Ruby 1.9. This is because
|
19
|
+
Ruby 1.9 handles text encodings much more magically than Ruby 1.8.x and so
|
20
|
+
these features have been taken full advantage of in this library allowing
|
21
|
+
Mail to handle a lot more messages more cleanly than TMail. Mail does run on
|
22
|
+
Ruby 1.8.x... it's just not as fun to code.
|
23
|
+
|
24
|
+
Finally, Mail has been designed with a very simple object oriented system
|
25
|
+
that really opens up the email messages you are parsing, if you know what
|
26
|
+
you are doing, you can fiddle with every last bit of your email directly.
|
27
|
+
|
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 is tested by Travis (![Travis Build Status](https://secure.travis-ci.org/mikel/mail.png "Build Status")) and works on the following platforms:
|
46
|
+
|
47
|
+
* jruby-1.6.5.1 [ x86_64 ]
|
48
|
+
* rbx-head-d18 [ x86_64 ]
|
49
|
+
* ree-1.8.7-2011.03 [ i686 ]
|
50
|
+
* ruby-1.8.7-p357 [ i686 ]
|
51
|
+
* ruby-1.9.2-p290 [ x86_64 ]
|
52
|
+
* ruby-1.9.3-p0 [ x86_64 ]
|
53
|
+
|
54
|
+
Discussion
|
55
|
+
----------
|
56
|
+
|
57
|
+
If you want to discuss mail with like minded individuals, please subscribe to
|
58
|
+
the [Google Group](http://groups.google.com/group/mail-ruby).
|
59
|
+
|
60
|
+
Current Capabilities of Mail
|
61
|
+
----------------------------
|
62
|
+
|
63
|
+
* RFC2822 Support, Reading and Writing
|
64
|
+
* RFC2045-2049 Support for multipart emails
|
65
|
+
* Support for creating multipart alternate emails
|
66
|
+
* Support for reading multipart/report emails & getting details from such
|
67
|
+
* Support for multibyte emails - needs quite a lot of work and testing
|
68
|
+
* Wrappers for File, Net/POP3, Net/SMTP
|
69
|
+
* Auto encoding of non US-ASCII header fields
|
70
|
+
* Auto encoding of non US-ASCII bodies
|
71
|
+
|
72
|
+
Mail is RFC2822 compliant now, that is, it can parse and generate valid US-ASCII
|
73
|
+
emails. There are a few obsoleted syntax emails that it will have problems with, but
|
74
|
+
it also is quite robust, meaning, if it finds something it doesn't understand it will
|
75
|
+
not crash, instead, it will skip the problem and keep parsing. In the case of a header
|
76
|
+
it doesn't understand, it will initialise the header as an optional unstructured
|
77
|
+
field and continue parsing.
|
78
|
+
|
79
|
+
This means Mail won't (ever) crunch your data (I think).
|
80
|
+
|
81
|
+
You can also create MIME emails. There are helper methods for making a
|
82
|
+
multipart/alternate email for text/plain and text/html (the most common pair)
|
83
|
+
and you can manually create any other type of MIME email.
|
84
|
+
|
85
|
+
Roadmap
|
86
|
+
-------
|
87
|
+
|
88
|
+
Next TODO:
|
89
|
+
|
90
|
+
* Improve MIME support for character sets in headers, currently works, mostly, needs
|
91
|
+
refinement.
|
92
|
+
|
93
|
+
Testing Policy
|
94
|
+
--------------
|
95
|
+
|
96
|
+
Basically... we do BDD on Mail. No method gets written in Mail without a
|
97
|
+
corresponding or covering spec. We expect as a minimum 100% coverage
|
98
|
+
measured by RCov. While this is not perfect by any measure, it is pretty
|
99
|
+
good. Additionally, all functional tests from TMail are to be passing before
|
100
|
+
the gem gets released.
|
101
|
+
|
102
|
+
It also means you can be sure Mail will behave correctly.
|
103
|
+
|
104
|
+
API Policy
|
105
|
+
----------
|
106
|
+
|
107
|
+
No API removals within a single point release. All removals to be deprecated with
|
108
|
+
warnings for at least one MINOR point release before removal.
|
109
|
+
|
110
|
+
Also, all private or protected methods to be declared as such - though this is still I/P.
|
111
|
+
|
112
|
+
Installation
|
113
|
+
------------
|
114
|
+
|
115
|
+
Installation is fairly simple, I host mail on rubygems, so you can just do:
|
116
|
+
|
117
|
+
# gem install mail
|
118
|
+
|
119
|
+
Encodings
|
120
|
+
---------
|
121
|
+
|
122
|
+
If you didn't know, handling encodings in Emails is not as straight forward as you
|
123
|
+
would hope.
|
124
|
+
|
125
|
+
I have tried to simplify it some:
|
126
|
+
|
127
|
+
1. All objects that can render into an email, have an `#encoded` method. Encoded will
|
128
|
+
return the object as a complete string ready to send in the mail system, that is,
|
129
|
+
it will include the header field and value and CRLF at the end and wrapped as
|
130
|
+
needed.
|
131
|
+
|
132
|
+
2. All objects that can render into an email, have a :decoded method. Decoded will
|
133
|
+
return the object's "value" only as a string. This means it will not include
|
134
|
+
the header fields (like 'To:' or 'Subject:').
|
135
|
+
|
136
|
+
3. By default, calling <code>#to_s</code> on a container object will call its encoded
|
137
|
+
method, while <code>#to_s</code> on a field object will call it's decoded method.
|
138
|
+
So calling <code>#to_s</code> on a Mail object will return the mail, all encoded
|
139
|
+
ready to send, while calling <code>#to_s</code> on the From field or the body will
|
140
|
+
return the decoded value of the object. The header object of Mail is considered a
|
141
|
+
container. If you are in doubt, call <code>#encoded</code>, or <code>#decoded</code>
|
142
|
+
explicitly, this is safer if you are not sure.
|
143
|
+
|
144
|
+
4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
|
145
|
+
provide decoded parameter values when you call the parameter names as methods against
|
146
|
+
the object.
|
147
|
+
|
148
|
+
5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
|
149
|
+
provide encoded parameter values when you call the parameter names through the
|
150
|
+
<code>object.parameters['<parameter_name>']</code> method call.
|
151
|
+
|
152
|
+
Contributing
|
153
|
+
------------
|
154
|
+
|
155
|
+
Please do! Contributing is easy in Mail. Please read the CONTRIBUTING.md document for more info
|
156
|
+
|
157
|
+
Usage
|
158
|
+
-----
|
159
|
+
|
160
|
+
All major mail functions should be able to happen from the Mail module.
|
161
|
+
So, you should be able to just <code>require 'mail'</code> to get started.
|
162
|
+
|
163
|
+
### Making an email
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
mail = Mail.new do
|
167
|
+
from 'mikel@test.lindsaar.net'
|
168
|
+
to 'you@test.lindsaar.net'
|
169
|
+
subject 'This is a test email'
|
170
|
+
body File.read('body.txt')
|
171
|
+
end
|
172
|
+
|
173
|
+
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
|
174
|
+
```
|
175
|
+
|
176
|
+
### Making an email, have it your way:
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
mail = Mail.new do
|
180
|
+
body File.read('body.txt')
|
181
|
+
end
|
182
|
+
|
183
|
+
mail['from'] = 'mikel@test.lindsaar.net'
|
184
|
+
mail[:to] = 'you@test.lindsaar.net'
|
185
|
+
mail.subject = 'This is a test email'
|
186
|
+
|
187
|
+
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
|
188
|
+
```
|
189
|
+
|
190
|
+
### Don't Worry About Message IDs:
|
191
|
+
|
192
|
+
```ruby
|
193
|
+
mail = Mail.new do
|
194
|
+
to 'you@test.lindsaar.net'
|
195
|
+
body 'Some simple body'
|
196
|
+
end
|
197
|
+
|
198
|
+
mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
|
199
|
+
```
|
200
|
+
|
201
|
+
Mail will automatically add a Message-ID field if it is missing and
|
202
|
+
give it a unique, random Message-ID along the lines of:
|
203
|
+
|
204
|
+
<4a7ff76d7016_13a81ab802e1@local.fqdn.mail>
|
205
|
+
|
206
|
+
### Or do worry about Message-IDs:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
mail = Mail.new do
|
210
|
+
to 'you@test.lindsaar.net'
|
211
|
+
message_id '<ThisIsMyMessageId@some.domain.com>'
|
212
|
+
body 'Some simple body'
|
213
|
+
end
|
214
|
+
|
215
|
+
mail.to_s =~ /Message\-ID: <ThisIsMyMessageId@some.domain.com>/ #=> 27
|
216
|
+
```
|
217
|
+
|
218
|
+
Mail will take the message_id you assign to it trusting that you know
|
219
|
+
what you are doing.
|
220
|
+
|
221
|
+
### Sending an email:
|
222
|
+
|
223
|
+
Mail defaults to sending via SMTP to local host port 25. If you have a
|
224
|
+
sendmail or postfix daemon running on on this port, sending email is as
|
225
|
+
easy as:
|
226
|
+
|
227
|
+
```ruby
|
228
|
+
Mail.deliver do
|
229
|
+
from 'me@test.lindsaar.net'
|
230
|
+
to 'you@test.lindsaar.net'
|
231
|
+
subject 'Here is the image you wanted'
|
232
|
+
body File.read('body.txt')
|
233
|
+
add_file '/full/path/to/somefile.png'
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
or
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
mail = Mail.new do
|
241
|
+
from 'me@test.lindsaar.net'
|
242
|
+
to 'you@test.lindsaar.net'
|
243
|
+
subject 'Here is the image you wanted'
|
244
|
+
body File.read('body.txt')
|
245
|
+
add_file :filename => 'somefile.png', :content => File.read('/somefile.png')
|
246
|
+
end
|
247
|
+
|
248
|
+
mail.deliver!
|
249
|
+
```
|
250
|
+
|
251
|
+
Sending via sendmail can be done like so:
|
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.delivery_method :sendmail
|
263
|
+
|
264
|
+
mail.deliver
|
265
|
+
```
|
266
|
+
|
267
|
+
Exim requires it's own delivery manager, and can be used like so:
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
mail.delivery_method :exim, :location => "/usr/bin/exim"
|
271
|
+
|
272
|
+
mail.deliver
|
273
|
+
```
|
274
|
+
|
275
|
+
### Getting emails from a pop server:
|
276
|
+
|
277
|
+
You can configure Mail to receive email using <code>retriever_method</code>
|
278
|
+
within <code>Mail.defaults</code>:
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
Mail.defaults do
|
282
|
+
retriever_method :pop3, :address => "pop.gmail.com",
|
283
|
+
:port => 995,
|
284
|
+
:user_name => '<username>',
|
285
|
+
:password => '<password>',
|
286
|
+
:enable_ssl => true
|
287
|
+
end
|
288
|
+
```
|
289
|
+
|
290
|
+
You can access incoming email in a number of ways.
|
291
|
+
|
292
|
+
The most recent email:
|
293
|
+
|
294
|
+
```ruby
|
295
|
+
Mail.all #=> Returns an array of all emails
|
296
|
+
Mail.first #=> Returns the first unread email
|
297
|
+
Mail.last #=> Returns the first unread email
|
298
|
+
```
|
299
|
+
|
300
|
+
The first 10 emails sorted by date in ascending order:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
emails = Mail.find(:what => :first, :count => 10, :order => :asc)
|
304
|
+
emails.length #=> 10
|
305
|
+
```
|
306
|
+
|
307
|
+
Or even all emails:
|
308
|
+
|
309
|
+
```ruby
|
310
|
+
emails = Mail.all
|
311
|
+
emails.length #=> LOTS!
|
312
|
+
```
|
313
|
+
|
314
|
+
|
315
|
+
### Reading an Email
|
316
|
+
|
317
|
+
```ruby
|
318
|
+
mail = Mail.read('/path/to/message.eml')
|
319
|
+
|
320
|
+
mail.envelope.from #=> 'mikel@test.lindsaar.net'
|
321
|
+
mail.from.addresses #=> ['mikel@test.lindsaar.net', 'ada@test.lindsaar.net']
|
322
|
+
mail.sender.address #=> 'mikel@test.lindsaar.net'
|
323
|
+
mail.to #=> 'bob@test.lindsaar.net'
|
324
|
+
mail.cc #=> 'sam@test.lindsaar.net'
|
325
|
+
mail.subject #=> "This is the subject"
|
326
|
+
mail.date.to_s #=> '21 Nov 1997 09:55:06 -0600'
|
327
|
+
mail.message_id #=> '<4D6AA7EB.6490534@xxx.xxx>'
|
328
|
+
mail.body.decoded #=> 'This is the body of the email...
|
329
|
+
```
|
330
|
+
|
331
|
+
Many more methods available.
|
332
|
+
|
333
|
+
### Reading a Multipart Email
|
334
|
+
|
335
|
+
```ruby
|
336
|
+
mail = Mail.read('multipart_email')
|
337
|
+
|
338
|
+
mail.multipart? #=> true
|
339
|
+
mail.parts.length #=> 2
|
340
|
+
mail.preamble #=> "Text before the first part"
|
341
|
+
mail.epilogue #=> "Text after the last part"
|
342
|
+
mail.parts.map { |p| p.content_type } #=> ['text/plain', 'application/pdf']
|
343
|
+
mail.parts.map { |p| p.class } #=> [Mail::Message, Mail::Message]
|
344
|
+
mail.parts[0].content_type_parameters #=> {'charset' => 'ISO-8859-1'}
|
345
|
+
mail.parts[1].content_type_parameters #=> {'name' => 'my.pdf'}
|
346
|
+
```
|
347
|
+
|
348
|
+
Mail generates a tree of parts. Each message has many or no parts. Each part
|
349
|
+
is another message which can have many or no parts.
|
350
|
+
|
351
|
+
A message will only have parts if it is a multipart/mixed or related/mixed
|
352
|
+
content type and has a boundary defined.
|
353
|
+
|
354
|
+
### Testing and extracting attachments
|
355
|
+
```ruby
|
356
|
+
mail.attachments.each do | attachment |
|
357
|
+
# Attachments is an AttachmentsList object containing a
|
358
|
+
# number of Part objects
|
359
|
+
if (attachment.content_type.start_with?('image/'))
|
360
|
+
# extracting images for example...
|
361
|
+
filename = attachment.filename
|
362
|
+
begin
|
363
|
+
File.open(images_dir + filename, "w+b", 0644) {|f| f.write attachment.body.decoded}
|
364
|
+
rescue Exception => e
|
365
|
+
puts "Unable to save data for #{filename} because #{e.message}"
|
366
|
+
end
|
367
|
+
end
|
368
|
+
end
|
369
|
+
```
|
370
|
+
### Writing and sending a multipart/alternative (html and text) email
|
371
|
+
|
372
|
+
Mail makes some basic assumptions and makes doing the common thing as
|
373
|
+
simple as possible.... (asking a lot from a mail library)
|
374
|
+
|
375
|
+
```ruby
|
376
|
+
mail = Mail.deliver do
|
377
|
+
to 'nicolas@test.lindsaar.net.au'
|
378
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
379
|
+
subject 'First multipart email sent with Mail'
|
380
|
+
|
381
|
+
text_part do
|
382
|
+
body 'This is plain text'
|
383
|
+
end
|
384
|
+
|
385
|
+
html_part do
|
386
|
+
content_type 'text/html; charset=UTF-8'
|
387
|
+
body '<h1>This is HTML</h1>'
|
388
|
+
end
|
389
|
+
end
|
390
|
+
```
|
391
|
+
|
392
|
+
Mail then delivers the email at the end of the block and returns the
|
393
|
+
resulting Mail::Message object, which you can then inspect if you
|
394
|
+
so desire...
|
395
|
+
|
396
|
+
```
|
397
|
+
puts mail.to_s #=>
|
398
|
+
|
399
|
+
To: nicolas@test.lindsaar.net.au
|
400
|
+
From: Mikel Lindsaar <mikel@test.lindsaar.net.au>
|
401
|
+
Subject: First multipart email sent with Mail
|
402
|
+
Content-Type: multipart/alternative;
|
403
|
+
boundary=--==_mimepart_4a914f0c911be_6f0f1ab8026659
|
404
|
+
Message-ID: <4a914f12ac7e_6f0f1ab80267d1@baci.local.mail>
|
405
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
406
|
+
Mime-Version: 1.0
|
407
|
+
Content-Transfer-Encoding: 7bit
|
408
|
+
|
409
|
+
|
410
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659
|
411
|
+
Content-ID: <4a914f12c8c4_6f0f1ab80268d6@baci.local.mail>
|
412
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
413
|
+
Mime-Version: 1.0
|
414
|
+
Content-Type: text/plain
|
415
|
+
Content-Transfer-Encoding: 7bit
|
416
|
+
|
417
|
+
This is plain text
|
418
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659
|
419
|
+
Content-Type: text/html; charset=UTF-8
|
420
|
+
Content-ID: <4a914f12cf86_6f0f1ab802692c@baci.local.mail>
|
421
|
+
Date: Mon, 24 Aug 2009 00:15:46 +1000
|
422
|
+
Mime-Version: 1.0
|
423
|
+
Content-Transfer-Encoding: 7bit
|
424
|
+
|
425
|
+
<h1>This is HTML</h1>
|
426
|
+
----==_mimepart_4a914f0c911be_6f0f1ab8026659--
|
427
|
+
```
|
428
|
+
|
429
|
+
Mail inserts the content transfer encoding, the mime version,
|
430
|
+
the content-id's and handles the content-type and boundary.
|
431
|
+
|
432
|
+
Mail assumes that if your text in the body is only us-ascii, that your
|
433
|
+
transfer encoding is 7bit and it is text/plain. You can override this
|
434
|
+
by explicitly declaring it.
|
435
|
+
|
436
|
+
### Making Multipart/Alternate, without a block
|
437
|
+
|
438
|
+
You don't have to use a block with the text and html part included, you
|
439
|
+
can just do it declaratively. However, you need to add Mail::Parts to
|
440
|
+
an email, not Mail::Messages.
|
441
|
+
|
442
|
+
```ruby
|
443
|
+
mail = Mail.new do
|
444
|
+
to 'nicolas@test.lindsaar.net.au'
|
445
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
446
|
+
subject 'First multipart email sent with Mail'
|
447
|
+
end
|
448
|
+
|
449
|
+
text_part = Mail::Part.new do
|
450
|
+
body 'This is plain text'
|
451
|
+
end
|
452
|
+
|
453
|
+
html_part = Mail::Part.new do
|
454
|
+
content_type 'text/html; charset=UTF-8'
|
455
|
+
body '<h1>This is HTML</h1>'
|
456
|
+
end
|
457
|
+
|
458
|
+
mail.text_part = text_part
|
459
|
+
mail.html_part = html_part
|
460
|
+
```
|
461
|
+
|
462
|
+
Results in the same email as done using the block form
|
463
|
+
|
464
|
+
### Getting error reports from an email:
|
465
|
+
|
466
|
+
```ruby
|
467
|
+
@mail = Mail.read('/path/to/bounce_message.eml')
|
468
|
+
|
469
|
+
@mail.bounced? #=> true
|
470
|
+
@mail.final_recipient #=> rfc822;mikel@dont.exist.com
|
471
|
+
@mail.action #=> failed
|
472
|
+
@mail.error_status #=> 5.5.0
|
473
|
+
@mail.diagnostic_code #=> smtp;550 Requested action not taken: mailbox unavailable
|
474
|
+
@mail.retryable? #=> false
|
475
|
+
```
|
476
|
+
|
477
|
+
### Attaching and Detaching Files
|
478
|
+
|
479
|
+
You can just read the file off an absolute path, Mail will try
|
480
|
+
to guess the mime_type and will encode the file in Base64 for you.
|
481
|
+
|
482
|
+
```ruby
|
483
|
+
@mail = Mail.new
|
484
|
+
@mail.add_file("/path/to/file.jpg")
|
485
|
+
@mail.parts.first.attachment? #=> true
|
486
|
+
@mail.parts.first.content_transfer_encoding.to_s #=> 'base64'
|
487
|
+
@mail.attachments.first.mime_type #=> 'image/jpg'
|
488
|
+
@mail.attachments.first.filename #=> 'file.jpg'
|
489
|
+
@mail.attachments.first.decoded == File.read('/path/to/file.jpg') #=> true
|
490
|
+
```
|
491
|
+
|
492
|
+
Or You can pass in file_data and give it a filename, again, mail
|
493
|
+
will try and guess the mime_type for you.
|
494
|
+
|
495
|
+
```ruby
|
496
|
+
@mail = Mail.new
|
497
|
+
@mail.attachments['myfile.pdf'] = File.read('path/to/myfile.pdf')
|
498
|
+
@mail.parts.first.attachment? #=> true
|
499
|
+
@mail.attachments.first.mime_type #=> 'application/pdf'
|
500
|
+
@mail.attachments.first.decoded == File.read('path/to/myfile.pdf') #=> true
|
501
|
+
```
|
502
|
+
|
503
|
+
You can also override the guessed MIME media type if you really know better
|
504
|
+
than mail (this should be rarely needed)
|
505
|
+
|
506
|
+
```ruby
|
507
|
+
@mail = Mail.new
|
508
|
+
file_data = File.read('path/to/myfile.pdf')
|
509
|
+
@mail.attachments['myfile.pdf'] = { :mime_type => 'application/x-pdf',
|
510
|
+
:content => File.read('path/to/myfile.pdf') }
|
511
|
+
@mail.parts.first.mime_type #=> 'application/x-pdf'
|
512
|
+
```
|
513
|
+
|
514
|
+
Of course... Mail will round trip an attachment as well
|
515
|
+
|
516
|
+
```ruby
|
517
|
+
@mail = Mail.new do
|
518
|
+
to 'nicolas@test.lindsaar.net.au'
|
519
|
+
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
|
520
|
+
subject 'First multipart email sent with Mail'
|
521
|
+
|
522
|
+
text_part do
|
523
|
+
body 'Here is the attachment you wanted'
|
524
|
+
end
|
525
|
+
|
526
|
+
html_part do
|
527
|
+
content_type 'text/html; charset=UTF-8'
|
528
|
+
body '<h1>Funky Title</h1><p>Here is the attachment you wanted</p>'
|
529
|
+
end
|
530
|
+
|
531
|
+
add_file '/path/to/myfile.pdf'
|
532
|
+
end
|
533
|
+
|
534
|
+
@round_tripped_mail = Mail.new(@mail.encoded)
|
535
|
+
|
536
|
+
@round_tripped_mail.attachments.length #=> 1
|
537
|
+
@round_tripped_mail.attachments.first.filename #=> 'myfile.pdf'
|
538
|
+
```
|
539
|
+
See "Testing and extracting attachments" above for more details.
|
540
|
+
|
541
|
+
Using Mail with Testing or Spec'ing Libraries
|
542
|
+
---------------------------------------------
|
543
|
+
|
544
|
+
If mail is part of your system, you'll need a way to test it without actually
|
545
|
+
sending emails, the TestMailer can do this for you.
|
546
|
+
|
547
|
+
```
|
548
|
+
require 'mail'
|
549
|
+
=> true
|
550
|
+
Mail.defaults do
|
551
|
+
delivery_method :test
|
552
|
+
end
|
553
|
+
=> #<Mail::Configuration:0x19345a8 @delivery_method=Mail::TestMailer>
|
554
|
+
Mail::TestMailer.deliveries
|
555
|
+
=> []
|
556
|
+
Mail.deliver do
|
557
|
+
to 'mikel@me.com'
|
558
|
+
from 'you@you.com'
|
559
|
+
subject 'testing'
|
560
|
+
body 'hello'
|
561
|
+
end
|
562
|
+
=> #<Mail::Message:0x19284ec ...
|
563
|
+
Mail::TestMailer.deliveries.length
|
564
|
+
=> 1
|
565
|
+
Mail::TestMailer.deliveries.first
|
566
|
+
=> #<Mail::Message:0x19284ec ...
|
567
|
+
Mail::TestMailer.deliveries.clear
|
568
|
+
=> []
|
569
|
+
```
|
570
|
+
|
571
|
+
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):
|
572
|
+
|
573
|
+
```
|
574
|
+
Mail.defaults do
|
575
|
+
delivery_method :test # in practice you'd do this in spec_helper.rb
|
576
|
+
end
|
577
|
+
|
578
|
+
describe "sending an email" do
|
579
|
+
include Mail::Matchers
|
580
|
+
|
581
|
+
before(:each) do
|
582
|
+
Mail::TestMailer.deliveries.clear
|
583
|
+
|
584
|
+
Mail.deliver do
|
585
|
+
to ['mikel@me.com', 'mike2@me.com']
|
586
|
+
from 'you@you.com'
|
587
|
+
subject 'testing'
|
588
|
+
body 'hello'
|
589
|
+
end
|
590
|
+
end
|
591
|
+
|
592
|
+
it { should have_sent_email } # passes if any email at all was sent
|
593
|
+
|
594
|
+
it { should have_sent_email.from('you@you.com') }
|
595
|
+
it { should have_sent_email.to('mike1@me.com') }
|
596
|
+
|
597
|
+
# can specify a list of recipients...
|
598
|
+
it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
|
599
|
+
|
600
|
+
# ...or chain recipients together
|
601
|
+
it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
|
602
|
+
|
603
|
+
it { should have_sent_email.with_subject('testing') }
|
604
|
+
|
605
|
+
it { should have_sent_email.with_body('hello') }
|
606
|
+
|
607
|
+
# Can match subject or body with a regex
|
608
|
+
# (or anything that responds_to? :match)
|
609
|
+
|
610
|
+
it { should have_sent_email.matching_subject(/test(ing)?/) }
|
611
|
+
it { should have_sent_email.matching_body(/h(a|e)llo/) }
|
612
|
+
|
613
|
+
# Can chain together modifiers
|
614
|
+
# Note that apart from recipients, repeating a modifier overwrites old value.
|
615
|
+
|
616
|
+
it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
|
617
|
+
end
|
618
|
+
```
|
619
|
+
|
620
|
+
Excerpts from TREC Spam Corpus 2005
|
621
|
+
-----------------------------------
|
622
|
+
|
623
|
+
The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
|
624
|
+
2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
|
625
|
+
that project and license agreement. They are used in this project to verify
|
626
|
+
and describe the development of this email parser implementation.
|
627
|
+
|
628
|
+
http://plg.uwaterloo.ca/~gvcormac/treccorpus/
|
629
|
+
|
630
|
+
They are used as allowed by 'Permitted Uses, Clause 3':
|
631
|
+
|
632
|
+
"Small excerpts of the information may be displayed to others
|
633
|
+
or published in a scientific or technical context, solely for
|
634
|
+
the purpose of describing the research and development and
|
635
|
+
related issues."
|
636
|
+
|
637
|
+
-- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
|
638
|
+
|
639
|
+
License
|
640
|
+
-------
|
641
|
+
|
642
|
+
(The MIT License)
|
643
|
+
|
644
|
+
Copyright (c) 2009, 2010, 2011, 2012
|
645
|
+
|
646
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
647
|
+
a copy of this software and associated documentation files (the
|
648
|
+
'Software'), to deal in the Software without restriction, including
|
649
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
650
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
651
|
+
permit persons to whom the Software is furnished to do so, subject to
|
652
|
+
the following conditions:
|
653
|
+
|
654
|
+
The above copyright notice and this permission notice shall be
|
655
|
+
included in all copies or substantial portions of the Software.
|
656
|
+
|
657
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
658
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
659
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
660
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
661
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
662
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
663
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|