#to_s
on a container object will call its encoded
method, while #to_s
on a field object will call it's decoded method.
So calling #to_s
on a Mail object will return the mail, all encoded
ready to send, while calling #to_s
on the From field or the body will
return the decoded value of the object. The header object of Mail is considered a
container. If you are in doubt, call #encoded
, or #decoded
explicitly, this is safer if you are not sure.
4. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
provide decoded parameter values when you call the parameter names as methods against
the object.
5. Structured fields that have parameter values that can be encoded (e.g. Content-Type) will
provide encoded parameter values when you call the parameter names through the
object.parameters['']
method call.
Contributing
------------
Please do! Contributing is easy in Mail. Please read the CONTRIBUTING.md document for more info
Usage
-----
All major mail functions should be able to happen from the Mail module.
So, you should be able to just require 'mail'
to get started.
### Making an email
```ruby
mail = Mail.new do
from 'mikel@test.lindsaar.net'
to 'you@test.lindsaar.net'
subject 'This is a test email'
body File.read('body.txt')
end
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
```
### Making an email, have it your way:
```ruby
mail = Mail.new do
body File.read('body.txt')
end
mail['from'] = 'mikel@test.lindsaar.net'
mail[:to] = 'you@test.lindsaar.net'
mail.subject = 'This is a test email'
mail.to_s #=> "From: mikel@test.lindsaar.net\r\nTo: you@...
```
### Don't Worry About Message IDs:
```ruby
mail = Mail.new do
to 'you@test.lindsaar.net'
body 'Some simple body'
end
mail.to_s =~ /Message\-ID: <[\d\w_]+@.+.mail/ #=> 27
```
Mail will automatically add a Message-ID field if it is missing and
give it a unique, random Message-ID along the lines of:
<4a7ff76d7016_13a81ab802e1@local.fqdn.mail>
### Or do worry about Message-IDs:
```ruby
mail = Mail.new do
to 'you@test.lindsaar.net'
message_id 'retriever_method
within Mail.defaults
:
```ruby
Mail.defaults do
retriever_method :pop3, :address => "pop.gmail.com",
:port => 995,
:user_name => 'Here is the attachment you wanted
' end add_file '/path/to/myfile.pdf' end @round_tripped_mail = Mail.new(@mail.encoded) @round_tripped_mail.attachments.length #=> 1 @round_tripped_mail.attachments.first.filename #=> 'myfile.pdf' ``` See "Testing and extracting attachments" above for more details. Using Mail with Testing or Spec'ing Libraries --------------------------------------------- If mail is part of your system, you'll need a way to test it without actually sending emails, the TestMailer can do this for you. ``` require 'mail' => true Mail.defaults do delivery_method :test end => #delivery_method
as above too):
```
Mail.defaults do
delivery_method :test # in practice you'd do this in spec_helper.rb
end
describe "sending an email" do
include Mail::Matchers
before(:each) do
Mail::TestMailer.deliveries.clear
Mail.deliver do
to ['mikel@me.com', 'mike2@me.com']
from 'you@you.com'
subject 'testing'
body 'hello'
end
end
it { should have_sent_email } # passes if any email at all was sent
it { should have_sent_email.from('you@you.com') }
it { should have_sent_email.to('mike1@me.com') }
# can specify a list of recipients...
it { should have_sent_email.to(['mike1@me.com', 'mike2@me.com']) }
# ...or chain recipients together
it { should have_sent_email.to('mike1@me.com').to('mike2@me.com') }
it { should have_sent_email.with_subject('testing') }
it { should have_sent_email.with_body('hello') }
# Can match subject or body with a regex
# (or anything that responds_to? :match)
it { should have_sent_email.matching_subject(/test(ing)?/) }
it { should have_sent_email.matching_body(/h(a|e)llo/) }
# Can chain together modifiers
# Note that apart from recipients, repeating a modifier overwrites old value.
it { should have_sent_email.from('you@you.com').to('mike1@me.com').matching_body(/hell/)
end
```
Excerpts from TREC Spam Corpus 2005
-----------------------------------
The spec fixture files in spec/fixtures/emails/from_trec_2005 are from the
2005 TREC Public Spam Corpus. They remain copyrighted under the terms of
that project and license agreement. They are used in this project to verify
and describe the development of this email parser implementation.
http://plg.uwaterloo.ca/~gvcormac/treccorpus/
They are used as allowed by 'Permitted Uses, Clause 3':
"Small excerpts of the information may be displayed to others
or published in a scientific or technical context, solely for
the purpose of describing the research and development and
related issues."
-- http://plg.uwaterloo.ca/~gvcormac/treccorpus/
License
-------
(The MIT License)
Copyright (c) 2009, 2010, 2011, 2012
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.