##// END OF EJS Templates
add support for pop3s (SSL) to redmine:email:receive_pop3 (#16707)...
Toshi MARUYAMA -
r12863:eaac0eff082e
parent child
Show More
@@ -1,62 +1,74
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require 'net/pop'
18 require 'net/pop'
19
19
20 module Redmine
20 module Redmine
21 module POP3
21 module POP3
22 class << self
22 class << self
23 def check(pop_options={}, options={})
23 def check(pop_options={}, options={})
24 if pop_options[:ssl]
25 ssl = true
26 if pop_options[:ssl] == 'force'
27 Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)
28 else
29 Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_PEER)
30 end
31 else
32 ssl = false
33 end
34
24 host = pop_options[:host] || '127.0.0.1'
35 host = pop_options[:host] || '127.0.0.1'
25 port = pop_options[:port] || '110'
36 port = pop_options[:port]
37 port ||= ssl ? '995' : '110'
26 apop = (pop_options[:apop].to_s == '1')
38 apop = (pop_options[:apop].to_s == '1')
27 delete_unprocessed = (pop_options[:delete_unprocessed].to_s == '1')
39 delete_unprocessed = (pop_options[:delete_unprocessed].to_s == '1')
28
40
29 pop = Net::POP3.APOP(apop).new(host,port)
41 pop = Net::POP3.APOP(apop).new(host,port)
30 logger.debug "Connecting to #{host}..." if logger && logger.debug?
42 logger.debug "Connecting to #{host}..." if logger && logger.debug?
31 pop.start(pop_options[:username], pop_options[:password]) do |pop_session|
43 pop.start(pop_options[:username], pop_options[:password]) do |pop_session|
32 if pop_session.mails.empty?
44 if pop_session.mails.empty?
33 logger.debug "No email to process" if logger && logger.debug?
45 logger.debug "No email to process" if logger && logger.debug?
34 else
46 else
35 logger.debug "#{pop_session.mails.size} email(s) to process..." if logger && logger.debug?
47 logger.debug "#{pop_session.mails.size} email(s) to process..." if logger && logger.debug?
36 pop_session.each_mail do |msg|
48 pop_session.each_mail do |msg|
37 message = msg.pop
49 message = msg.pop
38 message_id = (message =~ /^Message-I[dD]: (.*)/ ? $1 : '').strip
50 message_id = (message =~ /^Message-I[dD]: (.*)/ ? $1 : '').strip
39 if MailHandler.safe_receive(message, options)
51 if MailHandler.safe_receive(message, options)
40 msg.delete
52 msg.delete
41 logger.debug "--> Message #{message_id} processed and deleted from the server" if logger && logger.debug?
53 logger.debug "--> Message #{message_id} processed and deleted from the server" if logger && logger.debug?
42 else
54 else
43 if delete_unprocessed
55 if delete_unprocessed
44 msg.delete
56 msg.delete
45 logger.debug "--> Message #{message_id} NOT processed and deleted from the server" if logger && logger.debug?
57 logger.debug "--> Message #{message_id} NOT processed and deleted from the server" if logger && logger.debug?
46 else
58 else
47 logger.debug "--> Message #{message_id} NOT processed and left on the server" if logger && logger.debug?
59 logger.debug "--> Message #{message_id} NOT processed and left on the server" if logger && logger.debug?
48 end
60 end
49 end
61 end
50 end
62 end
51 end
63 end
52 end
64 end
53 end
65 end
54
66
55 private
67 private
56
68
57 def logger
69 def logger
58 ::Rails.logger
70 ::Rails.logger
59 end
71 end
60 end
72 end
61 end
73 end
62 end
74 end
@@ -1,180 +1,182
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 namespace :redmine do
18 namespace :redmine do
19 namespace :email do
19 namespace :email do
20
20
21 desc <<-END_DESC
21 desc <<-END_DESC
22 Read an email from standard input.
22 Read an email from standard input.
23
23
24 General options:
24 General options:
25 unknown_user=ACTION how to handle emails from an unknown user
25 unknown_user=ACTION how to handle emails from an unknown user
26 ACTION can be one of the following values:
26 ACTION can be one of the following values:
27 ignore: email is ignored (default)
27 ignore: email is ignored (default)
28 accept: accept as anonymous user
28 accept: accept as anonymous user
29 create: create a user account
29 create: create a user account
30 no_permission_check=1 disable permission checking when receiving
30 no_permission_check=1 disable permission checking when receiving
31 the email
31 the email
32 no_account_notice=1 disable new user account notification
32 no_account_notice=1 disable new user account notification
33 default_group=foo,bar adds created user to foo and bar groups
33 default_group=foo,bar adds created user to foo and bar groups
34
34
35 Issue attributes control options:
35 Issue attributes control options:
36 project=PROJECT identifier of the target project
36 project=PROJECT identifier of the target project
37 status=STATUS name of the target status
37 status=STATUS name of the target status
38 tracker=TRACKER name of the target tracker
38 tracker=TRACKER name of the target tracker
39 category=CATEGORY name of the target category
39 category=CATEGORY name of the target category
40 priority=PRIORITY name of the target priority
40 priority=PRIORITY name of the target priority
41 allow_override=ATTRS allow email content to override attributes
41 allow_override=ATTRS allow email content to override attributes
42 specified by previous options
42 specified by previous options
43 ATTRS is a comma separated list of attributes
43 ATTRS is a comma separated list of attributes
44
44
45 Examples:
45 Examples:
46 # No project specified. Emails MUST contain the 'Project' keyword:
46 # No project specified. Emails MUST contain the 'Project' keyword:
47 rake redmine:email:read RAILS_ENV="production" < raw_email
47 rake redmine:email:read RAILS_ENV="production" < raw_email
48
48
49 # Fixed project and default tracker specified, but emails can override
49 # Fixed project and default tracker specified, but emails can override
50 # both tracker and priority attributes:
50 # both tracker and priority attributes:
51 rake redmine:email:read RAILS_ENV="production" \\
51 rake redmine:email:read RAILS_ENV="production" \\
52 project=foo \\
52 project=foo \\
53 tracker=bug \\
53 tracker=bug \\
54 allow_override=tracker,priority < raw_email
54 allow_override=tracker,priority < raw_email
55 END_DESC
55 END_DESC
56
56
57 task :read => :environment do
57 task :read => :environment do
58 Mailer.with_synched_deliveries do
58 Mailer.with_synched_deliveries do
59 MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV))
59 MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV))
60 end
60 end
61 end
61 end
62
62
63 desc <<-END_DESC
63 desc <<-END_DESC
64 Read emails from an IMAP server.
64 Read emails from an IMAP server.
65
65
66 General options:
66 General options:
67 unknown_user=ACTION how to handle emails from an unknown user
67 unknown_user=ACTION how to handle emails from an unknown user
68 ACTION can be one of the following values:
68 ACTION can be one of the following values:
69 ignore: email is ignored (default)
69 ignore: email is ignored (default)
70 accept: accept as anonymous user
70 accept: accept as anonymous user
71 create: create a user account
71 create: create a user account
72 no_permission_check=1 disable permission checking when receiving
72 no_permission_check=1 disable permission checking when receiving
73 the email
73 the email
74 no_account_notice=1 disable new user account notification
74 no_account_notice=1 disable new user account notification
75 default_group=foo,bar adds created user to foo and bar groups
75 default_group=foo,bar adds created user to foo and bar groups
76
76
77 Available IMAP options:
77 Available IMAP options:
78 host=HOST IMAP server host (default: 127.0.0.1)
78 host=HOST IMAP server host (default: 127.0.0.1)
79 port=PORT IMAP server port (default: 143)
79 port=PORT IMAP server port (default: 143)
80 ssl=SSL Use SSL? (default: false)
80 ssl=SSL Use SSL? (default: false)
81 username=USERNAME IMAP account
81 username=USERNAME IMAP account
82 password=PASSWORD IMAP password
82 password=PASSWORD IMAP password
83 folder=FOLDER IMAP folder to read (default: INBOX)
83 folder=FOLDER IMAP folder to read (default: INBOX)
84
84
85 Issue attributes control options:
85 Issue attributes control options:
86 project=PROJECT identifier of the target project
86 project=PROJECT identifier of the target project
87 status=STATUS name of the target status
87 status=STATUS name of the target status
88 tracker=TRACKER name of the target tracker
88 tracker=TRACKER name of the target tracker
89 category=CATEGORY name of the target category
89 category=CATEGORY name of the target category
90 priority=PRIORITY name of the target priority
90 priority=PRIORITY name of the target priority
91 allow_override=ATTRS allow email content to override attributes
91 allow_override=ATTRS allow email content to override attributes
92 specified by previous options
92 specified by previous options
93 ATTRS is a comma separated list of attributes
93 ATTRS is a comma separated list of attributes
94
94
95 Processed emails control options:
95 Processed emails control options:
96 move_on_success=MAILBOX move emails that were successfully received
96 move_on_success=MAILBOX move emails that were successfully received
97 to MAILBOX instead of deleting them
97 to MAILBOX instead of deleting them
98 move_on_failure=MAILBOX move emails that were ignored to MAILBOX
98 move_on_failure=MAILBOX move emails that were ignored to MAILBOX
99
99
100 Examples:
100 Examples:
101 # No project specified. Emails MUST contain the 'Project' keyword:
101 # No project specified. Emails MUST contain the 'Project' keyword:
102
102
103 rake redmine:email:receive_imap RAILS_ENV="production" \\
103 rake redmine:email:receive_imap RAILS_ENV="production" \\
104 host=imap.foo.bar username=redmine@example.net password=xxx
104 host=imap.foo.bar username=redmine@example.net password=xxx
105
105
106
106
107 # Fixed project and default tracker specified, but emails can override
107 # Fixed project and default tracker specified, but emails can override
108 # both tracker and priority attributes:
108 # both tracker and priority attributes:
109
109
110 rake redmine:email:receive_imap RAILS_ENV="production" \\
110 rake redmine:email:receive_imap RAILS_ENV="production" \\
111 host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
111 host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
112 project=foo \\
112 project=foo \\
113 tracker=bug \\
113 tracker=bug \\
114 allow_override=tracker,priority
114 allow_override=tracker,priority
115 END_DESC
115 END_DESC
116
116
117 task :receive_imap => :environment do
117 task :receive_imap => :environment do
118 imap_options = {:host => ENV['host'],
118 imap_options = {:host => ENV['host'],
119 :port => ENV['port'],
119 :port => ENV['port'],
120 :ssl => ENV['ssl'],
120 :ssl => ENV['ssl'],
121 :username => ENV['username'],
121 :username => ENV['username'],
122 :password => ENV['password'],
122 :password => ENV['password'],
123 :folder => ENV['folder'],
123 :folder => ENV['folder'],
124 :move_on_success => ENV['move_on_success'],
124 :move_on_success => ENV['move_on_success'],
125 :move_on_failure => ENV['move_on_failure']}
125 :move_on_failure => ENV['move_on_failure']}
126
126
127 Mailer.with_synched_deliveries do
127 Mailer.with_synched_deliveries do
128 Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV))
128 Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV))
129 end
129 end
130 end
130 end
131
131
132 desc <<-END_DESC
132 desc <<-END_DESC
133 Read emails from an POP3 server.
133 Read emails from an POP3 server.
134
134
135 Available POP3 options:
135 Available POP3 options:
136 host=HOST POP3 server host (default: 127.0.0.1)
136 host=HOST POP3 server host (default: 127.0.0.1)
137 port=PORT POP3 server port (default: 110)
137 port=PORT POP3 server port (default: 110)
138 username=USERNAME POP3 account
138 username=USERNAME POP3 account
139 password=PASSWORD POP3 password
139 password=PASSWORD POP3 password
140 apop=1 use APOP authentication (default: false)
140 apop=1 use APOP authentication (default: false)
141 ssl=SSL Use SSL? (default: false)
141 delete_unprocessed=1 delete messages that could not be processed
142 delete_unprocessed=1 delete messages that could not be processed
142 successfully from the server (default
143 successfully from the server (default
143 behaviour is to leave them on the server)
144 behaviour is to leave them on the server)
144
145
145 See redmine:email:receive_imap for more options and examples.
146 See redmine:email:receive_imap for more options and examples.
146 END_DESC
147 END_DESC
147
148
148 task :receive_pop3 => :environment do
149 task :receive_pop3 => :environment do
149 pop_options = {:host => ENV['host'],
150 pop_options = {:host => ENV['host'],
150 :port => ENV['port'],
151 :port => ENV['port'],
151 :apop => ENV['apop'],
152 :apop => ENV['apop'],
153 :ssl => ENV['ssl'],
152 :username => ENV['username'],
154 :username => ENV['username'],
153 :password => ENV['password'],
155 :password => ENV['password'],
154 :delete_unprocessed => ENV['delete_unprocessed']}
156 :delete_unprocessed => ENV['delete_unprocessed']}
155
157
156 Mailer.with_synched_deliveries do
158 Mailer.with_synched_deliveries do
157 Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV))
159 Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV))
158 end
160 end
159 end
161 end
160
162
161 desc "Send a test email to the user with the provided login name"
163 desc "Send a test email to the user with the provided login name"
162 task :test, [:login] => :environment do |task, args|
164 task :test, [:login] => :environment do |task, args|
163 include Redmine::I18n
165 include Redmine::I18n
164 abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
166 abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
165
167
166 user = User.find_by_login(args[:login])
168 user = User.find_by_login(args[:login])
167 abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
169 abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
168
170
169 ActionMailer::Base.raise_delivery_errors = true
171 ActionMailer::Base.raise_delivery_errors = true
170 begin
172 begin
171 Mailer.with_synched_deliveries do
173 Mailer.with_synched_deliveries do
172 Mailer.test_email(user).deliver
174 Mailer.test_email(user).deliver
173 end
175 end
174 puts l(:notice_email_sent, user.mail)
176 puts l(:notice_email_sent, user.mail)
175 rescue Exception => e
177 rescue Exception => e
176 abort l(:notice_email_error, e.message)
178 abort l(:notice_email_error, e.message)
177 end
179 end
178 end
180 end
179 end
181 end
180 end
182 end
General Comments 0
You need to be logged in to leave comments. Login now