@@ -1,307 +1,308 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 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 | class Mailer < ActionMailer::Base |
|
19 | 19 | helper :application |
|
20 | 20 | helper :issues |
|
21 | 21 | helper :custom_fields |
|
22 | 22 | |
|
23 | 23 | include ActionController::UrlWriter |
|
24 | 24 | include Redmine::I18n |
|
25 | 25 | |
|
26 | 26 | def self.default_url_options |
|
27 | 27 | h = Setting.host_name |
|
28 | 28 | h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? |
|
29 | 29 | { :host => h, :protocol => Setting.protocol } |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | def issue_add(issue) |
|
33 | 33 | redmine_headers 'Project' => issue.project.identifier, |
|
34 | 34 | 'Issue-Id' => issue.id, |
|
35 | 35 | 'Issue-Author' => issue.author.login |
|
36 | 36 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
37 | 37 | message_id issue |
|
38 | 38 | recipients issue.recipients |
|
39 | 39 | cc(issue.watcher_recipients - @recipients) |
|
40 | 40 | subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" |
|
41 | 41 | body :issue => issue, |
|
42 | 42 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | def issue_edit(journal) |
|
46 | 46 | issue = journal.journalized |
|
47 | 47 | redmine_headers 'Project' => issue.project.identifier, |
|
48 | 48 | 'Issue-Id' => issue.id, |
|
49 | 49 | 'Issue-Author' => issue.author.login |
|
50 | 50 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
51 | 51 | message_id journal |
|
52 | 52 | references issue |
|
53 | 53 | @author = journal.user |
|
54 | 54 | recipients issue.recipients |
|
55 | 55 | # Watchers in cc |
|
56 | 56 | cc(issue.watcher_recipients - @recipients) |
|
57 | 57 | s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " |
|
58 | 58 | s << "(#{issue.status.name}) " if journal.new_value_for('status_id') |
|
59 | 59 | s << issue.subject |
|
60 | 60 | subject s |
|
61 | 61 | body :issue => issue, |
|
62 | 62 | :journal => journal, |
|
63 | 63 | :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def reminder(user, issues, days) |
|
67 | 67 | set_language_if_valid user.language |
|
68 | 68 | recipients user.mail |
|
69 | 69 | subject l(:mail_subject_reminder, issues.size) |
|
70 | 70 | body :issues => issues, |
|
71 | 71 | :days => days, |
|
72 | 72 | :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc') |
|
73 | 73 | end |
|
74 | 74 | |
|
75 | 75 | def document_added(document) |
|
76 | 76 | redmine_headers 'Project' => document.project.identifier |
|
77 | 77 | recipients document.project.recipients |
|
78 | 78 | subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" |
|
79 | 79 | body :document => document, |
|
80 | 80 | :document_url => url_for(:controller => 'documents', :action => 'show', :id => document) |
|
81 | 81 | end |
|
82 | 82 | |
|
83 | 83 | def attachments_added(attachments) |
|
84 | 84 | container = attachments.first.container |
|
85 | 85 | added_to = '' |
|
86 | 86 | added_to_url = '' |
|
87 | 87 | case container.class.name |
|
88 | 88 | when 'Project' |
|
89 | 89 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container) |
|
90 | 90 | added_to = "#{l(:label_project)}: #{container}" |
|
91 | 91 | when 'Version' |
|
92 | 92 | added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id) |
|
93 | 93 | added_to = "#{l(:label_version)}: #{container.name}" |
|
94 | 94 | when 'Document' |
|
95 | 95 | added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) |
|
96 | 96 | added_to = "#{l(:label_document)}: #{container.title}" |
|
97 | 97 | end |
|
98 | 98 | redmine_headers 'Project' => container.project.identifier |
|
99 | 99 | recipients container.project.recipients |
|
100 | 100 | subject "[#{container.project.name}] #{l(:label_attachment_new)}" |
|
101 | 101 | body :attachments => attachments, |
|
102 | 102 | :added_to => added_to, |
|
103 | 103 | :added_to_url => added_to_url |
|
104 | 104 | end |
|
105 | 105 | |
|
106 | 106 | def news_added(news) |
|
107 | 107 | redmine_headers 'Project' => news.project.identifier |
|
108 | 108 | message_id news |
|
109 | 109 | recipients news.project.recipients |
|
110 | 110 | subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}" |
|
111 | 111 | body :news => news, |
|
112 | 112 | :news_url => url_for(:controller => 'news', :action => 'show', :id => news) |
|
113 | 113 | end |
|
114 | 114 | |
|
115 | 115 | def message_posted(message, recipients) |
|
116 | 116 | redmine_headers 'Project' => message.project.identifier, |
|
117 | 117 | 'Topic-Id' => (message.parent_id || message.id) |
|
118 | 118 | message_id message |
|
119 | 119 | references message.parent unless message.parent.nil? |
|
120 | 120 | recipients(recipients) |
|
121 | 121 | subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" |
|
122 | 122 | body :message => message, |
|
123 | 123 | :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root) |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | def account_information(user, password) |
|
127 | 127 | set_language_if_valid user.language |
|
128 | 128 | recipients user.mail |
|
129 | 129 | subject l(:mail_subject_register, Setting.app_title) |
|
130 | 130 | body :user => user, |
|
131 | 131 | :password => password, |
|
132 | 132 | :login_url => url_for(:controller => 'account', :action => 'login') |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def account_activation_request(user) |
|
136 | 136 | # Send the email to all active administrators |
|
137 | 137 | recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact |
|
138 | 138 | subject l(:mail_subject_account_activation_request, Setting.app_title) |
|
139 | 139 | body :user => user, |
|
140 | 140 | :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc') |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | # A registered user's account was activated by an administrator |
|
144 | 144 | def account_activated(user) |
|
145 | 145 | set_language_if_valid user.language |
|
146 | 146 | recipients user.mail |
|
147 | 147 | subject l(:mail_subject_register, Setting.app_title) |
|
148 | 148 | body :user => user, |
|
149 | 149 | :login_url => url_for(:controller => 'account', :action => 'login') |
|
150 | 150 | end |
|
151 | 151 | |
|
152 | 152 | def lost_password(token) |
|
153 | 153 | set_language_if_valid(token.user.language) |
|
154 | 154 | recipients token.user.mail |
|
155 | 155 | subject l(:mail_subject_lost_password, Setting.app_title) |
|
156 | 156 | body :token => token, |
|
157 | 157 | :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value) |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | def register(token) |
|
161 | 161 | set_language_if_valid(token.user.language) |
|
162 | 162 | recipients token.user.mail |
|
163 | 163 | subject l(:mail_subject_register, Setting.app_title) |
|
164 | 164 | body :token => token, |
|
165 | 165 | :url => url_for(:controller => 'account', :action => 'activate', :token => token.value) |
|
166 | 166 | end |
|
167 | 167 | |
|
168 | 168 | def test(user) |
|
169 | 169 | set_language_if_valid(user.language) |
|
170 | 170 | recipients user.mail |
|
171 | 171 | subject 'Redmine test' |
|
172 | 172 | body :url => url_for(:controller => 'welcome') |
|
173 | 173 | end |
|
174 | 174 | |
|
175 | 175 | # Overrides default deliver! method to prevent from sending an email |
|
176 | 176 | # with no recipient, cc or bcc |
|
177 | 177 | def deliver!(mail = @mail) |
|
178 | 178 | return false if (recipients.nil? || recipients.empty?) && |
|
179 | 179 | (cc.nil? || cc.empty?) && |
|
180 | 180 | (bcc.nil? || bcc.empty?) |
|
181 | 181 | |
|
182 | 182 | # Set Message-Id and References |
|
183 | 183 | if @message_id_object |
|
184 | 184 | mail.message_id = self.class.message_id_for(@message_id_object) |
|
185 | 185 | end |
|
186 | 186 | if @references_objects |
|
187 | 187 | mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} |
|
188 | 188 | end |
|
189 | 189 | super(mail) |
|
190 | 190 | end |
|
191 | 191 | |
|
192 | 192 | # Sends reminders to issue assignees |
|
193 | 193 | # Available options: |
|
194 | 194 | # * :days => how many days in the future to remind about (defaults to 7) |
|
195 | 195 | # * :tracker => id of tracker for filtering issues (defaults to all trackers) |
|
196 | 196 | # * :project => id or identifier of project to process (defaults to all projects) |
|
197 | 197 | def self.reminders(options={}) |
|
198 | 198 | days = options[:days] || 7 |
|
199 | 199 | project = options[:project] ? Project.find(options[:project]) : nil |
|
200 | 200 | tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil |
|
201 | 201 | |
|
202 | 202 | s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date] |
|
203 | 203 | s << "#{Issue.table_name}.assigned_to_id IS NOT NULL" |
|
204 | 204 | s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}" |
|
205 | 205 | s << "#{Issue.table_name}.project_id = #{project.id}" if project |
|
206 | 206 | s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker |
|
207 | 207 | |
|
208 | 208 | issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], |
|
209 | 209 | :conditions => s.conditions |
|
210 | 210 | ).group_by(&:assigned_to) |
|
211 | 211 | issues_by_assignee.each do |assignee, issues| |
|
212 | 212 | deliver_reminder(assignee, issues, days) unless assignee.nil? |
|
213 | 213 | end |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | private |
|
217 | 217 | def initialize_defaults(method_name) |
|
218 | 218 | super |
|
219 | 219 | set_language_if_valid Setting.default_language |
|
220 | 220 | from Setting.mail_from |
|
221 | 221 | |
|
222 | 222 | # Common headers |
|
223 | 223 | headers 'X-Mailer' => 'Redmine', |
|
224 | 224 | 'X-Redmine-Host' => Setting.host_name, |
|
225 | 'X-Redmine-Site' => Setting.app_title | |
|
225 | 'X-Redmine-Site' => Setting.app_title, | |
|
226 | 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" | |
|
226 | 227 | end |
|
227 | 228 | |
|
228 | 229 | # Appends a Redmine header field (name is prepended with 'X-Redmine-') |
|
229 | 230 | def redmine_headers(h) |
|
230 | 231 | h.each { |k,v| headers["X-Redmine-#{k}"] = v } |
|
231 | 232 | end |
|
232 | 233 | |
|
233 | 234 | # Overrides the create_mail method |
|
234 | 235 | def create_mail |
|
235 | 236 | # Removes the current user from the recipients and cc |
|
236 | 237 | # if he doesn't want to receive notifications about what he does |
|
237 | 238 | @author ||= User.current |
|
238 | 239 | if @author.pref[:no_self_notified] |
|
239 | 240 | recipients.delete(@author.mail) if recipients |
|
240 | 241 | cc.delete(@author.mail) if cc |
|
241 | 242 | end |
|
242 | 243 | # Blind carbon copy recipients |
|
243 | 244 | if Setting.bcc_recipients? |
|
244 | 245 | bcc([recipients, cc].flatten.compact.uniq) |
|
245 | 246 | recipients [] |
|
246 | 247 | cc [] |
|
247 | 248 | end |
|
248 | 249 | super |
|
249 | 250 | end |
|
250 | 251 | |
|
251 | 252 | # Renders a message with the corresponding layout |
|
252 | 253 | def render_message(method_name, body) |
|
253 | 254 | layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml' |
|
254 | 255 | body[:content_for_layout] = render(:file => method_name, :body => body) |
|
255 | 256 | ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true) |
|
256 | 257 | end |
|
257 | 258 | |
|
258 | 259 | # for the case of plain text only |
|
259 | 260 | def body(*params) |
|
260 | 261 | value = super(*params) |
|
261 | 262 | if Setting.plain_text_mail? |
|
262 | 263 | templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}") |
|
263 | 264 | unless String === @body or templates.empty? |
|
264 | 265 | template = File.basename(templates.first) |
|
265 | 266 | @body[:content_for_layout] = render(:file => template, :body => @body) |
|
266 | 267 | @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true) |
|
267 | 268 | return @body |
|
268 | 269 | end |
|
269 | 270 | end |
|
270 | 271 | return value |
|
271 | 272 | end |
|
272 | 273 | |
|
273 | 274 | # Makes partial rendering work with Rails 1.2 (retro-compatibility) |
|
274 | 275 | def self.controller_path |
|
275 | 276 | '' |
|
276 | 277 | end unless respond_to?('controller_path') |
|
277 | 278 | |
|
278 | 279 | # Returns a predictable Message-Id for the given object |
|
279 | 280 | def self.message_id_for(object) |
|
280 | 281 | # id + timestamp should reduce the odds of a collision |
|
281 | 282 | # as far as we don't send multiple emails for the same object |
|
282 | 283 | hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{object.created_on.strftime("%Y%m%d%H%M%S")}" |
|
283 | 284 | host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') |
|
284 | 285 | host = "#{::Socket.gethostname}.redmine" if host.empty? |
|
285 | 286 | "<#{hash}@#{host}>" |
|
286 | 287 | end |
|
287 | 288 | |
|
288 | 289 | private |
|
289 | 290 | |
|
290 | 291 | def message_id(object) |
|
291 | 292 | @message_id_object = object |
|
292 | 293 | end |
|
293 | 294 | |
|
294 | 295 | def references(object) |
|
295 | 296 | @references_objects ||= [] |
|
296 | 297 | @references_objects << object |
|
297 | 298 | end |
|
298 | 299 | end |
|
299 | 300 | |
|
300 | 301 | # Patch TMail so that message_id is not overwritten |
|
301 | 302 | module TMail |
|
302 | 303 | class Mail |
|
303 | 304 | def add_message_id( fqdn = nil ) |
|
304 | 305 | self.message_id ||= ::TMail::new_message_id(fqdn) |
|
305 | 306 | end |
|
306 | 307 | end |
|
307 | 308 | end |
General Comments 0
You need to be logged in to leave comments.
Login now