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

Check Email With Ruby

By

Here's a riddle for you. It's the only way to catch fish without playing hooky from work and you do it everyday. What is it? Checking your email. (I didn't say it was a good riddle.)

Email is one of the most basic computing functions most of us do on a daily basis. And while catching phish (get it now?) and deleting SPAM are small annoyances we put up with in order to get to the business of business, most of us are not willing to put up with the annoyance of dealing with unintuitive and complicated email clients. Checking your email should be easy and, if you use Ruby to do it, it can be.

Difficulty: Average
Time Required: 15 minutes

Here's How:

  1. Checking your email with Ruby is as simple as using the pre-installed POP3 email libraries. To begin using the Ruby POP3 library, simply require 'net/pop' and connect to a server.
  2. Be sure to provide the correct username and password for your account. This example is rather simple, it just logs into the POP3 server, checks if there are any messages and, if there are, displays the number of new messages. You could easily expand this example to include a timer and play a sound when you get new mail, making for an even more useful program.
    
    #!/usr/bin/env ruby
    require 'net/pop'

    pop = Net::POP3.new 'mail.isp.com'
    pop.start 'username@isp.com', 'password'

    if pop.mails.empty?
    puts "No mail."
    else
    puts "You have #{pop.mails.length} new messages."
    end
  3. The POP3 library can also download your mail. To save your mail messages, simply iterate through the pop.mails collection like any other array. Each mail message has a pop method that will retrieve the message from the server and store it in a string.
  4. Write that message to a File object to save the email message.

    
    #!/usr/bin/env ruby
    require 'net/pop'

    pop = Net::POP3.new 'mail.isp.com'
    pop.start 'username@isp.com', 'password'

    if pop.mails.empty?
    puts "No mail."
    else
    puts "You have #{pop.mails.length} new messages."
    puts "Downloading..."

    pop.mails.each_with_index do|m,i|
    File.open( "inbox/#{i}", 'w+' ) do|f|
    f.write m.pop
    end

    m.delete
    end
    end

Tips:

  1. POP3 is used to check your mailboxes on ISP mail accounts. It's one of the ways a number of popular email clients, like Outlook Express, Eudora and Thunderbird, check and download the email you have stored on your ISP's server. Though the IMAP protocol is often used too, especially on mobile devices and in corporate environments, most mail servers support the POP3 protocol.
  2. Even though POP3 can't send mail--only download it--this can be particularly useful if the Ruby program is also set to respond to emails automatically (such as with a mailing list manager).
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. How to Use Ruby to Check Email

©2014 About.com. All rights reserved.