2

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Simmo
  • 1,717
  • 19
  • 37
  • 1
    What's the reason behind setting up your own SMTP server? – Thomas Klemm Jan 25 '13 at 16:39
  • I'd like to process the incoming messages with Rails. I do some internal stuff and then shove some of the content on via ActiveResource. I could write a gem which would be triggered by something like Postfix, but it seems neater to have it all together. – Simmo Jan 25 '13 at 16:52
  • There are a few cloud services dedicated to that matter that might be worth checking out, e.g. [Cloudmailin](http://www.cloudmailin.com/) [(Receiving mail in Ruby)](https://devcenter.heroku.com/articles/cloudmailin#receiving-email-in-ruby) or [Postmark](https://postmarkapp.com). – Thomas Klemm Jan 25 '13 at 18:36
  • Apart from external providers, there are a few more options described in this [blog post](http://steve.dynedge.co.uk/2010/09/07/incoming-email-in-rails-3-choosing-the-right-approach/) from the creator of Cloudmailin, a notable one seems the [mailman gem](https://github.com/titanous/mailman). – Thomas Klemm Jan 25 '13 at 18:40
  • [Mailgun](http://www.mailgun.com/) seems to provide an even easier solution for [receiving mails via HTTP (Mailgun)](https://devcenter.heroku.com/articles/mailgun#receiving-messages-via-http). – Thomas Klemm Jan 25 '13 at 18:59
  • Hi Thomas. Thanks for all the suggestions. I should have said perhaps that this is an Intranet app and I can't rely on connectivity from external services. – Simmo Jan 28 '13 at 09:56

1 Answers1

4

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
Community
  • 1
  • 1
Iuri G.
  • 10,460
  • 4
  • 22
  • 39
  • Hi Luri. Looks like a good start but when I run the rake task it starts the SMTP server (I think) and then stops it again as soon as the rake task completes. How do I get it to continue running? I've tried it as a daemon but the same thing occurs, it just starts and stops straight-away. – Simmo Jan 28 '13 at 14:38
  • 1
    I modified my code. Added `server.join` in the end. hopefully that does the trick. – Iuri G. Jan 28 '13 at 14:41