ruby-on-railsrubysmtpruby-daemons

How do I create a daemon to run an SMTP server within a Rails stack?


I'm running a rails app which, amongst other things, needs to role it's own SMTP server. Mini-SMTP-Server looks very good, but I don't know how to get it to run as a daemon. I'd like to be able to act on incoming messages and I need to have the full Rails stack available for other tasks.

I've looked at the daemons gem and it seems suitable but don't know how to wire it up to start listening for SMTP messages in a sensible fashion.


Solution

  • create a Rake smtp_server rake task, make sure it depends on environment and then write your code for smtp server in that task. Look at this thread for setting up rake task as daemon: Daemoninsing a rake task

    desc 'smtp_server'
    task :smtp_server => :environment do
      # Create a new server instance listening at 127.0.0.1:2525
      # and accepting a maximum of 4 simultaneous connections
      server = MiniSmtpServer.new(2525, "127.0.0.1", 4)
    
      # Start the server
      server.start
      # Join the thread to main pool
      server.join
    end