##// END OF EJS Templates
Use async email deliveries in rake tasks (#16784)....
Jean-Philippe Lang -
r12848:2d3f3cd9aa45
parent child
Show More
@@ -1,174 +1,180
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 MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV))
58 Mailer.with_synched_deliveries do
59 MailHandler.receive(STDIN.read, MailHandler.extract_options_from_env(ENV))
60 end
59 end
61 end
60
62
61 desc <<-END_DESC
63 desc <<-END_DESC
62 Read emails from an IMAP server.
64 Read emails from an IMAP server.
63
65
64 General options:
66 General options:
65 unknown_user=ACTION how to handle emails from an unknown user
67 unknown_user=ACTION how to handle emails from an unknown user
66 ACTION can be one of the following values:
68 ACTION can be one of the following values:
67 ignore: email is ignored (default)
69 ignore: email is ignored (default)
68 accept: accept as anonymous user
70 accept: accept as anonymous user
69 create: create a user account
71 create: create a user account
70 no_permission_check=1 disable permission checking when receiving
72 no_permission_check=1 disable permission checking when receiving
71 the email
73 the email
72 no_account_notice=1 disable new user account notification
74 no_account_notice=1 disable new user account notification
73 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
74
76
75 Available IMAP options:
77 Available IMAP options:
76 host=HOST IMAP server host (default: 127.0.0.1)
78 host=HOST IMAP server host (default: 127.0.0.1)
77 port=PORT IMAP server port (default: 143)
79 port=PORT IMAP server port (default: 143)
78 ssl=SSL Use SSL? (default: false)
80 ssl=SSL Use SSL? (default: false)
79 username=USERNAME IMAP account
81 username=USERNAME IMAP account
80 password=PASSWORD IMAP password
82 password=PASSWORD IMAP password
81 folder=FOLDER IMAP folder to read (default: INBOX)
83 folder=FOLDER IMAP folder to read (default: INBOX)
82
84
83 Issue attributes control options:
85 Issue attributes control options:
84 project=PROJECT identifier of the target project
86 project=PROJECT identifier of the target project
85 status=STATUS name of the target status
87 status=STATUS name of the target status
86 tracker=TRACKER name of the target tracker
88 tracker=TRACKER name of the target tracker
87 category=CATEGORY name of the target category
89 category=CATEGORY name of the target category
88 priority=PRIORITY name of the target priority
90 priority=PRIORITY name of the target priority
89 allow_override=ATTRS allow email content to override attributes
91 allow_override=ATTRS allow email content to override attributes
90 specified by previous options
92 specified by previous options
91 ATTRS is a comma separated list of attributes
93 ATTRS is a comma separated list of attributes
92
94
93 Processed emails control options:
95 Processed emails control options:
94 move_on_success=MAILBOX move emails that were successfully received
96 move_on_success=MAILBOX move emails that were successfully received
95 to MAILBOX instead of deleting them
97 to MAILBOX instead of deleting them
96 move_on_failure=MAILBOX move emails that were ignored to MAILBOX
98 move_on_failure=MAILBOX move emails that were ignored to MAILBOX
97
99
98 Examples:
100 Examples:
99 # No project specified. Emails MUST contain the 'Project' keyword:
101 # No project specified. Emails MUST contain the 'Project' keyword:
100
102
101 rake redmine:email:receive_imap RAILS_ENV="production" \\
103 rake redmine:email:receive_imap RAILS_ENV="production" \\
102 host=imap.foo.bar username=redmine@example.net password=xxx
104 host=imap.foo.bar username=redmine@example.net password=xxx
103
105
104
106
105 # Fixed project and default tracker specified, but emails can override
107 # Fixed project and default tracker specified, but emails can override
106 # both tracker and priority attributes:
108 # both tracker and priority attributes:
107
109
108 rake redmine:email:receive_imap RAILS_ENV="production" \\
110 rake redmine:email:receive_imap RAILS_ENV="production" \\
109 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 \\
110 project=foo \\
112 project=foo \\
111 tracker=bug \\
113 tracker=bug \\
112 allow_override=tracker,priority
114 allow_override=tracker,priority
113 END_DESC
115 END_DESC
114
116
115 task :receive_imap => :environment do
117 task :receive_imap => :environment do
116 imap_options = {:host => ENV['host'],
118 imap_options = {:host => ENV['host'],
117 :port => ENV['port'],
119 :port => ENV['port'],
118 :ssl => ENV['ssl'],
120 :ssl => ENV['ssl'],
119 :username => ENV['username'],
121 :username => ENV['username'],
120 :password => ENV['password'],
122 :password => ENV['password'],
121 :folder => ENV['folder'],
123 :folder => ENV['folder'],
122 :move_on_success => ENV['move_on_success'],
124 :move_on_success => ENV['move_on_success'],
123 :move_on_failure => ENV['move_on_failure']}
125 :move_on_failure => ENV['move_on_failure']}
124
126
125 Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV))
127 Mailer.with_synched_deliveries do
128 Redmine::IMAP.check(imap_options, MailHandler.extract_options_from_env(ENV))
129 end
126 end
130 end
127
131
128 desc <<-END_DESC
132 desc <<-END_DESC
129 Read emails from an POP3 server.
133 Read emails from an POP3 server.
130
134
131 Available POP3 options:
135 Available POP3 options:
132 host=HOST POP3 server host (default: 127.0.0.1)
136 host=HOST POP3 server host (default: 127.0.0.1)
133 port=PORT POP3 server port (default: 110)
137 port=PORT POP3 server port (default: 110)
134 username=USERNAME POP3 account
138 username=USERNAME POP3 account
135 password=PASSWORD POP3 password
139 password=PASSWORD POP3 password
136 apop=1 use APOP authentication (default: false)
140 apop=1 use APOP authentication (default: false)
137 delete_unprocessed=1 delete messages that could not be processed
141 delete_unprocessed=1 delete messages that could not be processed
138 successfully from the server (default
142 successfully from the server (default
139 behaviour is to leave them on the server)
143 behaviour is to leave them on the server)
140
144
141 See redmine:email:receive_imap for more options and examples.
145 See redmine:email:receive_imap for more options and examples.
142 END_DESC
146 END_DESC
143
147
144 task :receive_pop3 => :environment do
148 task :receive_pop3 => :environment do
145 pop_options = {:host => ENV['host'],
149 pop_options = {:host => ENV['host'],
146 :port => ENV['port'],
150 :port => ENV['port'],
147 :apop => ENV['apop'],
151 :apop => ENV['apop'],
148 :username => ENV['username'],
152 :username => ENV['username'],
149 :password => ENV['password'],
153 :password => ENV['password'],
150 :delete_unprocessed => ENV['delete_unprocessed']}
154 :delete_unprocessed => ENV['delete_unprocessed']}
151
155
152 Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV))
156 Mailer.with_synched_deliveries do
157 Redmine::POP3.check(pop_options, MailHandler.extract_options_from_env(ENV))
158 end
153 end
159 end
154
160
155 desc "Send a test email to the user with the provided login name"
161 desc "Send a test email to the user with the provided login name"
156 task :test, [:login] => :environment do |task, args|
162 task :test, [:login] => :environment do |task, args|
157 include Redmine::I18n
163 include Redmine::I18n
158 abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
164 abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
159
165
160 user = User.find_by_login(args[:login])
166 user = User.find_by_login(args[:login])
161 abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
167 abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
162
168
163 ActionMailer::Base.raise_delivery_errors = true
169 ActionMailer::Base.raise_delivery_errors = true
164 begin
170 begin
165 Mailer.with_synched_deliveries do
171 Mailer.with_synched_deliveries do
166 Mailer.test_email(user).deliver
172 Mailer.test_email(user).deliver
167 end
173 end
168 puts l(:notice_email_sent, user.mail)
174 puts l(:notice_email_sent, user.mail)
169 rescue Exception => e
175 rescue Exception => e
170 abort l(:notice_email_error, e.message)
176 abort l(:notice_email_error, e.message)
171 end
177 end
172 end
178 end
173 end
179 end
174 end
180 end
General Comments 0
You need to be logged in to leave comments. Login now