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