From ef5b5a11a2df15af86343b1ec45a3a227541cf8f Mon Sep 17 00:00:00 2001 From: Patrick Gansterer Date: Sat, 4 Feb 2017 07:20:32 +0100 Subject: [PATCH] Add reply_email_smtp_server task --- plugins/messages/lib/tasks/foodsoft.rake | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/plugins/messages/lib/tasks/foodsoft.rake b/plugins/messages/lib/tasks/foodsoft.rake index 89c69a3b..c753ab6c 100644 --- a/plugins/messages/lib/tasks/foodsoft.rake +++ b/plugins/messages/lib/tasks/foodsoft.rake @@ -1,10 +1,33 @@ require "mail" +require "mini-smtp-server" + +class ReplyEmailSmtpServer < MiniSmtpServer + + def new_message_event(message_hash) + m = /<(?[^<>]+)>/.match(message_hash[:to]) + raise "invalid format for RCPT TO" if m.nil? + hande_mail(m[:recipient], message_hash[:data]) + rescue => error + rake_say error.message + end + +end namespace :foodsoft do desc "Parse incoming email on stdin (options: RECIPIENT=f.1.2.a1b2c3d3e5)" task :parse_reply_email => :environment do hande_mail(ENV['RECIPIENT'], STDIN.read) end + + desc "Start STMP server for incoming email (options: PORT=25, HOST=0.0.0.0)" + task :reply_email_smtp_server => :environment do + port = ENV['PORT'].to_i + host = ENV['HOST'] + rake_say "Started SMTP server for incomming email on port #{port}." + server = ReplyEmailSmtpServer.new(port, host) + server.start + server.join + end end def hande_mail(recipient, received_email)