1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Spotlight on Gems: TMail

Using the TMail RubyGem for Email

By

The TMail RubyGem is a library used for composing and manipulating email messages. At its core, the TMail gem is an email parser. It will take an email message and give you an easy interface to the headers and the body of the message. Complemented by the Net::POP and Net::SMTP libraries, TMail can be used to automate many email tasks such as downloading, sorting and responding to email messages.

Installing

You can install the TMail RubyGem just like you would install any other gem.

 $ sudo gem install tmail

The TMail gem is written partially in C for greater speed. If you'll be doing bulk mail processing, you may want to consider installing and configuring a C compiler. However, if your system is not set up to compile C programs for Ruby gems, TMail also includes a pure-Ruby implementation that will function in the same way. The C implementation is faster, but for personal use the pure Ruby implementation will work fine.

Creating new Email Messages

Empty mail objects can be created by calling TMail::Mail.new. This creates a completely blank email message. To be a valid email message, you'll need to fill in the To, From and Subject headers. To modify headers, simply access them using the accessor methods. Once the email message is created, you can call the to_s method to convert it back into a string. Then it can be sent with the Net::SMTP library or storing on disk.

#!/usr/bin/env ruby
 require 'rubygems'
 require 'tmail'
 require 'net/smtp'
 
 mail = TMail::Mail.new
 mail.to = 'username@other-isp.com'
 mail.from = 'username@isp.com'
 mail.subject = 'Test message'
 mail.body = 'This is a test message from Ruby'
 
 Net::SMTP.start( 'mail.isp.com', 25 ) do|smtp|
   smtp.send_message(
     mail.to_s,
     'username@isp.com',
     'username@other-isp.com'
   )
 end

Email Messages From a Template

Templates are commonplace when you're discussing email. It's a very common action to email a pre-written message in response to another email message or some other action. For example, when you sign up as a member of a website, you may get a welcome message detailing some features of the website. Creating this automated welcome message is a simple task--simply load the pre-written template message, fill in the To header and send it out.

There are two methods that can be used to parse existing email messages. The TMail::Mail.parse method will parse a method stored in a string. This can be useful for smaller messages, but for larger messages--especially those with attachments--this is not appropriate. Instead, use the TMail::Mail.load method, which will parse an email message stored in a file.

 #!/usr/bin/env ruby
 require 'rubygems'
 require 'tmail'
 require 'net/smtp'
 
 mail = TMail::Mail.parse(DATA.read)
 mail.to = 'user@isp.com'
 
 Net::SMTP.start( 'mail.isp.com', 25 ) do|smtp|
   smtp.send_message(
     mail.to_s,
     'username@test.com',
     'user@isp.com'
   )
 end
 
 __END__
 From: test@test.com
 Subject: Welcome to test.com!
 
 This is a welcome message from
 test.com! Thank you for registering
 with us.
  1. About.com
  2. Technology
  3. Ruby
  4. Gems
  5. Spotlight on Gems: TMail

©2014 About.com. All rights reserved.