Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tim Morgan http://timmorgan.org

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Using POP3 to Retrieve Email for Rails App

Put this in a file called "inbox" in the scripts/ directory of your Rails app. change the host, username, and password to match.

Set up a cron job to run this script every so often (frequency depends on your needs).

   1  
   2  #!/usr/bin/env ruby
   3  
   4  require 'net/pop'
   5  require File.dirname(__FILE__) + '/../config/environment'
   6  
   7  logger = RAILS_DEFAULT_LOGGER
   8  
   9  logger.info "Running Mail Importer..." 
  10  Net::POP3.start("localhost", nil, "username", "password") do |pop|
  11    if pop.mails.empty?
  12      logger.info "NO MAIL" 
  13    else
  14      pop.mails.each do |email|
  15        begin
  16          logger.info "receiving mail..." 
  17          Notifier.receive(email.pop)
  18          email.delete
  19        rescue Exception => e
  20          logger.error "Error receiving email at " + Time.now.to_s + "::: " + e.message
  21        end
  22      end
  23    end
  24  end
  25  logger.info "Finished Mail Importer." 
  26  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS