##// END OF EJS Templates
Fixes code comments....
Jean-Philippe Lang -
r2657:fe8f4e5b8794
parent child
Show More
@@ -1,385 +1,385
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 # Builds a tmail object used to email recipients of the added issue.
33 33 #
34 34 # Example:
35 35 # issue_add(issue) => tmail object
36 36 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients
37 37 def issue_add(issue)
38 38 redmine_headers 'Project' => issue.project.identifier,
39 39 'Issue-Id' => issue.id,
40 40 'Issue-Author' => issue.author.login
41 41 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
42 42 message_id issue
43 43 recipients issue.recipients
44 44 cc(issue.watcher_recipients - @recipients)
45 45 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
46 46 body :issue => issue,
47 47 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
48 48 end
49 49
50 50 # Builds a tmail object used to email recipients of the edited issue.
51 51 #
52 52 # Example:
53 53 # issue_edit(journal) => tmail object
54 54 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients
55 55 def issue_edit(journal)
56 56 issue = journal.journalized.reload
57 57 redmine_headers 'Project' => issue.project.identifier,
58 58 'Issue-Id' => issue.id,
59 59 'Issue-Author' => issue.author.login
60 60 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
61 61 message_id journal
62 62 references issue
63 63 @author = journal.user
64 64 recipients issue.recipients
65 65 # Watchers in cc
66 66 cc(issue.watcher_recipients - @recipients)
67 67 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
68 68 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
69 69 s << issue.subject
70 70 subject s
71 71 body :issue => issue,
72 72 :journal => journal,
73 73 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
74 74 end
75 75
76 76 def reminder(user, issues, days)
77 77 set_language_if_valid user.language
78 78 recipients user.mail
79 79 subject l(:mail_subject_reminder, issues.size)
80 80 body :issues => issues,
81 81 :days => days,
82 82 :issues_url => url_for(:controller => 'issues', :action => 'index', :set_filter => 1, :assigned_to_id => user.id, :sort_key => 'due_date', :sort_order => 'asc')
83 83 end
84 84
85 85 # Builds a tmail object used to email users belonging to the added document's project.
86 86 #
87 87 # Example:
88 88 # document_added(document) => tmail object
89 89 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients
90 90 def document_added(document)
91 91 redmine_headers 'Project' => document.project.identifier
92 92 recipients document.project.recipients
93 93 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
94 94 body :document => document,
95 95 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
96 96 end
97 97
98 98 # Builds a tmail object used to email recipients of a project when an attachements are added.
99 99 #
100 100 # Example:
101 101 # attachments_added(attachments) => tmail object
102 102 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients
103 103 def attachments_added(attachments)
104 104 container = attachments.first.container
105 105 added_to = ''
106 106 added_to_url = ''
107 107 case container.class.name
108 108 when 'Project'
109 109 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container)
110 110 added_to = "#{l(:label_project)}: #{container}"
111 111 when 'Version'
112 112 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
113 113 added_to = "#{l(:label_version)}: #{container.name}"
114 114 when 'Document'
115 115 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
116 116 added_to = "#{l(:label_document)}: #{container.title}"
117 117 end
118 118 redmine_headers 'Project' => container.project.identifier
119 119 recipients container.project.recipients
120 120 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
121 121 body :attachments => attachments,
122 122 :added_to => added_to,
123 123 :added_to_url => added_to_url
124 124 end
125 125
126 126 # Builds a tmail object used to email recipients of a news' project when a news item is added.
127 127 #
128 128 # Example:
129 129 # news_added(news) => tmail object
130 130 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients
131 131 def news_added(news)
132 132 redmine_headers 'Project' => news.project.identifier
133 133 message_id news
134 134 recipients news.project.recipients
135 135 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
136 136 body :news => news,
137 137 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
138 138 end
139 139
140 140 # Builds a tmail object used to email the specified recipients of the specified message that was posted.
141 141 #
142 142 # Example:
143 143 # message_posted(message, recipients) => tmail object
144 144 # Mailer.deliver_message_posted(message, recipients) => sends an email to the recipients
145 145 def message_posted(message, recipients)
146 146 redmine_headers 'Project' => message.project.identifier,
147 147 'Topic-Id' => (message.parent_id || message.id)
148 148 message_id message
149 149 references message.parent unless message.parent.nil?
150 150 recipients(recipients)
151 151 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
152 152 body :message => message,
153 153 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
154 154 end
155 155
156 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
156 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added.
157 157 #
158 158 # Example:
159 # wiki_content_updated(wiki_content) => tmail object
160 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
159 # wiki_content_added(wiki_content) => tmail object
160 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients
161 161 def wiki_content_added(wiki_content)
162 162 redmine_headers 'Project' => wiki_content.project.identifier,
163 163 'Wiki-Page-Id' => wiki_content.page.id
164 164 message_id wiki_content
165 165 recipients wiki_content.project.recipients
166 166 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :page => wiki_content.page.pretty_title)}"
167 167 body :wiki_content => wiki_content,
168 168 :wiki_content_url => url_for(:controller => 'wiki', :action => 'index', :id => wiki_content.project, :page => wiki_content.page.title)
169 169 end
170 170
171 171 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated.
172 172 #
173 173 # Example:
174 174 # wiki_content_updated(wiki_content) => tmail object
175 175 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients
176 176 def wiki_content_updated(wiki_content)
177 177 redmine_headers 'Project' => wiki_content.project.identifier,
178 178 'Wiki-Page-Id' => wiki_content.page.id
179 179 message_id wiki_content
180 180 recipients wiki_content.project.recipients
181 181 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :page => wiki_content.page.pretty_title)}"
182 182 body :wiki_content => wiki_content,
183 183 :wiki_content_url => url_for(:controller => 'wiki', :action => 'index', :id => wiki_content.project, :page => wiki_content.page.title),
184 184 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff', :id => wiki_content.project, :page => wiki_content.page.title, :version => wiki_content.version)
185 185 end
186 186
187 187 # Builds a tmail object used to email the specified user their account information.
188 188 #
189 189 # Example:
190 190 # account_information(user, password) => tmail object
191 191 # Mailer.deliver_account_information(user, password) => sends account information to the user
192 192 def account_information(user, password)
193 193 set_language_if_valid user.language
194 194 recipients user.mail
195 195 subject l(:mail_subject_register, Setting.app_title)
196 196 body :user => user,
197 197 :password => password,
198 198 :login_url => url_for(:controller => 'account', :action => 'login')
199 199 end
200 200
201 201 # Builds a tmail object used to email all active administrators of an account activation request.
202 202 #
203 203 # Example:
204 204 # account_activation_request(user) => tmail object
205 205 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
206 206 def account_activation_request(user)
207 207 # Send the email to all active administrators
208 208 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
209 209 subject l(:mail_subject_account_activation_request, Setting.app_title)
210 210 body :user => user,
211 211 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
212 212 end
213 213
214 214 # Builds a tmail object used to email the specified user that their account was activated by an administrator.
215 215 #
216 216 # Example:
217 217 # account_activated(user) => tmail object
218 218 # Mailer.deliver_account_activated(user) => sends an email to the registered user
219 219 def account_activated(user)
220 220 set_language_if_valid user.language
221 221 recipients user.mail
222 222 subject l(:mail_subject_register, Setting.app_title)
223 223 body :user => user,
224 224 :login_url => url_for(:controller => 'account', :action => 'login')
225 225 end
226 226
227 227 def lost_password(token)
228 228 set_language_if_valid(token.user.language)
229 229 recipients token.user.mail
230 230 subject l(:mail_subject_lost_password, Setting.app_title)
231 231 body :token => token,
232 232 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
233 233 end
234 234
235 235 def register(token)
236 236 set_language_if_valid(token.user.language)
237 237 recipients token.user.mail
238 238 subject l(:mail_subject_register, Setting.app_title)
239 239 body :token => token,
240 240 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
241 241 end
242 242
243 243 def test(user)
244 244 set_language_if_valid(user.language)
245 245 recipients user.mail
246 246 subject 'Redmine test'
247 247 body :url => url_for(:controller => 'welcome')
248 248 end
249 249
250 250 # Overrides default deliver! method to prevent from sending an email
251 251 # with no recipient, cc or bcc
252 252 def deliver!(mail = @mail)
253 253 return false if (recipients.nil? || recipients.empty?) &&
254 254 (cc.nil? || cc.empty?) &&
255 255 (bcc.nil? || bcc.empty?)
256 256
257 257 # Set Message-Id and References
258 258 if @message_id_object
259 259 mail.message_id = self.class.message_id_for(@message_id_object)
260 260 end
261 261 if @references_objects
262 262 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
263 263 end
264 264 super(mail)
265 265 end
266 266
267 267 # Sends reminders to issue assignees
268 268 # Available options:
269 269 # * :days => how many days in the future to remind about (defaults to 7)
270 270 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
271 271 # * :project => id or identifier of project to process (defaults to all projects)
272 272 def self.reminders(options={})
273 273 days = options[:days] || 7
274 274 project = options[:project] ? Project.find(options[:project]) : nil
275 275 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
276 276
277 277 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date]
278 278 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL"
279 279 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}"
280 280 s << "#{Issue.table_name}.project_id = #{project.id}" if project
281 281 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker
282 282
283 283 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker],
284 284 :conditions => s.conditions
285 285 ).group_by(&:assigned_to)
286 286 issues_by_assignee.each do |assignee, issues|
287 287 deliver_reminder(assignee, issues, days) unless assignee.nil?
288 288 end
289 289 end
290 290
291 291 private
292 292 def initialize_defaults(method_name)
293 293 super
294 294 set_language_if_valid Setting.default_language
295 295 from Setting.mail_from
296 296
297 297 # Common headers
298 298 headers 'X-Mailer' => 'Redmine',
299 299 'X-Redmine-Host' => Setting.host_name,
300 300 'X-Redmine-Site' => Setting.app_title,
301 301 'Precedence' => 'bulk',
302 302 'Auto-Submitted' => 'auto-generated'
303 303 end
304 304
305 305 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
306 306 def redmine_headers(h)
307 307 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
308 308 end
309 309
310 310 # Overrides the create_mail method
311 311 def create_mail
312 312 # Removes the current user from the recipients and cc
313 313 # if he doesn't want to receive notifications about what he does
314 314 @author ||= User.current
315 315 if @author.pref[:no_self_notified]
316 316 recipients.delete(@author.mail) if recipients
317 317 cc.delete(@author.mail) if cc
318 318 end
319 319 # Blind carbon copy recipients
320 320 if Setting.bcc_recipients?
321 321 bcc([recipients, cc].flatten.compact.uniq)
322 322 recipients []
323 323 cc []
324 324 end
325 325 super
326 326 end
327 327
328 328 # Renders a message with the corresponding layout
329 329 def render_message(method_name, body)
330 330 layout = method_name.to_s.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
331 331 body[:content_for_layout] = render(:file => method_name, :body => body)
332 332 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}", :use_full_path => true)
333 333 end
334 334
335 335 # for the case of plain text only
336 336 def body(*params)
337 337 value = super(*params)
338 338 if Setting.plain_text_mail?
339 339 templates = Dir.glob("#{template_path}/#{@template}.text.plain.{rhtml,erb}")
340 340 unless String === @body or templates.empty?
341 341 template = File.basename(templates.first)
342 342 @body[:content_for_layout] = render(:file => template, :body => @body)
343 343 @body = ActionView::Base.new(template_root, @body, self).render(:file => "mailer/layout.text.plain.rhtml", :use_full_path => true)
344 344 return @body
345 345 end
346 346 end
347 347 return value
348 348 end
349 349
350 350 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
351 351 def self.controller_path
352 352 ''
353 353 end unless respond_to?('controller_path')
354 354
355 355 # Returns a predictable Message-Id for the given object
356 356 def self.message_id_for(object)
357 357 # id + timestamp should reduce the odds of a collision
358 358 # as far as we don't send multiple emails for the same object
359 359 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
360 360 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
361 361 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
362 362 host = "#{::Socket.gethostname}.redmine" if host.empty?
363 363 "<#{hash}@#{host}>"
364 364 end
365 365
366 366 private
367 367
368 368 def message_id(object)
369 369 @message_id_object = object
370 370 end
371 371
372 372 def references(object)
373 373 @references_objects ||= []
374 374 @references_objects << object
375 375 end
376 376 end
377 377
378 378 # Patch TMail so that message_id is not overwritten
379 379 module TMail
380 380 class Mail
381 381 def add_message_id( fqdn = nil )
382 382 self.message_id ||= ::TMail::new_message_id(fqdn)
383 383 end
384 384 end
385 385 end
General Comments 0
You need to be logged in to leave comments. Login now