How to make a script like e.g. 'script/mailman_server' work for multiple users in a Rails app

254 Views Asked by At

I have a Rails app that uses the mailman gem (link) to collect inbound pop3 mails and do stuff with them. It all works just fine, but now I wish to have multiple users sign up and receive mails from their pop3 configuration. Mailman is run in 'script/mailman_server', and I am unfamiliar with how to make a script (thats running its own process) work for multiple users. Could anyone enlighten me on the subject or link to a resource that touches this subject?

This is my code as of now

# script/mailman_server

#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"

Mailman.config.pop3 = {
  :username => 'some_name@some_email.com',
  :password => 'some_password',
  :server   => 'pop.gmail.com',
  :port     => 995,
  :ssl      => true
}

Mailman::Application.run do
  default do
    Email.receive_mail(message)
  end
end


# app/models/email.rb

class Email
  include Mongoid::Document
  include Mongoid::Timestamps

  field :from, type: String
  field :subject, type: String
  field :body, type: String

  belongs_to :customer, :inverse_of => :emails

  def self.receive_mail(message)
    email = Email.create!(subject: message.subject, body: message.body.decoded, from: message.from.first)
  end
end
0

There are 0 best solutions below