Adds a rake task to receive emails from a POP3 server (#2420).

See:

  rake -D redmine📧receive_pop3

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3330 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2010-01-17 13:53:13 +00:00
parent b4cf8ea525
commit 3eb815fddd
2 changed files with 89 additions and 0 deletions

56
lib/redmine/pop3.rb Normal file
View File

@ -0,0 +1,56 @@
# Redmine - project management software
# Copyright (C) 2006-2010 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'net/pop'
module Redmine
module POP3
class << self
def check(pop_options={}, options={})
host = pop_options[:host] || '127.0.0.1'
port = pop_options[:port] || '110'
apop = (pop_options[:apop].to_s == '1')
delete_unprocessed = (pop_options[:delete_unprocessed].to_s == '1')
pop = Net::POP3.APOP(apop).new(host,port)
puts "Connecting to #{host}..."
pop.start(pop_options[:username], pop_options[:password]) do |pop_session|
if pop_session.mails.empty?
puts "No email to process"
else
puts "#{pop_session.mails.size} email(s) to process..."
pop_session.each_mail do |msg|
message = msg.pop
message_id = (message =~ /^Message-ID: (.*)/ ? $1 : '').strip
if MailHandler.receive(message, options)
msg.delete
puts "--> Message #{message_id} processed and deleted from the server"
else
if delete_unprocessed
msg.delete
puts "--> Message #{message_id} NOT processed and deleted from the server"
else
puts "--> Message #{message_id} NOT processed and left on the server"
end
end
end
end
end
end
end
end
end

View File

@ -132,5 +132,38 @@ END_DESC
Redmine::IMAP.check(imap_options, options)
end
desc <<-END_DESC
Read emails from an POP3 server.
Available POP3 options:
host=HOST POP3 server host (default: 127.0.0.1)
port=PORT POP3 server port (default: 110)
username=USERNAME POP3 account
password=PASSWORD POP3 password
apop=1 use APOP authentication (default: false)
delete_unprocessed=1 delete messages that could not be processed
successfully from the server (default
behaviour is to leave them on the server)
See redmine:email:receive_imap for more options and examples.
END_DESC
task :receive_pop3 => :environment do
pop_options = {:host => ENV['host'],
:port => ENV['port'],
:apop => ENV['apop'],
:username => ENV['username'],
:password => ENV['password'],
:delete_unprocessed => ENV['delete_unprocessed']}
options = { :issue => {} }
%w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
Redmine::POP3.check(pop_options, options)
end
end
end