Mail: An All New Ruby E-mail Library

By Peter Cooper

To date, the main ways to send e-mails from Ruby have been Net::SMTP, TMail, and Rails' ActionMailer (which uses TMail). Now, however, there's a fourth option, the simply named "mail" by Mikel Lindsaar.

Mail is a new pure Ruby library designed to handle the generation, parsing, and sending of e-mail in a "Rubyesque" manner. Both the sending and receiving e-mails can be handled through the library and, where necessary, Mail proxies methods from libraries like Net::SMTP and Net::POP3. Ruby 1.9 support has been built in from day one so dealing with different text encodings in your e-mails is easier than ever (Mikel claims this is less than straightforward with TMail). Mikel also points out that Mail has 100% spec coverage.

There are lots of code examples on the Mail page but to give you an idea of how it works, here's a code example that sends an e-mail with an attachment:

Mail . defaults do smtp ' 127.0.0.1 ' end mail = Mail . new do from ' me@test.lindsaar.net ' to ' you@test.lindsaar.net ' subject ' Here is the image you wanted ' body File . read (' body.txt ') add_file { :filename => ' somefile.png ', :data => File . read (' /somefile.png ')} end mail . deliver!

As well as the main GitHub page for the Mail project, there's also a mail-ruby Google Group where questions can be asked, etc.

[ad] Find duplication, code smells, complex code and more in your Ruby code with Caliper! The metrics are free and setup takes just one click. Get started!