@@ -1,580 +1,585 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 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 'roadie' |
|
19 | 19 | |
|
20 | 20 | class Mailer < ActionMailer::Base |
|
21 | 21 | layout 'mailer' |
|
22 | 22 | helper :application |
|
23 | 23 | helper :issues |
|
24 | 24 | helper :custom_fields |
|
25 | 25 | |
|
26 | 26 | include Redmine::I18n |
|
27 | 27 | include Roadie::Rails::Automatic |
|
28 | 28 | |
|
29 | 29 | def self.default_url_options |
|
30 | 30 | options = {:protocol => Setting.protocol} |
|
31 | 31 | if Setting.host_name.to_s =~ /\A(https?\:\/\/)?(.+?)(\:(\d+))?(\/.+)?\z/i |
|
32 | 32 | host, port, prefix = $2, $4, $5 |
|
33 | 33 | options.merge!({ |
|
34 | 34 | :host => host, :port => port, :script_name => prefix |
|
35 | 35 | }) |
|
36 | 36 | else |
|
37 | 37 | options[:host] = Setting.host_name |
|
38 | 38 | end |
|
39 | 39 | options |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | # Builds a mail for notifying to_users and cc_users about a new issue |
|
43 | 43 | def issue_add(issue, to_users, cc_users) |
|
44 | 44 | redmine_headers 'Project' => issue.project.identifier, |
|
45 | 45 | 'Issue-Id' => issue.id, |
|
46 | 46 | 'Issue-Author' => issue.author.login |
|
47 | 47 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
48 | 48 | message_id issue |
|
49 | 49 | references issue |
|
50 | 50 | @author = issue.author |
|
51 | 51 | @issue = issue |
|
52 | 52 | @users = to_users + cc_users |
|
53 | 53 | @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue) |
|
54 | 54 | mail :to => to_users, |
|
55 | 55 | :cc => cc_users, |
|
56 | 56 | :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" |
|
57 | 57 | end |
|
58 | 58 | |
|
59 | 59 | # Notifies users about a new issue |
|
60 | 60 | def self.deliver_issue_add(issue) |
|
61 | 61 | to = issue.notified_users |
|
62 | 62 | cc = issue.notified_watchers - to |
|
63 | 63 | issue.each_notification(to + cc) do |users| |
|
64 | 64 | Mailer.issue_add(issue, to & users, cc & users).deliver |
|
65 | 65 | end |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | # Builds a mail for notifying to_users and cc_users about an issue update |
|
69 | 69 | def issue_edit(journal, to_users, cc_users) |
|
70 | 70 | issue = journal.journalized |
|
71 | 71 | redmine_headers 'Project' => issue.project.identifier, |
|
72 | 72 | 'Issue-Id' => issue.id, |
|
73 | 73 | 'Issue-Author' => issue.author.login |
|
74 | 74 | redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to |
|
75 | 75 | message_id journal |
|
76 | 76 | references issue |
|
77 | 77 | @author = journal.user |
|
78 | 78 | s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " |
|
79 | 79 | s << "(#{issue.status.name}) " if journal.new_value_for('status_id') |
|
80 | 80 | s << issue.subject |
|
81 | 81 | @issue = issue |
|
82 | 82 | @users = to_users + cc_users |
|
83 | 83 | @journal = journal |
|
84 | 84 | @journal_details = journal.visible_details(@users.first) |
|
85 | 85 | @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") |
|
86 | 86 | mail :to => to_users, |
|
87 | 87 | :cc => cc_users, |
|
88 | 88 | :subject => s |
|
89 | 89 | end |
|
90 | 90 | |
|
91 | 91 | # Notifies users about an issue update |
|
92 | 92 | def self.deliver_issue_edit(journal) |
|
93 | 93 | issue = journal.journalized.reload |
|
94 | 94 | to = journal.notified_users |
|
95 | 95 | cc = journal.notified_watchers - to |
|
96 | 96 | journal.each_notification(to + cc) do |users| |
|
97 | 97 | issue.each_notification(users) do |users2| |
|
98 | 98 | Mailer.issue_edit(journal, to & users2, cc & users2).deliver |
|
99 | 99 | end |
|
100 | 100 | end |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def reminder(user, issues, days) |
|
104 | 104 | set_language_if_valid user.language |
|
105 | 105 | @issues = issues |
|
106 | 106 | @days = days |
|
107 | 107 | @issues_url = url_for(:controller => 'issues', :action => 'index', |
|
108 | 108 | :set_filter => 1, :assigned_to_id => user.id, |
|
109 | 109 | :sort => 'due_date:asc') |
|
110 | 110 | mail :to => user, |
|
111 | 111 | :subject => l(:mail_subject_reminder, :count => issues.size, :days => days) |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | # Builds a Mail::Message object used to email users belonging to the added document's project. |
|
115 | 115 | # |
|
116 | 116 | # Example: |
|
117 | 117 | # document_added(document) => Mail::Message object |
|
118 | 118 | # Mailer.document_added(document).deliver => sends an email to the document's project recipients |
|
119 | 119 | def document_added(document) |
|
120 | 120 | redmine_headers 'Project' => document.project.identifier |
|
121 | 121 | @author = User.current |
|
122 | 122 | @document = document |
|
123 | 123 | @document_url = url_for(:controller => 'documents', :action => 'show', :id => document) |
|
124 | 124 | mail :to => document.notified_users, |
|
125 | 125 | :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | # Builds a Mail::Message object used to email recipients of a project when an attachements are added. |
|
129 | 129 | # |
|
130 | 130 | # Example: |
|
131 | 131 | # attachments_added(attachments) => Mail::Message object |
|
132 | 132 | # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients |
|
133 | 133 | def attachments_added(attachments) |
|
134 | 134 | container = attachments.first.container |
|
135 | 135 | added_to = '' |
|
136 | 136 | added_to_url = '' |
|
137 | 137 | @author = attachments.first.author |
|
138 | 138 | case container.class.name |
|
139 | 139 | when 'Project' |
|
140 | 140 | added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) |
|
141 | 141 | added_to = "#{l(:label_project)}: #{container}" |
|
142 | 142 | recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)} |
|
143 | 143 | when 'Version' |
|
144 | 144 | added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) |
|
145 | 145 | added_to = "#{l(:label_version)}: #{container.name}" |
|
146 | 146 | recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)} |
|
147 | 147 | when 'Document' |
|
148 | 148 | added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) |
|
149 | 149 | added_to = "#{l(:label_document)}: #{container.title}" |
|
150 | 150 | recipients = container.notified_users |
|
151 | 151 | end |
|
152 | 152 | redmine_headers 'Project' => container.project.identifier |
|
153 | 153 | @attachments = attachments |
|
154 | 154 | @added_to = added_to |
|
155 | 155 | @added_to_url = added_to_url |
|
156 | 156 | mail :to => recipients, |
|
157 | 157 | :subject => "[#{container.project.name}] #{l(:label_attachment_new)}" |
|
158 | 158 | end |
|
159 | 159 | |
|
160 | 160 | # Builds a Mail::Message object used to email recipients of a news' project when a news item is added. |
|
161 | 161 | # |
|
162 | 162 | # Example: |
|
163 | 163 | # news_added(news) => Mail::Message object |
|
164 | 164 | # Mailer.news_added(news).deliver => sends an email to the news' project recipients |
|
165 | 165 | def news_added(news) |
|
166 | 166 | redmine_headers 'Project' => news.project.identifier |
|
167 | 167 | @author = news.author |
|
168 | 168 | message_id news |
|
169 | 169 | references news |
|
170 | 170 | @news = news |
|
171 | 171 | @news_url = url_for(:controller => 'news', :action => 'show', :id => news) |
|
172 | 172 | mail :to => news.notified_users, |
|
173 | 173 | :cc => news.notified_watchers_for_added_news, |
|
174 | 174 | :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}" |
|
175 | 175 | end |
|
176 | 176 | |
|
177 | 177 | # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added. |
|
178 | 178 | # |
|
179 | 179 | # Example: |
|
180 | 180 | # news_comment_added(comment) => Mail::Message object |
|
181 | 181 | # Mailer.news_comment_added(comment) => sends an email to the news' project recipients |
|
182 | 182 | def news_comment_added(comment) |
|
183 | 183 | news = comment.commented |
|
184 | 184 | redmine_headers 'Project' => news.project.identifier |
|
185 | 185 | @author = comment.author |
|
186 | 186 | message_id comment |
|
187 | 187 | references news |
|
188 | 188 | @news = news |
|
189 | 189 | @comment = comment |
|
190 | 190 | @news_url = url_for(:controller => 'news', :action => 'show', :id => news) |
|
191 | 191 | mail :to => news.notified_users, |
|
192 | 192 | :cc => news.notified_watchers, |
|
193 | 193 | :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}" |
|
194 | 194 | end |
|
195 | 195 | |
|
196 | 196 | # Builds a Mail::Message object used to email the recipients of the specified message that was posted. |
|
197 | 197 | # |
|
198 | 198 | # Example: |
|
199 | 199 | # message_posted(message) => Mail::Message object |
|
200 | 200 | # Mailer.message_posted(message).deliver => sends an email to the recipients |
|
201 | 201 | def message_posted(message) |
|
202 | 202 | redmine_headers 'Project' => message.project.identifier, |
|
203 | 203 | 'Topic-Id' => (message.parent_id || message.id) |
|
204 | 204 | @author = message.author |
|
205 | 205 | message_id message |
|
206 | 206 | references message.root |
|
207 | 207 | recipients = message.notified_users |
|
208 | 208 | cc = ((message.root.notified_watchers + message.board.notified_watchers).uniq - recipients) |
|
209 | 209 | @message = message |
|
210 | 210 | @message_url = url_for(message.event_url) |
|
211 | 211 | mail :to => recipients, |
|
212 | 212 | :cc => cc, |
|
213 | 213 | :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" |
|
214 | 214 | end |
|
215 | 215 | |
|
216 | 216 | # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added. |
|
217 | 217 | # |
|
218 | 218 | # Example: |
|
219 | 219 | # wiki_content_added(wiki_content) => Mail::Message object |
|
220 | 220 | # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients |
|
221 | 221 | def wiki_content_added(wiki_content) |
|
222 | 222 | redmine_headers 'Project' => wiki_content.project.identifier, |
|
223 | 223 | 'Wiki-Page-Id' => wiki_content.page.id |
|
224 | 224 | @author = wiki_content.author |
|
225 | 225 | message_id wiki_content |
|
226 | 226 | recipients = wiki_content.notified_users |
|
227 | 227 | cc = wiki_content.page.wiki.notified_watchers - recipients |
|
228 | 228 | @wiki_content = wiki_content |
|
229 | 229 | @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', |
|
230 | 230 | :project_id => wiki_content.project, |
|
231 | 231 | :id => wiki_content.page.title) |
|
232 | 232 | mail :to => recipients, |
|
233 | 233 | :cc => cc, |
|
234 | 234 | :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" |
|
235 | 235 | end |
|
236 | 236 | |
|
237 | 237 | # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated. |
|
238 | 238 | # |
|
239 | 239 | # Example: |
|
240 | 240 | # wiki_content_updated(wiki_content) => Mail::Message object |
|
241 | 241 | # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients |
|
242 | 242 | def wiki_content_updated(wiki_content) |
|
243 | 243 | redmine_headers 'Project' => wiki_content.project.identifier, |
|
244 | 244 | 'Wiki-Page-Id' => wiki_content.page.id |
|
245 | 245 | @author = wiki_content.author |
|
246 | 246 | message_id wiki_content |
|
247 | 247 | recipients = wiki_content.notified_users |
|
248 | 248 | cc = wiki_content.page.wiki.notified_watchers + wiki_content.page.notified_watchers - recipients |
|
249 | 249 | @wiki_content = wiki_content |
|
250 | 250 | @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', |
|
251 | 251 | :project_id => wiki_content.project, |
|
252 | 252 | :id => wiki_content.page.title) |
|
253 | 253 | @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff', |
|
254 | 254 | :project_id => wiki_content.project, :id => wiki_content.page.title, |
|
255 | 255 | :version => wiki_content.version) |
|
256 | 256 | mail :to => recipients, |
|
257 | 257 | :cc => cc, |
|
258 | 258 | :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}" |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | # Builds a Mail::Message object used to email the specified user their account information. |
|
262 | 262 | # |
|
263 | 263 | # Example: |
|
264 | 264 | # account_information(user, password) => Mail::Message object |
|
265 | 265 | # Mailer.account_information(user, password).deliver => sends account information to the user |
|
266 | 266 | def account_information(user, password) |
|
267 | 267 | set_language_if_valid user.language |
|
268 | 268 | @user = user |
|
269 | 269 | @password = password |
|
270 | 270 | @login_url = url_for(:controller => 'account', :action => 'login') |
|
271 | 271 | mail :to => user.mail, |
|
272 | 272 | :subject => l(:mail_subject_register, Setting.app_title) |
|
273 | 273 | end |
|
274 | 274 | |
|
275 | 275 | # Builds a Mail::Message object used to email all active administrators of an account activation request. |
|
276 | 276 | # |
|
277 | 277 | # Example: |
|
278 | 278 | # account_activation_request(user) => Mail::Message object |
|
279 | 279 | # Mailer.account_activation_request(user).deliver => sends an email to all active administrators |
|
280 | 280 | def account_activation_request(user) |
|
281 | 281 | # Send the email to all active administrators |
|
282 | 282 | recipients = User.active.where(:admin => true) |
|
283 | 283 | @user = user |
|
284 | 284 | @url = url_for(:controller => 'users', :action => 'index', |
|
285 | 285 | :status => User::STATUS_REGISTERED, |
|
286 | 286 | :sort_key => 'created_on', :sort_order => 'desc') |
|
287 | 287 | mail :to => recipients, |
|
288 | 288 | :subject => l(:mail_subject_account_activation_request, Setting.app_title) |
|
289 | 289 | end |
|
290 | 290 | |
|
291 | 291 | # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator. |
|
292 | 292 | # |
|
293 | 293 | # Example: |
|
294 | 294 | # account_activated(user) => Mail::Message object |
|
295 | 295 | # Mailer.account_activated(user).deliver => sends an email to the registered user |
|
296 | 296 | def account_activated(user) |
|
297 | 297 | set_language_if_valid user.language |
|
298 | 298 | @user = user |
|
299 | 299 | @login_url = url_for(:controller => 'account', :action => 'login') |
|
300 | 300 | mail :to => user.mail, |
|
301 | 301 | :subject => l(:mail_subject_register, Setting.app_title) |
|
302 | 302 | end |
|
303 | 303 | |
|
304 | 304 | def lost_password(token, recipient=nil) |
|
305 | 305 | set_language_if_valid(token.user.language) |
|
306 | 306 | recipient ||= token.user.mail |
|
307 | 307 | @token = token |
|
308 | 308 | @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value) |
|
309 | 309 | mail :to => recipient, |
|
310 | 310 | :subject => l(:mail_subject_lost_password, Setting.app_title) |
|
311 | 311 | end |
|
312 | 312 | |
|
313 | 313 | # Notifies user that his password was updated |
|
314 | 314 | def self.password_updated(user) |
|
315 | # Don't send a notification to the dummy email address when changing the password | |
|
316 | # of the default admin account which is required after the first login | |
|
317 | # TODO: maybe not the best way to handle this | |
|
318 | return if user.admin? && user.login == 'admin' && user.mail == 'admin@example.net' | |
|
319 | ||
|
315 | 320 | Mailer.security_notification(user, |
|
316 | 321 | message: :mail_body_password_updated, |
|
317 | 322 | title: :button_change_password, |
|
318 | 323 | url: {controller: 'my', action: 'password'} |
|
319 | 324 | ).deliver |
|
320 | 325 | end |
|
321 | 326 | |
|
322 | 327 | def register(token) |
|
323 | 328 | set_language_if_valid(token.user.language) |
|
324 | 329 | @token = token |
|
325 | 330 | @url = url_for(:controller => 'account', :action => 'activate', :token => token.value) |
|
326 | 331 | mail :to => token.user.mail, |
|
327 | 332 | :subject => l(:mail_subject_register, Setting.app_title) |
|
328 | 333 | end |
|
329 | 334 | |
|
330 | 335 | def security_notification(recipients, options={}) |
|
331 | 336 | redmine_headers 'Sender' => User.current.login |
|
332 | 337 | @user = Array(recipients).detect{|r| r.is_a? User } |
|
333 | 338 | set_language_if_valid(@user.try :language) |
|
334 | 339 | @message = l(options[:message], |
|
335 | 340 | field: (options[:field] && l(options[:field])), |
|
336 | 341 | value: options[:value] |
|
337 | 342 | ) |
|
338 | 343 | @title = options[:title] && l(options[:title]) |
|
339 | 344 | @url = options[:url] && (options[:url].is_a?(Hash) ? url_for(options[:url]) : options[:url]) |
|
340 | 345 | mail :to => recipients, |
|
341 | 346 | :subject => "[#{Setting.app_title}] #{l(:mail_subject_security_notification)}" |
|
342 | 347 | end |
|
343 | 348 | |
|
344 | 349 | def settings_updated(recipients, changes) |
|
345 | 350 | redmine_headers 'Sender' => User.current.login |
|
346 | 351 | @changes = changes |
|
347 | 352 | @url = url_for(controller: 'settings', action: 'index') |
|
348 | 353 | mail :to => recipients, |
|
349 | 354 | :subject => "[#{Setting.app_title}] #{l(:mail_subject_security_notification)}" |
|
350 | 355 | end |
|
351 | 356 | |
|
352 | 357 | # Notifies admins about settings changes |
|
353 | 358 | def self.security_settings_updated(changes) |
|
354 | 359 | return unless changes.present? |
|
355 | 360 | |
|
356 | 361 | users = User.active.where(admin: true).to_a |
|
357 | 362 | Mailer.settings_updated(users, changes).deliver |
|
358 | 363 | end |
|
359 | 364 | |
|
360 | 365 | def test_email(user) |
|
361 | 366 | set_language_if_valid(user.language) |
|
362 | 367 | @url = url_for(:controller => 'welcome') |
|
363 | 368 | mail :to => user.mail, |
|
364 | 369 | :subject => 'Redmine test' |
|
365 | 370 | end |
|
366 | 371 | |
|
367 | 372 | # Sends reminders to issue assignees |
|
368 | 373 | # Available options: |
|
369 | 374 | # * :days => how many days in the future to remind about (defaults to 7) |
|
370 | 375 | # * :tracker => id of tracker for filtering issues (defaults to all trackers) |
|
371 | 376 | # * :project => id or identifier of project to process (defaults to all projects) |
|
372 | 377 | # * :users => array of user/group ids who should be reminded |
|
373 | 378 | # * :version => name of target version for filtering issues (defaults to none) |
|
374 | 379 | def self.reminders(options={}) |
|
375 | 380 | days = options[:days] || 7 |
|
376 | 381 | project = options[:project] ? Project.find(options[:project]) : nil |
|
377 | 382 | tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil |
|
378 | 383 | target_version_id = options[:version] ? Version.named(options[:version]).pluck(:id) : nil |
|
379 | 384 | if options[:version] && target_version_id.blank? |
|
380 | 385 | raise ActiveRecord::RecordNotFound.new("Couldn't find Version with named #{options[:version]}") |
|
381 | 386 | end |
|
382 | 387 | user_ids = options[:users] |
|
383 | 388 | |
|
384 | 389 | scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" + |
|
385 | 390 | " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" + |
|
386 | 391 | " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date |
|
387 | 392 | ) |
|
388 | 393 | scope = scope.where(:assigned_to_id => user_ids) if user_ids.present? |
|
389 | 394 | scope = scope.where(:project_id => project.id) if project |
|
390 | 395 | scope = scope.where(:fixed_version_id => target_version_id) if target_version_id.present? |
|
391 | 396 | scope = scope.where(:tracker_id => tracker.id) if tracker |
|
392 | 397 | issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker). |
|
393 | 398 | group_by(&:assigned_to) |
|
394 | 399 | issues_by_assignee.keys.each do |assignee| |
|
395 | 400 | if assignee.is_a?(Group) |
|
396 | 401 | assignee.users.each do |user| |
|
397 | 402 | issues_by_assignee[user] ||= [] |
|
398 | 403 | issues_by_assignee[user] += issues_by_assignee[assignee] |
|
399 | 404 | end |
|
400 | 405 | end |
|
401 | 406 | end |
|
402 | 407 | |
|
403 | 408 | issues_by_assignee.each do |assignee, issues| |
|
404 | 409 | reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active? |
|
405 | 410 | end |
|
406 | 411 | end |
|
407 | 412 | |
|
408 | 413 | # Activates/desactivates email deliveries during +block+ |
|
409 | 414 | def self.with_deliveries(enabled = true, &block) |
|
410 | 415 | was_enabled = ActionMailer::Base.perform_deliveries |
|
411 | 416 | ActionMailer::Base.perform_deliveries = !!enabled |
|
412 | 417 | yield |
|
413 | 418 | ensure |
|
414 | 419 | ActionMailer::Base.perform_deliveries = was_enabled |
|
415 | 420 | end |
|
416 | 421 | |
|
417 | 422 | # Sends emails synchronously in the given block |
|
418 | 423 | def self.with_synched_deliveries(&block) |
|
419 | 424 | saved_method = ActionMailer::Base.delivery_method |
|
420 | 425 | if m = saved_method.to_s.match(%r{^async_(.+)$}) |
|
421 | 426 | synched_method = m[1] |
|
422 | 427 | ActionMailer::Base.delivery_method = synched_method.to_sym |
|
423 | 428 | ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings") |
|
424 | 429 | end |
|
425 | 430 | yield |
|
426 | 431 | ensure |
|
427 | 432 | ActionMailer::Base.delivery_method = saved_method |
|
428 | 433 | end |
|
429 | 434 | |
|
430 | 435 | def mail(headers={}, &block) |
|
431 | 436 | headers.reverse_merge! 'X-Mailer' => 'Redmine', |
|
432 | 437 | 'X-Redmine-Host' => Setting.host_name, |
|
433 | 438 | 'X-Redmine-Site' => Setting.app_title, |
|
434 | 439 | 'X-Auto-Response-Suppress' => 'All', |
|
435 | 440 | 'Auto-Submitted' => 'auto-generated', |
|
436 | 441 | 'From' => Setting.mail_from, |
|
437 | 442 | 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" |
|
438 | 443 | |
|
439 | 444 | # Replaces users with their email addresses |
|
440 | 445 | [:to, :cc, :bcc].each do |key| |
|
441 | 446 | if headers[key].present? |
|
442 | 447 | headers[key] = self.class.email_addresses(headers[key]) |
|
443 | 448 | end |
|
444 | 449 | end |
|
445 | 450 | |
|
446 | 451 | # Removes the author from the recipients and cc |
|
447 | 452 | # if the author does not want to receive notifications |
|
448 | 453 | # about what the author do |
|
449 | 454 | if @author && @author.logged? && @author.pref.no_self_notified |
|
450 | 455 | addresses = @author.mails |
|
451 | 456 | headers[:to] -= addresses if headers[:to].is_a?(Array) |
|
452 | 457 | headers[:cc] -= addresses if headers[:cc].is_a?(Array) |
|
453 | 458 | end |
|
454 | 459 | |
|
455 | 460 | if @author && @author.logged? |
|
456 | 461 | redmine_headers 'Sender' => @author.login |
|
457 | 462 | end |
|
458 | 463 | |
|
459 | 464 | # Blind carbon copy recipients |
|
460 | 465 | if Setting.bcc_recipients? |
|
461 | 466 | headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?) |
|
462 | 467 | headers[:to] = nil |
|
463 | 468 | headers[:cc] = nil |
|
464 | 469 | end |
|
465 | 470 | |
|
466 | 471 | if @message_id_object |
|
467 | 472 | headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>" |
|
468 | 473 | end |
|
469 | 474 | if @references_objects |
|
470 | 475 | headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ') |
|
471 | 476 | end |
|
472 | 477 | |
|
473 | 478 | m = if block_given? |
|
474 | 479 | super headers, &block |
|
475 | 480 | else |
|
476 | 481 | super headers do |format| |
|
477 | 482 | format.text |
|
478 | 483 | format.html unless Setting.plain_text_mail? |
|
479 | 484 | end |
|
480 | 485 | end |
|
481 | 486 | set_language_if_valid @initial_language |
|
482 | 487 | |
|
483 | 488 | m |
|
484 | 489 | end |
|
485 | 490 | |
|
486 | 491 | def initialize(*args) |
|
487 | 492 | @initial_language = current_language |
|
488 | 493 | set_language_if_valid Setting.default_language |
|
489 | 494 | super |
|
490 | 495 | end |
|
491 | 496 | |
|
492 | 497 | def self.deliver_mail(mail) |
|
493 | 498 | return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank? |
|
494 | 499 | begin |
|
495 | 500 | # Log errors when raise_delivery_errors is set to false, Rails does not |
|
496 | 501 | mail.raise_delivery_errors = true |
|
497 | 502 | super |
|
498 | 503 | rescue Exception => e |
|
499 | 504 | if ActionMailer::Base.raise_delivery_errors |
|
500 | 505 | raise e |
|
501 | 506 | else |
|
502 | 507 | Rails.logger.error "Email delivery error: #{e.message}" |
|
503 | 508 | end |
|
504 | 509 | end |
|
505 | 510 | end |
|
506 | 511 | |
|
507 | 512 | def self.method_missing(method, *args, &block) |
|
508 | 513 | if m = method.to_s.match(%r{^deliver_(.+)$}) |
|
509 | 514 | ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead." |
|
510 | 515 | send(m[1], *args).deliver |
|
511 | 516 | else |
|
512 | 517 | super |
|
513 | 518 | end |
|
514 | 519 | end |
|
515 | 520 | |
|
516 | 521 | # Returns an array of email addresses to notify by |
|
517 | 522 | # replacing users in arg with their notified email addresses |
|
518 | 523 | # |
|
519 | 524 | # Example: |
|
520 | 525 | # Mailer.email_addresses(users) |
|
521 | 526 | # => ["foo@example.net", "bar@example.net"] |
|
522 | 527 | def self.email_addresses(arg) |
|
523 | 528 | arr = Array.wrap(arg) |
|
524 | 529 | mails = arr.reject {|a| a.is_a? Principal} |
|
525 | 530 | users = arr - mails |
|
526 | 531 | if users.any? |
|
527 | 532 | mails += EmailAddress. |
|
528 | 533 | where(:user_id => users.map(&:id)). |
|
529 | 534 | where("is_default = ? OR notify = ?", true, true). |
|
530 | 535 | pluck(:address) |
|
531 | 536 | end |
|
532 | 537 | mails |
|
533 | 538 | end |
|
534 | 539 | |
|
535 | 540 | private |
|
536 | 541 | |
|
537 | 542 | # Appends a Redmine header field (name is prepended with 'X-Redmine-') |
|
538 | 543 | def redmine_headers(h) |
|
539 | 544 | h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s } |
|
540 | 545 | end |
|
541 | 546 | |
|
542 | 547 | def self.token_for(object, rand=true) |
|
543 | 548 | timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) |
|
544 | 549 | hash = [ |
|
545 | 550 | "redmine", |
|
546 | 551 | "#{object.class.name.demodulize.underscore}-#{object.id}", |
|
547 | 552 | timestamp.strftime("%Y%m%d%H%M%S") |
|
548 | 553 | ] |
|
549 | 554 | if rand |
|
550 | 555 | hash << Redmine::Utils.random_hex(8) |
|
551 | 556 | end |
|
552 | 557 | host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '') |
|
553 | 558 | host = "#{::Socket.gethostname}.redmine" if host.empty? |
|
554 | 559 | "#{hash.join('.')}@#{host}" |
|
555 | 560 | end |
|
556 | 561 | |
|
557 | 562 | # Returns a Message-Id for the given object |
|
558 | 563 | def self.message_id_for(object) |
|
559 | 564 | token_for(object, true) |
|
560 | 565 | end |
|
561 | 566 | |
|
562 | 567 | # Returns a uniq token for a given object referenced by all notifications |
|
563 | 568 | # related to this object |
|
564 | 569 | def self.references_for(object) |
|
565 | 570 | token_for(object, false) |
|
566 | 571 | end |
|
567 | 572 | |
|
568 | 573 | def message_id(object) |
|
569 | 574 | @message_id_object = object |
|
570 | 575 | end |
|
571 | 576 | |
|
572 | 577 | def references(object) |
|
573 | 578 | @references_objects ||= [] |
|
574 | 579 | @references_objects << object |
|
575 | 580 | end |
|
576 | 581 | |
|
577 | 582 | def mylogger |
|
578 | 583 | Rails.logger |
|
579 | 584 | end |
|
580 | 585 | end |
General Comments 0
You need to be logged in to leave comments.
Login now