title: How to start MidiSmtpServer in Rails
body: <p>I am trying to use MidiSmtpServer to receive email in a Heroku application, and have been using the code on one of the examples that the documents show. However, I don't know where to put that code for the SMTP server to start after Puma, or where to put it for it to start at all. Using <code>on_worker_boot</code> in puma.rb doesnt work.</p>
  
  <p>puma.rb:</p>
  
  <pre><code># Puma can serve each request in a thread from an internal thread pool.
  # The `threads` method setting takes two numbers: a minimum and maximum.
  # Any libraries that use thread pools should be configured to match
  # the maximum value specified for Puma. Default is set to 5 threads for minimum
  # and maximum; this matches the default thread size of Active Record.
  #
  max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
  min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
  threads min_threads_count, max_threads_count
  
  # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
  #
  port ENV.fetch("PORT") { 3000 }
  
  # Specifies the `environment` that Puma will run in.
  #
  environment ENV.fetch("RAILS_ENV") { "development" }
  
  # Specifies the `pidfile` that Puma will use.
  pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
  
  # Specifies the number of `workers` to boot in clustered mode.
  # Workers are forked web server processes. If using threads and workers together
  # the concurrency of the application would be max `threads` * `workers`.
  # Workers do not work on JRuby or Windows (both of which do not support
  # processes).
  #
  # workers ENV.fetch("WEB_CONCURRENCY") { 2 }
  
  require "midi-smtp-server"
  require "mail"
  on_worker_boot do
  
    class MySmtpd &lt; MidiSmtpServer::Smtpd
      def on_message_data_event(ctx)
        puts "[#{ctx[:envelope][:from]}] for recipient(s): [#{ctx[:envelope][:to]}]..."
  
        # Just decode message ones to make sure, that this message ist readable
        @mail = Mail.read_from_string(ctx[:message][:data])
  
        # handle incoming mail, just show the message source
        puts @mail.to_s
      end
    end
  
    # try to gracefully shutdown on Ctrl-C
    trap("INT") do
      puts "Interrupted, exit now..."
      exit 0
    end
  
    # Output for debug
    puts "#{Time.now}: Starting MySmtpd..."
  
    # Create a new server instance listening at localhost interfaces 127.0.0.1:2525
    # and accepting a maximum of 4 simultaneous connections
    server = MySmtpd.new(2525, "0.0.0.0", 4)
  
    # setup exit code
    at_exit do
      # check to shutdown connection
      if server # Output for debug
        puts "#{Time.now}: Shutdown MySmtpd..."    # stop all threads and connections gracefully
  
        server.stop
      end  # Output for debug
  
      puts "#{Time.now}: MySmtpd down!\n"
    end
  
    # Start the server
    server.start
  
    # Run on server forever
    server.join
  end
  # Use the `preload_app!` method when specifying a `workers` number.
  # This directive tells Puma to first boot the application and load code
  # before forking the application. This takes advantage of Copy On Write
  # process behavior so workers use less memory.
  #
  # preload_app!
  
  # Allow puma to be restarted by `rails restart` command.
  plugin :tmp_restart
  </code></pre>