Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Mail: An All New Ruby E-mail Library

By Peter Cooper / November 11, 2009

emailIcon.pngTo 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' # Port 25 defult
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.

caliper-logo.png[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!

Comments

  1. apeacox says:

    Have you tried the Pony (http://github.com/adamwiggins/pony) gem? I've used it, works simply well, it's used like a php mail() call :-)

  2. Stephen Celis says:

    Don't forget Pony!

    http://github.com/adamwiggins/pony

  3. Peter Cooper says:

    Ah yes, I totally forgot about Pony! I tried it once and it worked okay.

  4. Daniel Berger says:

    Hey, as long as we're pumping other email libraries, I'll pump my ez-email library:

    http://shards.rubyforge.org/wiki/wiki.pl?Ez-Email

    Can't do attachments yet, I'll have to get on that. :)

  5. Jason Seifer says:

    I remember that clip art!

  6. Peter Cooper says:

    I did a Tineye search on it before I used it and found it's in use on hundreds of sites, so I'm guessing it's verrry popular :-)

  7. Mikel Lindsaar says:

    Hey Peter,

    One correction, I aim for 100% spec coverage :) Not quite there... but I am in the high 95% percentile per rcov usually.

    Also, another point, in Mail I tried to bring together all the different things a mail library should do. I didn't know about Pony, and Ez-Mail, they look good.

    Anyone who wants to help contribute to Mail is more than welcome. It is a full on project and needs all the bright people it can get.

    Regards

    Mikel

  8. John Woodell says:

    Unfortunately, tmail doesn't attempt to parse MIME quoted_printable content. Sending mail is only half of the challenge. We desperately need a mail library with spec coverage. THANKS!

  9. Loren Segal says:

    A small but equally powerful mail lib: mmmail.

    http://github.com/lsegal/mmmail

Other Posts to Enjoy

Twitter Mentions