Created
April 19, 2012 16:14
-
-
Save sidravic/2422044 to your computer and use it in GitHub Desktop.
mailman_guide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if Rails.env == "production" | |
MAILMAN_USER = "something@production.com" | |
MAILMAN_PASSWORD = "pasword123" | |
else | |
MAILMAN_USER = "something@staging.com" | |
MAILMAN_PASSWORD = "password321" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :mailman do | |
desc "Mailman::Start" | |
task :start, :roles => [:app] do | |
run "cd #{current_path};RAILS_ENV=#{rack_env} bundle exec script/mailman_daemon start" | |
end | |
desc "Mailman::Stop" | |
task :stop, :roles => [:app] do | |
run "cd #{current_path};RAILS_ENV=#{rack_env} bundle exec script/mailman_daemon stop" | |
end | |
desc "Mailman::Restart" | |
task :restart, :roles => [:app] do | |
mailman.stop | |
mailman.start | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'daemons' | |
gem 'mailman', require: false | |
gem 'maildir' | |
gem 'mail' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class IncomingMail | |
def initialize(message, params) | |
@user = User.where(email: message.from).first rescue nil | |
@message = message | |
@params = params | |
end | |
def process(method) | |
begin | |
if spam_test #### this is a good place to filter unwanted mail. | |
self.send method | |
end | |
rescue Exception => e | |
Mailman.logger.error "Exception occurred while receiving message:\n#{@message}" | |
Mailman.logger.error [e, *e.backtrace].join("\n") | |
end | |
end | |
def spam_test | |
if @user | |
return true | |
else | |
Mailman.logger.error "Mail from #{@message.from.first} is unsolicited. Message ignored." | |
return false | |
end | |
end | |
def some_other_param_test(object) | |
### test | |
end | |
def default | |
# create default action for emails from valid users. | |
Mailman.logger.info 'Message matched the default route. Message ignored' | |
end | |
def method_for_route_one | |
@interesting_param = @params[:interesting] | |
if some_other_param_test(@interesting_param) | |
if ## some Rails environment method | |
Mailman.logger.info "#{@user.name} performed something useful." | |
end | |
else | |
Mailman.logger.error "Something went wrong, here's useful debugging detail." | |
end | |
end | |
def method_for_route_two | |
@interesting_param = @params[:interesting] | |
if some_other_param_test(@interesting_param) | |
if ## some Rails environment method | |
Mailman.logger.info "#{@user.name} performed something else." | |
end | |
else | |
Mailman.logger.error "Something went wrong, here's another useful debugging detail." | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
require 'maildir' | |
def send_test_mail | |
@mail = Mail.new(from: @from_address, to: @to_address, subject: @subject, body: @body) | |
@sent = @maildir.add(@mail) | |
sleep 2 | |
end | |
describe ReceivedMail do | |
before(:each) do | |
@maildir = Maildir.new('tmp/test_maildir') | |
@maildir.serializer = Maildir::Serializer::Mail.new | |
@other_variable = Object.new | |
end | |
describe "Mail received by the mailman server" do | |
before(:each) do | |
@from_address = 'from@default.com' | |
@to_address = 'to@default.com' | |
@subject = 'default subject' | |
@body = 'default body text' | |
end | |
it "should achieve something" do | |
pending | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require "bundler/setup" | |
require 'daemons' | |
Daemons.run('script/mailman_server') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "rubygems" | |
require "bundler/setup" | |
require "mailman" | |
ENV["RAILS_ENV"] ||= "test" | |
require File.dirname(__FILE__) + "/../config/environment" | |
Mailman.config.ignore_stdin = true | |
Mailman.config.logger = Logger.new File.expand_path("../../log/mailman_#{Rails.env}.log", __FILE__) | |
if Rails.env == 'test' | |
Mailman.config.maildir = File.expand_path("../../tmp/test_maildir", __FILE__) | |
else | |
Mailman.config.logger = Logger.new File.expand_path("../../log/mailman.log", __FILE__) | |
Mailman.config.poll_interval = 15 | |
Mailman.config.pop3 = { | |
server: 'pop.gmail.com', port: 995, ssl: true, | |
username: MAILMAN_USER, | |
password: MAILMAN_PASSWORD | |
} | |
end | |
Mailman::Application.run do | |
to('route_one_%interesting%@%domain%') do | |
IncomingMail.new(message, params).process(:method_for_route_one) | |
end | |
to('someone_else@%domain%').subject('%interesting%') do | |
IncomingMail.new(message, params).process(:method_for_route_two) | |
end | |
default do | |
IncomingMail.new(message, params).process(:default) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment