To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / lib / tasks / email.rake @ 1298:4f746d8966dd

History | View | Annotate | Download (8.56 KB)

1 1115:433d4f72a19b Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 1115:433d4f72a19b Chris
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
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
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18
namespace :redmine do
19
  namespace :email do
20
21
    desc <<-END_DESC
22
Read an email from standard input.
23

24
General options:
25
  unknown_user=ACTION      how to handle emails from an unknown user
26
                           ACTION can be one of the following values:
27
                           ignore: email is ignored (default)
28
                           accept: accept as anonymous user
29
                           create: create a user account
30
  no_permission_check=1    disable permission checking when receiving
31
                           the email
32 1295:622f24f53b42 Chris
  no_account_notice=1      disable new user account notification
33
  default_group=foo,bar    adds created user to foo and bar groups
34 1115:433d4f72a19b Chris

35
Issue attributes control options:
36
  project=PROJECT          identifier of the target project
37
  status=STATUS            name of the target status
38
  tracker=TRACKER          name of the target tracker
39
  category=CATEGORY        name of the target category
40
  priority=PRIORITY        name of the target priority
41
  allow_override=ATTRS     allow email content to override attributes
42
                           specified by previous options
43
                           ATTRS is a comma separated list of attributes
44

45
Examples:
46
  # No project specified. Emails MUST contain the 'Project' keyword:
47
  rake redmine:email:read RAILS_ENV="production" < raw_email
48

49
  # Fixed project and default tracker specified, but emails can override
50
  # both tracker and priority attributes:
51
  rake redmine:email:read RAILS_ENV="production" \\
52
                  project=foo \\
53
                  tracker=bug \\
54
                  allow_override=tracker,priority < raw_email
55
END_DESC
56
57
    task :read => :environment do
58
      options = { :issue => {} }
59
      %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
60
      options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
61
      options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
62
      options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
63 1295:622f24f53b42 Chris
      options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice']
64
      options[:default_group] = ENV['default_group'] if ENV['default_group']
65 1115:433d4f72a19b Chris
66
      MailHandler.receive(STDIN.read, options)
67
    end
68
69
    desc <<-END_DESC
70
Read emails from an IMAP server.
71

72
General options:
73
  unknown_user=ACTION      how to handle emails from an unknown user
74
                           ACTION can be one of the following values:
75
                           ignore: email is ignored (default)
76
                           accept: accept as anonymous user
77
                           create: create a user account
78
  no_permission_check=1    disable permission checking when receiving
79
                           the email
80 1295:622f24f53b42 Chris
  no_account_notice=1      disable new user account notification
81
  default_group=foo,bar    adds created user to foo and bar groups
82 1115:433d4f72a19b Chris

83
Available IMAP options:
84
  host=HOST                IMAP server host (default: 127.0.0.1)
85
  port=PORT                IMAP server port (default: 143)
86
  ssl=SSL                  Use SSL? (default: false)
87
  username=USERNAME        IMAP account
88
  password=PASSWORD        IMAP password
89
  folder=FOLDER            IMAP folder to read (default: INBOX)
90

91
Issue attributes control options:
92
  project=PROJECT          identifier of the target project
93
  status=STATUS            name of the target status
94
  tracker=TRACKER          name of the target tracker
95
  category=CATEGORY        name of the target category
96
  priority=PRIORITY        name of the target priority
97
  allow_override=ATTRS     allow email content to override attributes
98
                           specified by previous options
99
                           ATTRS is a comma separated list of attributes
100

101
Processed emails control options:
102
  move_on_success=MAILBOX  move emails that were successfully received
103
                           to MAILBOX instead of deleting them
104
  move_on_failure=MAILBOX  move emails that were ignored to MAILBOX
105

106
Examples:
107
  # No project specified. Emails MUST contain the 'Project' keyword:
108

109
  rake redmine:email:receive_imap RAILS_ENV="production" \\
110
    host=imap.foo.bar username=redmine@example.net password=xxx
111

112

113
  # Fixed project and default tracker specified, but emails can override
114
  # both tracker and priority attributes:
115

116
  rake redmine:email:receive_imap RAILS_ENV="production" \\
117
    host=imap.foo.bar username=redmine@example.net password=xxx ssl=1 \\
118
    project=foo \\
119
    tracker=bug \\
120
    allow_override=tracker,priority
121
END_DESC
122
123
    task :receive_imap => :environment do
124
      imap_options = {:host => ENV['host'],
125
                      :port => ENV['port'],
126
                      :ssl => ENV['ssl'],
127
                      :username => ENV['username'],
128
                      :password => ENV['password'],
129
                      :folder => ENV['folder'],
130
                      :move_on_success => ENV['move_on_success'],
131
                      :move_on_failure => ENV['move_on_failure']}
132
133
      options = { :issue => {} }
134
      %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
135
      options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
136
      options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
137
      options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
138 1295:622f24f53b42 Chris
      options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice']
139
      options[:default_group] = ENV['default_group'] if ENV['default_group']
140 1115:433d4f72a19b Chris
141
      Redmine::IMAP.check(imap_options, options)
142
    end
143
144
    desc <<-END_DESC
145
Read emails from an POP3 server.
146

147
Available POP3 options:
148
  host=HOST                POP3 server host (default: 127.0.0.1)
149
  port=PORT                POP3 server port (default: 110)
150
  username=USERNAME        POP3 account
151
  password=PASSWORD        POP3 password
152
  apop=1                   use APOP authentication (default: false)
153
  delete_unprocessed=1     delete messages that could not be processed
154
                           successfully from the server (default
155
                           behaviour is to leave them on the server)
156

157
See redmine:email:receive_imap for more options and examples.
158
END_DESC
159
160
    task :receive_pop3 => :environment do
161
      pop_options  = {:host => ENV['host'],
162
                      :port => ENV['port'],
163
                      :apop => ENV['apop'],
164
                      :username => ENV['username'],
165
                      :password => ENV['password'],
166
                      :delete_unprocessed => ENV['delete_unprocessed']}
167
168
      options = { :issue => {} }
169
      %w(project status tracker category priority).each { |a| options[:issue][a.to_sym] = ENV[a] if ENV[a] }
170
      options[:allow_override] = ENV['allow_override'] if ENV['allow_override']
171
      options[:unknown_user] = ENV['unknown_user'] if ENV['unknown_user']
172
      options[:no_permission_check] = ENV['no_permission_check'] if ENV['no_permission_check']
173 1295:622f24f53b42 Chris
      options[:no_account_notice] = ENV['no_account_notice'] if ENV['no_account_notice']
174
      options[:default_group] = ENV['default_group'] if ENV['default_group']
175 1115:433d4f72a19b Chris
176
      Redmine::POP3.check(pop_options, options)
177
    end
178
179
    desc "Send a test email to the user with the provided login name"
180
    task :test, [:login] => :environment do |task, args|
181
      include Redmine::I18n
182
      abort l(:notice_email_error, "Please include the user login to test with. Example: rake redmine:email:test[login]") if args[:login].blank?
183
184
      user = User.find_by_login(args[:login])
185
      abort l(:notice_email_error, "User #{args[:login]} not found") unless user && user.logged?
186
187
      ActionMailer::Base.raise_delivery_errors = true
188
      begin
189
        Mailer.with_synched_deliveries do
190
          Mailer.test_email(user).deliver
191
        end
192
        puts l(:notice_email_sent, user.mail)
193
      rescue Exception => e
194
        abort l(:notice_email_error, e.message)
195
      end
196
    end
197
  end
198
end