TMail: Sending email

TMail is a commonly used library by the ActionMailer component of Ruby

The students of the FORPC101 batch wanted to know how they could send an email in Ruby. Today, out of the various options available, we will have a quick look at TMail – a library used for composing and manipulating email messages for Ruby.

TMail is designed to be an Request For Comments (RFC) compatible library. The goal of TMail is to allow you to create emails programatically without having to know about the RFCs or how they work, or what their standards are.

Installation

On a Windows box, we can install the TMail gem as follows:

c:\>gem install tmail

Sending an email

You first put at the top of your class:

require 'tmail'

Accessing a TMail object is done via the TMail::Mail class. As email can be fairly complex creatures, you will find a large amount of accessor and setter methods in this class!

You then create an empty mail object by calling TMail::Mail.new. This creates a completely blank email message. To be a valid email message, you’ll need to modify the headers by simply accessing them using the accessor methods. Some of the public instance methods we use are: from, to, subject, date and body (for full details, please refer to the TMail documentation). For example, the documentation for the instance method subject is:
subject( default = nil )
Returns the subject of the mail instance. If the subject field does not exist, returns nil by default or you can pass in as the parameter for what you want the default value to be. Example:

mail = TMail::Mail.new
mail.subject #=> nil
mail.subject("") #=> ""
mail.subject = "Hello"
mail.subject #=> "Hello"

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. The Net::SMTP library provides functionality to send internet mail via SMTP, the Simple Mail Transfer Protocol. The Net::SMTP.start method creates a new Net::SMTP object, opens a TCP connection and connects to the server. ‘localhost’ is the IP address of your SMTP server and the port used is 25. Since we have called with a block, the newly-opened Net::SMTP object is yielded to the block, and automatically closed when the block finishes.

Before you run the program (full code below), you need to start the SMTP server. On Windows, I prefer to download and use the Free SMTP Server for testing. Connect your PC to the Internet, then start the Free SMTP Server program. The program will try to detect your provider’s DNS server. If a DNS server is available and the standard SMTP port #25 is free, the program will become ready to process your mail. If it is not possible to detect provider’s DNS server, the program will prompt you to enter any DNS server you know, after that if the server exists the program will become ready to process mail. If the port #25 is busy by another local SMTP server/service, the program will show you an error message. You can specify any DNS server or SMTP port by using the button Options, which is situated on the Toolbar. You should use the word “localhost” as the SMTP server host in your mail program.

Here’s the complete program code:

require 'rubygems'
require 'tmail'
require 'net/smtp'

tomail = 'satishtalim@gmail.com'
frommail = 'superman@world.com'
mail = TMail::Mail.new
mail.to = tomail
mail.from = frommail
mail.subject = 'Test message'
mail.date = Time.now
mail.body = 'Thanks to Locaweb for making this possible.'


Net::SMTP.start( 'localhost', 25 ) do|smtpclient|
  smtpclient.send_message(
    mail.to_s,
    frommail,
    tomail
  )
end

It is to be noted that TMail is a commonly used library by the ActionMailer component of Ruby on Rails, the Nitro web framework, the Ruby-Talk mail gateway and many others.

Technorati Tags: , , ,

comments powered by Disqus