comparison app/models/mailer.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents b2f7f52a164d
children 4f746d8966dd 51364c0cd58f
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 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.
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 require 'ar_condition'
19
20 class Mailer < ActionMailer::Base 18 class Mailer < ActionMailer::Base
21 layout 'mailer' 19 layout 'mailer'
22 helper :application 20 helper :application
23 helper :issues 21 helper :issues
24 helper :custom_fields 22 helper :custom_fields
25 23
26 include ActionController::UrlWriter
27 include Redmine::I18n 24 include Redmine::I18n
28 25
29 def self.default_url_options 26 def self.default_url_options
30 h = Setting.host_name 27 { :host => Setting.host_name, :protocol => Setting.protocol }
31 h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? 28 end
32 { :host => h, :protocol => Setting.protocol } 29
33 end 30 # Builds a Mail::Message object used to email the specified member
34 31 # that he was added to a project
35 # todo: luisf: 2Aug2012 - refactor... 32 #
36 def added_to_project(member, project) 33 # Example:
34 # member_added_to_project(member, project) => Mail::Message object
35 # Mailer.member_added_to_project(member, project) => sends an email to the registered member
36 def member_added_to_project(member, project)
37
37 principal = Principal.find(member.user_id) 38 principal = Principal.find(member.user_id)
38 39
40 users = []
39 if principal.type == "User" 41 if principal.type == "User"
40 user = User.find(member.user_id) 42 users = [User.find(member.user_id)]
41 user_add_to_project(user, project)
42 else 43 else
43 users = Principal.find(member.user_id).users 44 users = Principal.find(member.user_id).users
44 users.map {|user| user_add_to_project(user, project) } 45 end
45 end 46
46 end 47 users.map do |user|
47 48
48 # Builds a tmail object used to email the specified user that he was added to a project 49 set_language_if_valid user.language
49 # 50 @project_url = url_for(:controller => 'projects', :action => 'show', :id => project.id)
50 # Example: 51 @project_name = project.name
51 # user_add_to_project(user, project) => tmail object 52 mail :to => user.mail,
52 # Mailer.deliver_add_to_project(user, project) => sends an email to the registered user 53 :subject => l(:mail_subject_added_to_project, Setting.app_title)
53 def user_add_to_project(user, project) 54
54 set_language_if_valid user.language 55 end
55 recipients user.mail
56 subject l(:mail_subject_added_to_project, Setting.app_title)
57 body :project_url => url_for(:controller => 'projects', :action => 'show', :id => project.id),
58 :project_name => project.name
59 render_multipart('added_to_project', body)
60 end 56 end
61 57
62 # Builds a tmail object used to email recipients of the added issue. 58 # Builds a Mail::Message object used to email recipients of the added issue.
63 # 59 #
64 # Example: 60 # Example:
65 # issue_add(issue) => tmail object 61 # issue_add(issue) => Mail::Message object
66 # Mailer.deliver_issue_add(issue) => sends an email to issue recipients 62 # Mailer.issue_add(issue).deliver => sends an email to issue recipients
67 def issue_add(issue) 63 def issue_add(issue)
68 redmine_headers 'Project' => issue.project.identifier, 64 redmine_headers 'Project' => issue.project.identifier,
69 'Issue-Id' => issue.id, 65 'Issue-Id' => issue.id,
70 'Issue-Author' => issue.author.login 66 'Issue-Author' => issue.author.login
71 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to 67 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
72 message_id issue 68 message_id issue
73 recipients issue.recipients 69 @author = issue.author
74 cc(issue.watcher_recipients - @recipients) 70 @issue = issue
75 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" 71 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
76 body :issue => issue, 72 recipients = issue.recipients
77 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) 73 cc = issue.watcher_recipients - recipients
78 render_multipart('issue_add', body) 74 mail :to => recipients,
79 end 75 :cc => cc,
80 76 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
81 # Builds a tmail object used to email recipients of the edited issue. 77 end
82 # 78
83 # Example: 79 # Builds a Mail::Message object used to email recipients of the edited issue.
84 # issue_edit(journal) => tmail object 80 #
85 # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients 81 # Example:
82 # issue_edit(journal) => Mail::Message object
83 # Mailer.issue_edit(journal).deliver => sends an email to issue recipients
86 def issue_edit(journal) 84 def issue_edit(journal)
87 issue = journal.journalized.reload 85 issue = journal.journalized.reload
88 redmine_headers 'Project' => issue.project.identifier, 86 redmine_headers 'Project' => issue.project.identifier,
89 'Issue-Id' => issue.id, 87 'Issue-Id' => issue.id,
90 'Issue-Author' => issue.author.login 88 'Issue-Author' => issue.author.login
91 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to 89 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
92 message_id journal 90 message_id journal
93 references issue 91 references issue
94 @author = journal.user 92 @author = journal.user
95 recipients issue.recipients 93 recipients = journal.recipients
96 # Watchers in cc 94 # Watchers in cc
97 cc(issue.watcher_recipients - @recipients) 95 cc = journal.watcher_recipients - recipients
98 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " 96 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
99 s << "(#{issue.status.name}) " if journal.new_value_for('status_id') 97 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
100 s << issue.subject 98 s << issue.subject
101 subject s 99 @issue = issue
102 body :issue => issue, 100 @journal = journal
103 :journal => journal, 101 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
104 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") 102 mail :to => recipients,
105 103 :cc => cc,
106 render_multipart('issue_edit', body) 104 :subject => s
107 end 105 end
108 106
109 def reminder(user, issues, days) 107 def reminder(user, issues, days)
110 set_language_if_valid user.language 108 set_language_if_valid user.language
111 recipients user.mail 109 @issues = issues
112 subject l(:mail_subject_reminder, :count => issues.size, :days => days) 110 @days = days
113 body :issues => issues, 111 @issues_url = url_for(:controller => 'issues', :action => 'index',
114 :days => days,
115 :issues_url => url_for(:controller => 'issues', :action => 'index',
116 :set_filter => 1, :assigned_to_id => user.id, 112 :set_filter => 1, :assigned_to_id => user.id,
117 :sort => 'due_date:asc') 113 :sort => 'due_date:asc')
118 render_multipart('reminder', body) 114 mail :to => user.mail,
119 end 115 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
120 116 end
121 # Builds a tmail object used to email users belonging to the added document's project. 117
122 # 118 # Builds a Mail::Message object used to email users belonging to the added document's project.
123 # Example: 119 #
124 # document_added(document) => tmail object 120 # Example:
125 # Mailer.deliver_document_added(document) => sends an email to the document's project recipients 121 # document_added(document) => Mail::Message object
122 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
126 def document_added(document) 123 def document_added(document)
127 redmine_headers 'Project' => document.project.identifier 124 redmine_headers 'Project' => document.project.identifier
128 recipients document.recipients 125 @author = User.current
129 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" 126 @document = document
130 body :document => document, 127 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
131 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document) 128 mail :to => document.recipients,
132 render_multipart('document_added', body) 129 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
133 end 130 end
134 131
135 # Builds a tmail object used to email recipients of a project when an attachements are added. 132 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
136 # 133 #
137 # Example: 134 # Example:
138 # attachments_added(attachments) => tmail object 135 # attachments_added(attachments) => Mail::Message object
139 # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients 136 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
140 def attachments_added(attachments) 137 def attachments_added(attachments)
141 container = attachments.first.container 138 container = attachments.first.container
142 added_to = '' 139 added_to = ''
143 added_to_url = '' 140 added_to_url = ''
141 @author = attachments.first.author
144 case container.class.name 142 case container.class.name
145 when 'Project' 143 when 'Project'
146 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) 144 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
147 added_to = "#{l(:label_project)}: #{container}" 145 added_to = "#{l(:label_project)}: #{container}"
148 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 146 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
149 when 'Version' 147 when 'Version'
150 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) 148 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
151 added_to = "#{l(:label_version)}: #{container.name}" 149 added_to = "#{l(:label_version)}: #{container.name}"
152 recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} 150 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
153 when 'Document' 151 when 'Document'
154 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) 152 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
155 added_to = "#{l(:label_document)}: #{container.title}" 153 added_to = "#{l(:label_document)}: #{container.title}"
156 recipients container.recipients 154 recipients = container.recipients
157 end 155 end
158 redmine_headers 'Project' => container.project.identifier 156 redmine_headers 'Project' => container.project.identifier
159 subject "[#{container.project.name}] #{l(:label_attachment_new)}" 157 @attachments = attachments
160 body :attachments => attachments, 158 @added_to = added_to
161 :added_to => added_to, 159 @added_to_url = added_to_url
162 :added_to_url => added_to_url 160 mail :to => recipients,
163 render_multipart('attachments_added', body) 161 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
164 end 162 end
165 163
166 # Builds a tmail object used to email recipients of a news' project when a news item is added. 164 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
167 # 165 #
168 # Example: 166 # Example:
169 # news_added(news) => tmail object 167 # news_added(news) => Mail::Message object
170 # Mailer.deliver_news_added(news) => sends an email to the news' project recipients 168 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
171 def news_added(news) 169 def news_added(news)
172 redmine_headers 'Project' => news.project.identifier 170 redmine_headers 'Project' => news.project.identifier
171 @author = news.author
173 message_id news 172 message_id news
174 recipients news.recipients 173 @news = news
175 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}" 174 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
176 body :news => news, 175 mail :to => news.recipients,
177 :news_url => url_for(:controller => 'news', :action => 'show', :id => news) 176 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
178 render_multipart('news_added', body) 177 end
179 end 178
180 179 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
181 # Builds a tmail object used to email recipients of a news' project when a news comment is added. 180 #
182 # 181 # Example:
183 # Example: 182 # news_comment_added(comment) => Mail::Message object
184 # news_comment_added(comment) => tmail object
185 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients 183 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
186 def news_comment_added(comment) 184 def news_comment_added(comment)
187 news = comment.commented 185 news = comment.commented
188 redmine_headers 'Project' => news.project.identifier 186 redmine_headers 'Project' => news.project.identifier
187 @author = comment.author
189 message_id comment 188 message_id comment
190 recipients news.recipients 189 @news = news
191 cc news.watcher_recipients 190 @comment = comment
192 subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}" 191 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
193 body :news => news, 192 mail :to => news.recipients,
194 :comment => comment, 193 :cc => news.watcher_recipients,
195 :news_url => url_for(:controller => 'news', :action => 'show', :id => news) 194 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
196 render_multipart('news_comment_added', body) 195 end
197 end 196
198 197 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
199 # Builds a tmail object used to email the recipients of the specified message that was posted. 198 #
200 # 199 # Example:
201 # Example: 200 # message_posted(message) => Mail::Message object
202 # message_posted(message) => tmail object 201 # Mailer.message_posted(message).deliver => sends an email to the recipients
203 # Mailer.deliver_message_posted(message) => sends an email to the recipients
204 def message_posted(message) 202 def message_posted(message)
205 redmine_headers 'Project' => message.project.identifier, 203 redmine_headers 'Project' => message.project.identifier,
206 'Topic-Id' => (message.parent_id || message.id) 204 'Topic-Id' => (message.parent_id || message.id)
205 @author = message.author
207 message_id message 206 message_id message
208 references message.parent unless message.parent.nil? 207 references message.parent unless message.parent.nil?
209 recipients(message.recipients) 208 recipients = message.recipients
210 cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients) 209 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
211 subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" 210 @message = message
212 body :message => message, 211 @message_url = url_for(message.event_url)
213 :message_url => url_for(message.event_url) 212 mail :to => recipients,
214 render_multipart('message_posted', body) 213 :cc => cc,
215 end 214 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
216 215 end
217 # Builds a tmail object used to email the recipients of a project of the specified wiki content was added. 216
218 # 217 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
219 # Example: 218 #
220 # wiki_content_added(wiki_content) => tmail object 219 # Example:
221 # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients 220 # wiki_content_added(wiki_content) => Mail::Message object
221 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
222 def wiki_content_added(wiki_content) 222 def wiki_content_added(wiki_content)
223 redmine_headers 'Project' => wiki_content.project.identifier, 223 redmine_headers 'Project' => wiki_content.project.identifier,
224 'Wiki-Page-Id' => wiki_content.page.id 224 'Wiki-Page-Id' => wiki_content.page.id
225 @author = wiki_content.author
225 message_id wiki_content 226 message_id wiki_content
226 recipients wiki_content.recipients 227 recipients = wiki_content.recipients
227 cc(wiki_content.page.wiki.watcher_recipients - recipients) 228 cc = wiki_content.page.wiki.watcher_recipients - recipients
228 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" 229 @wiki_content = wiki_content
229 body :wiki_content => wiki_content, 230 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
230 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
231 :project_id => wiki_content.project, 231 :project_id => wiki_content.project,
232 :id => wiki_content.page.title) 232 :id => wiki_content.page.title)
233 render_multipart('wiki_content_added', body) 233 mail :to => recipients,
234 end 234 :cc => cc,
235 235 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
236 # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated. 236 end
237 # 237
238 # Example: 238 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
239 # wiki_content_updated(wiki_content) => tmail object 239 #
240 # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients 240 # Example:
241 # wiki_content_updated(wiki_content) => Mail::Message object
242 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
241 def wiki_content_updated(wiki_content) 243 def wiki_content_updated(wiki_content)
242 redmine_headers 'Project' => wiki_content.project.identifier, 244 redmine_headers 'Project' => wiki_content.project.identifier,
243 'Wiki-Page-Id' => wiki_content.page.id 245 'Wiki-Page-Id' => wiki_content.page.id
246 @author = wiki_content.author
244 message_id wiki_content 247 message_id wiki_content
245 recipients wiki_content.recipients 248 recipients = wiki_content.recipients
246 cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients) 249 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
247 subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}" 250 @wiki_content = wiki_content
248 body :wiki_content => wiki_content, 251 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
249 :wiki_content_url => url_for(:controller => 'wiki', :action => 'show',
250 :project_id => wiki_content.project, 252 :project_id => wiki_content.project,
251 :id => wiki_content.page.title), 253 :id => wiki_content.page.title)
252 :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff', 254 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
253 :project_id => wiki_content.project, :id => wiki_content.page.title, 255 :project_id => wiki_content.project, :id => wiki_content.page.title,
254 :version => wiki_content.version) 256 :version => wiki_content.version)
255 render_multipart('wiki_content_updated', body) 257 mail :to => recipients,
256 end 258 :cc => cc,
257 259 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
258 # Builds a tmail object used to email the specified user their account information. 260 end
259 # 261
260 # Example: 262 # Builds a Mail::Message object used to email the specified user their account information.
261 # account_information(user, password) => tmail object 263 #
262 # Mailer.deliver_account_information(user, password) => sends account information to the user 264 # Example:
265 # account_information(user, password) => Mail::Message object
266 # Mailer.account_information(user, password).deliver => sends account information to the user
263 def account_information(user, password) 267 def account_information(user, password)
264 set_language_if_valid user.language 268 set_language_if_valid user.language
265 recipients user.mail 269 @user = user
266 subject l(:mail_subject_register, Setting.app_title) 270 @password = password
267 body :user => user, 271 @login_url = url_for(:controller => 'account', :action => 'login')
268 :password => password, 272 mail :to => user.mail,
269 :login_url => url_for(:controller => 'account', :action => 'login') 273 :subject => l(:mail_subject_register, Setting.app_title)
270 render_multipart('account_information', body) 274 end
271 end 275
272 276 # Builds a Mail::Message object used to email all active administrators of an account activation request.
273 # Builds a tmail object used to email all active administrators of an account activation request. 277 #
274 # 278 # Example:
275 # Example: 279 # account_activation_request(user) => Mail::Message object
276 # account_activation_request(user) => tmail object 280 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
277 # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators
278 def account_activation_request(user) 281 def account_activation_request(user)
279 # Send the email to all active administrators 282 # Send the email to all active administrators
280 recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact 283 recipients = User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
281 subject l(:mail_subject_account_activation_request, Setting.app_title) 284 @user = user
282 body :user => user, 285 @url = url_for(:controller => 'users', :action => 'index',
283 :url => url_for(:controller => 'users', :action => 'index',
284 :status => User::STATUS_REGISTERED, 286 :status => User::STATUS_REGISTERED,
285 :sort_key => 'created_on', :sort_order => 'desc') 287 :sort_key => 'created_on', :sort_order => 'desc')
286 render_multipart('account_activation_request', body) 288 mail :to => recipients,
287 end 289 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
288 290 end
289 # Builds a tmail object used to email the specified user that their account was activated by an administrator. 291
290 # 292 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
291 # Example: 293 #
292 # account_activated(user) => tmail object 294 # Example:
293 # Mailer.deliver_account_activated(user) => sends an email to the registered user 295 # account_activated(user) => Mail::Message object
296 # Mailer.account_activated(user).deliver => sends an email to the registered user
294 def account_activated(user) 297 def account_activated(user)
295 set_language_if_valid user.language 298 set_language_if_valid user.language
296 recipients user.mail 299 @user = user
297 subject l(:mail_subject_register, Setting.app_title) 300 @login_url = url_for(:controller => 'account', :action => 'login')
298 body :user => user, 301 mail :to => user.mail,
299 :login_url => url_for(:controller => 'account', :action => 'login') 302 :subject => l(:mail_subject_register, Setting.app_title)
300 render_multipart('account_activated', body)
301 end 303 end
302 304
303 def lost_password(token) 305 def lost_password(token)
304 set_language_if_valid(token.user.language) 306 set_language_if_valid(token.user.language)
305 recipients token.user.mail 307 @token = token
306 subject l(:mail_subject_lost_password, Setting.app_title) 308 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
307 body :token => token, 309 mail :to => token.user.mail,
308 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value) 310 :subject => l(:mail_subject_lost_password, Setting.app_title)
309 render_multipart('lost_password', body)
310 end 311 end
311 312
312 def register(token) 313 def register(token)
313 set_language_if_valid(token.user.language) 314 set_language_if_valid(token.user.language)
314 recipients token.user.mail 315 @token = token
315 subject l(:mail_subject_register, Setting.app_title) 316 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
316 body :token => token, 317 mail :to => token.user.mail,
317 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value) 318 :subject => l(:mail_subject_register, Setting.app_title)
318 render_multipart('register', body) 319 end
319 end 320
320 321 def test_email(user)
321 def test(user)
322 set_language_if_valid(user.language) 322 set_language_if_valid(user.language)
323 recipients user.mail 323 @url = url_for(:controller => 'welcome')
324 subject 'Redmine test' 324 mail :to => user.mail,
325 body :url => url_for(:controller => 'welcome') 325 :subject => 'Redmine test'
326 render_multipart('test', body)
327 end 326 end
328 327
329 # Overrides default deliver! method to prevent from sending an email 328 # Overrides default deliver! method to prevent from sending an email
330 # with no recipient, cc or bcc 329 # with no recipient, cc or bcc
331 def deliver!(mail = @mail) 330 def deliver!(mail = @mail)
332 set_language_if_valid @initial_language 331 set_language_if_valid @initial_language
333 return false if (recipients.nil? || recipients.empty?) && 332 return false if (recipients.nil? || recipients.empty?) &&
334 (cc.nil? || cc.empty?) && 333 (cc.nil? || cc.empty?) &&
335 (bcc.nil? || bcc.empty?) 334 (bcc.nil? || bcc.empty?)
336 335
337 # Set Message-Id and References
338 if @message_id_object
339 mail.message_id = self.class.message_id_for(@message_id_object)
340 end
341 if @references_objects
342 mail.references = @references_objects.collect {|o| self.class.message_id_for(o)}
343 end
344 336
345 # Log errors when raise_delivery_errors is set to false, Rails does not 337 # Log errors when raise_delivery_errors is set to false, Rails does not
346 raise_errors = self.class.raise_delivery_errors 338 raise_errors = self.class.raise_delivery_errors
347 self.class.raise_delivery_errors = true 339 self.class.raise_delivery_errors = true
348 begin 340 begin
361 # Sends reminders to issue assignees 353 # Sends reminders to issue assignees
362 # Available options: 354 # Available options:
363 # * :days => how many days in the future to remind about (defaults to 7) 355 # * :days => how many days in the future to remind about (defaults to 7)
364 # * :tracker => id of tracker for filtering issues (defaults to all trackers) 356 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
365 # * :project => id or identifier of project to process (defaults to all projects) 357 # * :project => id or identifier of project to process (defaults to all projects)
366 # * :users => array of user ids who should be reminded 358 # * :users => array of user/group ids who should be reminded
367 def self.reminders(options={}) 359 def self.reminders(options={})
368 days = options[:days] || 7 360 days = options[:days] || 7
369 project = options[:project] ? Project.find(options[:project]) : nil 361 project = options[:project] ? Project.find(options[:project]) : nil
370 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil 362 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
371 user_ids = options[:users] 363 user_ids = options[:users]
372 364
373 s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date] 365 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
374 s << "#{Issue.table_name}.assigned_to_id IS NOT NULL" 366 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
375 s << ["#{Issue.table_name}.assigned_to_id IN (?)", user_ids] if user_ids.present? 367 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
376 s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}" 368 )
377 s << "#{Issue.table_name}.project_id = #{project.id}" if project 369 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
378 s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker 370 scope = scope.where(:project_id => project.id) if project
379 371 scope = scope.where(:tracker_id => tracker.id) if tracker
380 issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], 372
381 :conditions => s.conditions 373 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
382 ).group_by(&:assigned_to) 374 issues_by_assignee.keys.each do |assignee|
375 if assignee.is_a?(Group)
376 assignee.users.each do |user|
377 issues_by_assignee[user] ||= []
378 issues_by_assignee[user] += issues_by_assignee[assignee]
379 end
380 end
381 end
382
383 issues_by_assignee.each do |assignee, issues| 383 issues_by_assignee.each do |assignee, issues|
384 deliver_reminder(assignee, issues, days) if assignee.is_a?(User) && assignee.active? 384 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
385 end 385 end
386 end 386 end
387 387
388 # Activates/desactivates email deliveries during +block+ 388 # Activates/desactivates email deliveries during +block+
389 def self.with_deliveries(enabled = true, &block) 389 def self.with_deliveries(enabled = true, &block)
392 yield 392 yield
393 ensure 393 ensure
394 ActionMailer::Base.perform_deliveries = was_enabled 394 ActionMailer::Base.perform_deliveries = was_enabled
395 end 395 end
396 396
397 private 397 # Sends emails synchronously in the given block
398 def initialize_defaults(method_name) 398 def self.with_synched_deliveries(&block)
399 super 399 saved_method = ActionMailer::Base.delivery_method
400 @initial_language = current_language 400 if m = saved_method.to_s.match(%r{^async_(.+)$})
401 set_language_if_valid Setting.default_language 401 synched_method = m[1]
402 from Setting.mail_from 402 ActionMailer::Base.delivery_method = synched_method.to_sym
403 403 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
404 # Common headers 404 end
405 headers 'X-Mailer' => 'Redmine', 405 yield
406 ensure
407 ActionMailer::Base.delivery_method = saved_method
408 end
409
410 def mail(headers={})
411 headers.merge! 'X-Mailer' => 'Redmine',
406 'X-Redmine-Host' => Setting.host_name, 412 'X-Redmine-Host' => Setting.host_name,
407 'X-Redmine-Site' => Setting.app_title, 413 'X-Redmine-Site' => Setting.app_title,
408 'X-Auto-Response-Suppress' => 'OOF', 414 'X-Auto-Response-Suppress' => 'OOF',
409 'Auto-Submitted' => 'auto-generated' 415 'Auto-Submitted' => 'auto-generated',
410 end 416 'From' => Setting.mail_from,
417 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
418
419 # Removes the author from the recipients and cc
420 # if he doesn't want to receive notifications about what he does
421 if @author && @author.logged? && @author.pref[:no_self_notified]
422 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
423 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
424 end
425
426 if @author && @author.logged?
427 redmine_headers 'Sender' => @author.login
428 end
429
430 # Blind carbon copy recipients
431 if Setting.bcc_recipients?
432 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
433 headers[:to] = nil
434 headers[:cc] = nil
435 end
436
437 if @message_id_object
438 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
439 end
440 if @references_objects
441 headers[:references] = @references_objects.collect {|o| "<#{self.class.message_id_for(o)}>"}.join(' ')
442 end
443
444 super headers do |format|
445 format.text
446 format.html unless Setting.plain_text_mail?
447 end
448
449 set_language_if_valid @initial_language
450 end
451
452 def initialize(*args)
453 @initial_language = current_language
454 set_language_if_valid Setting.default_language
455 super
456 end
457
458 def self.deliver_mail(mail)
459 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
460 super
461 end
462
463 def self.method_missing(method, *args, &block)
464 if m = method.to_s.match(%r{^deliver_(.+)$})
465 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
466 send(m[1], *args).deliver
467 else
468 super
469 end
470 end
471
472 private
411 473
412 # Appends a Redmine header field (name is prepended with 'X-Redmine-') 474 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
413 def redmine_headers(h) 475 def redmine_headers(h)
414 h.each { |k,v| headers["X-Redmine-#{k}"] = v } 476 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
415 end 477 end
416
417 # Overrides the create_mail method
418 def create_mail
419 # Removes the current user from the recipients and cc
420 # if he doesn't want to receive notifications about what he does
421 @author ||= User.current
422 if @author.pref[:no_self_notified]
423 recipients.delete(@author.mail) if recipients
424 cc.delete(@author.mail) if cc
425 end
426
427 notified_users = [recipients, cc].flatten.compact.uniq
428 # Rails would log recipients only, not cc and bcc
429 mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger
430
431 # Blind carbon copy recipients
432 if Setting.bcc_recipients?
433 bcc(notified_users)
434 recipients []
435 cc []
436 end
437 super
438 end
439
440 # Rails 2.3 has problems rendering implicit multipart messages with
441 # layouts so this method will wrap an multipart messages with
442 # explicit parts.
443 #
444 # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type
445 # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts
446
447 def render_multipart(method_name, body)
448 if Setting.plain_text_mail?
449 content_type "text/plain"
450 body render(:file => "#{method_name}.text.erb",
451 :body => body,
452 :layout => 'mailer.text.erb')
453 else
454 content_type "multipart/alternative"
455 part :content_type => "text/plain",
456 :body => render(:file => "#{method_name}.text.erb",
457 :body => body, :layout => 'mailer.text.erb')
458 part :content_type => "text/html",
459 :body => render_message("#{method_name}.html.erb", body)
460 end
461 end
462
463 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
464 def self.controller_path
465 ''
466 end unless respond_to?('controller_path')
467 478
468 # Returns a predictable Message-Id for the given object 479 # Returns a predictable Message-Id for the given object
469 def self.message_id_for(object) 480 def self.message_id_for(object)
470 # id + timestamp should reduce the odds of a collision 481 # id + timestamp should reduce the odds of a collision
471 # as far as we don't send multiple emails for the same object 482 # as far as we don't send multiple emails for the same object
472 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) 483 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
473 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" 484 hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}"
474 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') 485 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
475 host = "#{::Socket.gethostname}.redmine" if host.empty? 486 host = "#{::Socket.gethostname}.redmine" if host.empty?
476 "<#{hash}@#{host}>" 487 "#{hash}@#{host}"
477 end 488 end
478
479 private
480 489
481 def message_id(object) 490 def message_id(object)
482 @message_id_object = object 491 @message_id_object = object
483 end 492 end
484 493
491 Rails.logger 500 Rails.logger
492 end 501 end
493 end 502 end
494 503
495 # Patch TMail so that message_id is not overwritten 504 # Patch TMail so that message_id is not overwritten
505
506 ### NB: Redmine 2.2 no longer uses TMail I think? This function has
507 ### been removed there
508
496 module TMail 509 module TMail
497 class Mail 510 class Mail
498 def add_message_id( fqdn = nil ) 511 def add_message_id( fqdn = nil )
499 self.message_id ||= ::TMail::new_message_id(fqdn) 512 self.message_id ||= ::TMail::new_message_id(fqdn)
500 end 513 end
501 end 514 end
502 end 515 end
503 516
504
505
506