views:

312

answers:

2

Having issues with a rails 2.2.2 app running on a VPS (Ubuntu 8.10): looking for github repos, posts, anything that covers the right way of setting up email processing. For example, should it be using sendmail or smtp?

+1  A: 

This is not Ubuntu-specific.

Configuring the sending of mail doesn't really have a "correct" answer. If you have sendmail working upon your machine (so that "mail [email protected]" works) then use that.

If you have an SMTP server running locally, which works, then use that.

The more specific answer really depends on you saying:

  • What did you try?
  • How did it fail?
Steve Kemp
A: 

Are you looking to send or receive email (or both)?

The answers will be different in each case, and may (or may not) include configuring a mail server on the VPS.

If you need to configure a mail server the best guides (extensive and step by step) I found are those from slicehost that you find here: Mail server setup and configuration

If you need to just receive emails the best thing is to avoid installing a mail server and let your service provider (or Moogle) handle the incoming email.

Then you have to write some code to fetch those emails form the POP or IMAP server and feed them to the incoming mail handler (which typically is based on ActionMailer). One easy way to write the fetching code is to use the fetcher plug-in which incorporates the following common pattern of interaction with the mail server:

  1. Connect to a remote server (POP or IMAP)
  2. Download the available messages
  3. Process each message (passing it to another object)
  4. Remove all downloaded messages from the remote server

you need to create an instance of the class Fetcher passing the class of the object that will process the emails (plus any other configuration parameters that are needed), then calling the fetch method will execute the steps 1 to 4 above.

The plug-in is on Github: http://github.com/look/fetcher/tree/master and you can use it both to build a daemon (i.e. a process which stays in a loop polling the mail server for new messages), or to write a batch file to be run from cron (taken from the plug-in docs):

begin
  Lockfile.new('cron_mail_fetcher.lock', :retries => 0) do
    config = YAML.load_file("#{RAILS_ROOT}/config/mail.yml")
    config = config[RAILS_ENV].to_options

    fetcher = Fetcher.create({:receiver => MailReceiver}.merge(config))
    fetcher.fetch
  end
rescue Lockfile::MaxTriesLockError => e
  puts "Another fetcher is already running. Exiting."
end
LucaM