Chris@441
|
1 # Redmine - project management software
|
Chris@1494
|
2 # Copyright (C) 2006-2014 Jean-Philippe Lang
|
Chris@0
|
3 #
|
Chris@0
|
4 # This program is free software; you can redistribute it and/or
|
Chris@0
|
5 # modify it under the terms of the GNU General Public License
|
Chris@0
|
6 # as published by the Free Software Foundation; either version 2
|
Chris@0
|
7 # of the License, or (at your option) any later version.
|
Chris@0
|
8 #
|
Chris@0
|
9 # This program is distributed in the hope that it will be useful,
|
Chris@0
|
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
12 # GNU General Public License for more details.
|
Chris@0
|
13 #
|
Chris@0
|
14 # You should have received a copy of the GNU General Public License
|
Chris@0
|
15 # along with this program; if not, write to the Free Software
|
Chris@0
|
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Chris@0
|
17
|
Chris@0
|
18 class Mailer < ActionMailer::Base
|
Chris@0
|
19 layout 'mailer'
|
Chris@0
|
20 helper :application
|
Chris@0
|
21 helper :issues
|
Chris@0
|
22 helper :custom_fields
|
Chris@0
|
23
|
Chris@0
|
24 include Redmine::I18n
|
Chris@0
|
25
|
Chris@0
|
26 def self.default_url_options
|
Chris@1115
|
27 { :host => Setting.host_name, :protocol => Setting.protocol }
|
Chris@0
|
28 end
|
Chris@441
|
29
|
Chris@1484
|
30 # Builds a mail for notifying the specified member that they were
|
Chris@1484
|
31 # added to a project
|
chris@1270
|
32 def member_added_to_project(member, project)
|
chris@1270
|
33
|
luis@949
|
34 principal = Principal.find(member.user_id)
|
luis@291
|
35
|
chris@1270
|
36 users = []
|
luis@949
|
37 if principal.type == "User"
|
chris@1270
|
38 users = [User.find(member.user_id)]
|
luis@949
|
39 else
|
luis@949
|
40 users = Principal.find(member.user_id).users
|
luis@949
|
41 end
|
luis@291
|
42
|
chris@1270
|
43 users.map do |user|
|
chris@1270
|
44
|
chris@1270
|
45 set_language_if_valid user.language
|
chris@1270
|
46 @project_url = url_for(:controller => 'projects', :action => 'show', :id => project.id)
|
chris@1270
|
47 @project_name = project.name
|
chris@1270
|
48 mail :to => user.mail,
|
chris@1270
|
49 :subject => l(:mail_subject_added_to_project, Setting.app_title)
|
chris@1270
|
50
|
chris@1270
|
51 end
|
luis@291
|
52 end
|
chris@37
|
53
|
Chris@1464
|
54 # Builds a mail for notifying to_users and cc_users about a new issue
|
Chris@1464
|
55 def issue_add(issue, to_users, cc_users)
|
Chris@0
|
56 redmine_headers 'Project' => issue.project.identifier,
|
Chris@0
|
57 'Issue-Id' => issue.id,
|
Chris@0
|
58 'Issue-Author' => issue.author.login
|
Chris@0
|
59 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
|
Chris@0
|
60 message_id issue
|
Chris@1464
|
61 references issue
|
Chris@1115
|
62 @author = issue.author
|
Chris@1115
|
63 @issue = issue
|
Chris@1464
|
64 @users = to_users + cc_users
|
Chris@1115
|
65 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
|
Chris@1464
|
66 mail :to => to_users.map(&:mail),
|
Chris@1464
|
67 :cc => cc_users.map(&:mail),
|
Chris@1115
|
68 :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
|
Chris@0
|
69 end
|
Chris@0
|
70
|
Chris@1464
|
71 # Notifies users about a new issue
|
Chris@1464
|
72 def self.deliver_issue_add(issue)
|
Chris@1464
|
73 to = issue.notified_users
|
Chris@1464
|
74 cc = issue.notified_watchers - to
|
Chris@1464
|
75 issue.each_notification(to + cc) do |users|
|
Chris@1464
|
76 Mailer.issue_add(issue, to & users, cc & users).deliver
|
Chris@1464
|
77 end
|
Chris@1464
|
78 end
|
Chris@1464
|
79
|
Chris@1464
|
80 # Builds a mail for notifying to_users and cc_users about an issue update
|
Chris@1464
|
81 def issue_edit(journal, to_users, cc_users)
|
Chris@1464
|
82 issue = journal.journalized
|
Chris@0
|
83 redmine_headers 'Project' => issue.project.identifier,
|
Chris@0
|
84 'Issue-Id' => issue.id,
|
Chris@0
|
85 'Issue-Author' => issue.author.login
|
Chris@0
|
86 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
|
Chris@0
|
87 message_id journal
|
Chris@0
|
88 references issue
|
Chris@0
|
89 @author = journal.user
|
Chris@0
|
90 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
|
Chris@0
|
91 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
|
Chris@0
|
92 s << issue.subject
|
Chris@1115
|
93 @issue = issue
|
Chris@1464
|
94 @users = to_users + cc_users
|
Chris@1115
|
95 @journal = journal
|
Chris@1464
|
96 @journal_details = journal.visible_details(@users.first)
|
Chris@1115
|
97 @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
|
Chris@1464
|
98 mail :to => to_users.map(&:mail),
|
Chris@1464
|
99 :cc => cc_users.map(&:mail),
|
Chris@1115
|
100 :subject => s
|
Chris@0
|
101 end
|
Chris@0
|
102
|
Chris@1464
|
103 # Notifies users about an issue update
|
Chris@1464
|
104 def self.deliver_issue_edit(journal)
|
Chris@1464
|
105 issue = journal.journalized.reload
|
Chris@1464
|
106 to = journal.notified_users
|
Chris@1464
|
107 cc = journal.notified_watchers
|
Chris@1464
|
108 journal.each_notification(to + cc) do |users|
|
Chris@1464
|
109 issue.each_notification(users) do |users2|
|
Chris@1464
|
110 Mailer.issue_edit(journal, to & users2, cc & users2).deliver
|
Chris@1464
|
111 end
|
Chris@1464
|
112 end
|
Chris@1464
|
113 end
|
Chris@1464
|
114
|
Chris@0
|
115 def reminder(user, issues, days)
|
Chris@0
|
116 set_language_if_valid user.language
|
Chris@1115
|
117 @issues = issues
|
Chris@1115
|
118 @days = days
|
Chris@1115
|
119 @issues_url = url_for(:controller => 'issues', :action => 'index',
|
Chris@909
|
120 :set_filter => 1, :assigned_to_id => user.id,
|
Chris@909
|
121 :sort => 'due_date:asc')
|
Chris@1115
|
122 mail :to => user.mail,
|
Chris@1115
|
123 :subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
|
Chris@0
|
124 end
|
Chris@0
|
125
|
Chris@1115
|
126 # Builds a Mail::Message object used to email users belonging to the added document's project.
|
Chris@0
|
127 #
|
Chris@0
|
128 # Example:
|
Chris@1115
|
129 # document_added(document) => Mail::Message object
|
Chris@1115
|
130 # Mailer.document_added(document).deliver => sends an email to the document's project recipients
|
Chris@0
|
131 def document_added(document)
|
Chris@0
|
132 redmine_headers 'Project' => document.project.identifier
|
Chris@1115
|
133 @author = User.current
|
Chris@1115
|
134 @document = document
|
Chris@1115
|
135 @document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
|
Chris@1115
|
136 mail :to => document.recipients,
|
Chris@1115
|
137 :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
|
Chris@0
|
138 end
|
Chris@0
|
139
|
Chris@1115
|
140 # Builds a Mail::Message object used to email recipients of a project when an attachements are added.
|
Chris@0
|
141 #
|
Chris@0
|
142 # Example:
|
Chris@1115
|
143 # attachments_added(attachments) => Mail::Message object
|
Chris@1115
|
144 # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients
|
Chris@0
|
145 def attachments_added(attachments)
|
Chris@0
|
146 container = attachments.first.container
|
Chris@0
|
147 added_to = ''
|
Chris@0
|
148 added_to_url = ''
|
Chris@1115
|
149 @author = attachments.first.author
|
Chris@0
|
150 case container.class.name
|
Chris@0
|
151 when 'Project'
|
Chris@441
|
152 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
|
Chris@0
|
153 added_to = "#{l(:label_project)}: #{container}"
|
Chris@1115
|
154 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
|
Chris@0
|
155 when 'Version'
|
Chris@441
|
156 added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
|
Chris@0
|
157 added_to = "#{l(:label_version)}: #{container.name}"
|
Chris@1115
|
158 recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail}
|
Chris@0
|
159 when 'Document'
|
Chris@0
|
160 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
|
Chris@0
|
161 added_to = "#{l(:label_document)}: #{container.title}"
|
Chris@1115
|
162 recipients = container.recipients
|
Chris@0
|
163 end
|
Chris@0
|
164 redmine_headers 'Project' => container.project.identifier
|
Chris@1115
|
165 @attachments = attachments
|
Chris@1115
|
166 @added_to = added_to
|
Chris@1115
|
167 @added_to_url = added_to_url
|
Chris@1115
|
168 mail :to => recipients,
|
Chris@1115
|
169 :subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
|
Chris@0
|
170 end
|
Chris@441
|
171
|
Chris@1115
|
172 # Builds a Mail::Message object used to email recipients of a news' project when a news item is added.
|
Chris@0
|
173 #
|
Chris@0
|
174 # Example:
|
Chris@1115
|
175 # news_added(news) => Mail::Message object
|
Chris@1115
|
176 # Mailer.news_added(news).deliver => sends an email to the news' project recipients
|
Chris@0
|
177 def news_added(news)
|
Chris@0
|
178 redmine_headers 'Project' => news.project.identifier
|
Chris@1115
|
179 @author = news.author
|
Chris@0
|
180 message_id news
|
Chris@1464
|
181 references news
|
Chris@1115
|
182 @news = news
|
Chris@1115
|
183 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
|
Chris@1115
|
184 mail :to => news.recipients,
|
Chris@1115
|
185 :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
|
Chris@0
|
186 end
|
Chris@0
|
187
|
Chris@1115
|
188 # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added.
|
Chris@441
|
189 #
|
Chris@441
|
190 # Example:
|
Chris@1115
|
191 # news_comment_added(comment) => Mail::Message object
|
Chris@441
|
192 # Mailer.news_comment_added(comment) => sends an email to the news' project recipients
|
Chris@441
|
193 def news_comment_added(comment)
|
Chris@441
|
194 news = comment.commented
|
Chris@441
|
195 redmine_headers 'Project' => news.project.identifier
|
Chris@1115
|
196 @author = comment.author
|
Chris@441
|
197 message_id comment
|
Chris@1464
|
198 references news
|
Chris@1115
|
199 @news = news
|
Chris@1115
|
200 @comment = comment
|
Chris@1115
|
201 @news_url = url_for(:controller => 'news', :action => 'show', :id => news)
|
Chris@1115
|
202 mail :to => news.recipients,
|
Chris@1115
|
203 :cc => news.watcher_recipients,
|
Chris@1115
|
204 :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
|
Chris@441
|
205 end
|
Chris@441
|
206
|
Chris@1115
|
207 # Builds a Mail::Message object used to email the recipients of the specified message that was posted.
|
Chris@0
|
208 #
|
Chris@0
|
209 # Example:
|
Chris@1115
|
210 # message_posted(message) => Mail::Message object
|
Chris@1115
|
211 # Mailer.message_posted(message).deliver => sends an email to the recipients
|
Chris@0
|
212 def message_posted(message)
|
Chris@0
|
213 redmine_headers 'Project' => message.project.identifier,
|
Chris@0
|
214 'Topic-Id' => (message.parent_id || message.id)
|
Chris@1115
|
215 @author = message.author
|
Chris@0
|
216 message_id message
|
Chris@1464
|
217 references message.root
|
Chris@1115
|
218 recipients = message.recipients
|
Chris@1115
|
219 cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients)
|
Chris@1115
|
220 @message = message
|
Chris@1115
|
221 @message_url = url_for(message.event_url)
|
Chris@1115
|
222 mail :to => recipients,
|
Chris@1115
|
223 :cc => cc,
|
Chris@1115
|
224 :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
|
Chris@0
|
225 end
|
Chris@441
|
226
|
Chris@1115
|
227 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added.
|
Chris@0
|
228 #
|
Chris@0
|
229 # Example:
|
Chris@1115
|
230 # wiki_content_added(wiki_content) => Mail::Message object
|
Chris@1115
|
231 # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients
|
Chris@0
|
232 def wiki_content_added(wiki_content)
|
Chris@0
|
233 redmine_headers 'Project' => wiki_content.project.identifier,
|
Chris@0
|
234 'Wiki-Page-Id' => wiki_content.page.id
|
Chris@1115
|
235 @author = wiki_content.author
|
Chris@0
|
236 message_id wiki_content
|
Chris@1115
|
237 recipients = wiki_content.recipients
|
Chris@1115
|
238 cc = wiki_content.page.wiki.watcher_recipients - recipients
|
Chris@1115
|
239 @wiki_content = wiki_content
|
Chris@1115
|
240 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
|
Chris@909
|
241 :project_id => wiki_content.project,
|
Chris@909
|
242 :id => wiki_content.page.title)
|
Chris@1115
|
243 mail :to => recipients,
|
Chris@1115
|
244 :cc => cc,
|
Chris@1115
|
245 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
|
Chris@0
|
246 end
|
Chris@441
|
247
|
Chris@1115
|
248 # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated.
|
Chris@0
|
249 #
|
Chris@0
|
250 # Example:
|
Chris@1115
|
251 # wiki_content_updated(wiki_content) => Mail::Message object
|
Chris@1115
|
252 # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients
|
Chris@0
|
253 def wiki_content_updated(wiki_content)
|
Chris@0
|
254 redmine_headers 'Project' => wiki_content.project.identifier,
|
Chris@0
|
255 'Wiki-Page-Id' => wiki_content.page.id
|
Chris@1115
|
256 @author = wiki_content.author
|
Chris@0
|
257 message_id wiki_content
|
Chris@1115
|
258 recipients = wiki_content.recipients
|
Chris@1115
|
259 cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients
|
Chris@1115
|
260 @wiki_content = wiki_content
|
Chris@1115
|
261 @wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
|
Chris@909
|
262 :project_id => wiki_content.project,
|
Chris@1115
|
263 :id => wiki_content.page.title)
|
Chris@1115
|
264 @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff',
|
Chris@909
|
265 :project_id => wiki_content.project, :id => wiki_content.page.title,
|
Chris@909
|
266 :version => wiki_content.version)
|
Chris@1115
|
267 mail :to => recipients,
|
Chris@1115
|
268 :cc => cc,
|
Chris@1115
|
269 :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
|
Chris@0
|
270 end
|
Chris@0
|
271
|
Chris@1115
|
272 # Builds a Mail::Message object used to email the specified user their account information.
|
Chris@0
|
273 #
|
Chris@0
|
274 # Example:
|
Chris@1115
|
275 # account_information(user, password) => Mail::Message object
|
Chris@1115
|
276 # Mailer.account_information(user, password).deliver => sends account information to the user
|
Chris@0
|
277 def account_information(user, password)
|
Chris@0
|
278 set_language_if_valid user.language
|
Chris@1115
|
279 @user = user
|
Chris@1115
|
280 @password = password
|
Chris@1115
|
281 @login_url = url_for(:controller => 'account', :action => 'login')
|
Chris@1115
|
282 mail :to => user.mail,
|
Chris@1115
|
283 :subject => l(:mail_subject_register, Setting.app_title)
|
Chris@0
|
284 end
|
Chris@0
|
285
|
Chris@1115
|
286 # Builds a Mail::Message object used to email all active administrators of an account activation request.
|
Chris@0
|
287 #
|
Chris@0
|
288 # Example:
|
Chris@1115
|
289 # account_activation_request(user) => Mail::Message object
|
Chris@1115
|
290 # Mailer.account_activation_request(user).deliver => sends an email to all active administrators
|
Chris@0
|
291 def account_activation_request(user)
|
Chris@0
|
292 # Send the email to all active administrators
|
Chris@1464
|
293 recipients = User.active.where(:admin => true).all.collect { |u| u.mail }.compact
|
Chris@1115
|
294 @user = user
|
Chris@1115
|
295 @url = url_for(:controller => 'users', :action => 'index',
|
Chris@909
|
296 :status => User::STATUS_REGISTERED,
|
Chris@909
|
297 :sort_key => 'created_on', :sort_order => 'desc')
|
Chris@1115
|
298 mail :to => recipients,
|
Chris@1115
|
299 :subject => l(:mail_subject_account_activation_request, Setting.app_title)
|
Chris@0
|
300 end
|
Chris@0
|
301
|
Chris@1115
|
302 # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator.
|
Chris@0
|
303 #
|
Chris@0
|
304 # Example:
|
Chris@1115
|
305 # account_activated(user) => Mail::Message object
|
Chris@1115
|
306 # Mailer.account_activated(user).deliver => sends an email to the registered user
|
Chris@0
|
307 def account_activated(user)
|
Chris@0
|
308 set_language_if_valid user.language
|
Chris@1115
|
309 @user = user
|
Chris@1115
|
310 @login_url = url_for(:controller => 'account', :action => 'login')
|
Chris@1115
|
311 mail :to => user.mail,
|
Chris@1115
|
312 :subject => l(:mail_subject_register, Setting.app_title)
|
Chris@0
|
313 end
|
Chris@0
|
314
|
Chris@0
|
315 def lost_password(token)
|
Chris@0
|
316 set_language_if_valid(token.user.language)
|
Chris@1115
|
317 @token = token
|
Chris@1115
|
318 @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
|
Chris@1115
|
319 mail :to => token.user.mail,
|
Chris@1115
|
320 :subject => l(:mail_subject_lost_password, Setting.app_title)
|
Chris@0
|
321 end
|
Chris@0
|
322
|
Chris@0
|
323 def register(token)
|
Chris@0
|
324 set_language_if_valid(token.user.language)
|
Chris@1115
|
325 @token = token
|
Chris@1115
|
326 @url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
|
Chris@1115
|
327 mail :to => token.user.mail,
|
Chris@1115
|
328 :subject => l(:mail_subject_register, Setting.app_title)
|
Chris@0
|
329 end
|
Chris@0
|
330
|
Chris@1115
|
331 def test_email(user)
|
Chris@0
|
332 set_language_if_valid(user.language)
|
Chris@1115
|
333 @url = url_for(:controller => 'welcome')
|
Chris@1115
|
334 mail :to => user.mail,
|
Chris@1115
|
335 :subject => 'Redmine test'
|
Chris@0
|
336 end
|
Chris@0
|
337
|
Chris@0
|
338 # Sends reminders to issue assignees
|
Chris@0
|
339 # Available options:
|
Chris@0
|
340 # * :days => how many days in the future to remind about (defaults to 7)
|
Chris@0
|
341 # * :tracker => id of tracker for filtering issues (defaults to all trackers)
|
Chris@0
|
342 # * :project => id or identifier of project to process (defaults to all projects)
|
Chris@1115
|
343 # * :users => array of user/group ids who should be reminded
|
Chris@0
|
344 def self.reminders(options={})
|
Chris@0
|
345 days = options[:days] || 7
|
Chris@0
|
346 project = options[:project] ? Project.find(options[:project]) : nil
|
Chris@0
|
347 tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
|
chris@22
|
348 user_ids = options[:users]
|
Chris@0
|
349
|
Chris@1115
|
350 scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" +
|
Chris@1115
|
351 " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" +
|
Chris@1115
|
352 " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
|
Chris@1115
|
353 )
|
Chris@1115
|
354 scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
|
Chris@1115
|
355 scope = scope.where(:project_id => project.id) if project
|
Chris@1115
|
356 scope = scope.where(:tracker_id => tracker.id) if tracker
|
Chris@0
|
357
|
Chris@1115
|
358 issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to)
|
Chris@1115
|
359 issues_by_assignee.keys.each do |assignee|
|
Chris@1115
|
360 if assignee.is_a?(Group)
|
Chris@1115
|
361 assignee.users.each do |user|
|
Chris@1115
|
362 issues_by_assignee[user] ||= []
|
Chris@1115
|
363 issues_by_assignee[user] += issues_by_assignee[assignee]
|
Chris@1115
|
364 end
|
Chris@1115
|
365 end
|
Chris@1115
|
366 end
|
Chris@1115
|
367
|
Chris@0
|
368 issues_by_assignee.each do |assignee, issues|
|
Chris@1115
|
369 reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active?
|
Chris@0
|
370 end
|
Chris@0
|
371 end
|
Chris@441
|
372
|
Chris@0
|
373 # Activates/desactivates email deliveries during +block+
|
Chris@0
|
374 def self.with_deliveries(enabled = true, &block)
|
Chris@0
|
375 was_enabled = ActionMailer::Base.perform_deliveries
|
Chris@0
|
376 ActionMailer::Base.perform_deliveries = !!enabled
|
Chris@0
|
377 yield
|
Chris@0
|
378 ensure
|
Chris@0
|
379 ActionMailer::Base.perform_deliveries = was_enabled
|
Chris@0
|
380 end
|
Chris@0
|
381
|
Chris@1115
|
382 # Sends emails synchronously in the given block
|
Chris@1115
|
383 def self.with_synched_deliveries(&block)
|
Chris@1115
|
384 saved_method = ActionMailer::Base.delivery_method
|
Chris@1115
|
385 if m = saved_method.to_s.match(%r{^async_(.+)$})
|
Chris@1115
|
386 synched_method = m[1]
|
Chris@1115
|
387 ActionMailer::Base.delivery_method = synched_method.to_sym
|
Chris@1115
|
388 ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings")
|
Chris@1115
|
389 end
|
Chris@1115
|
390 yield
|
Chris@1115
|
391 ensure
|
Chris@1115
|
392 ActionMailer::Base.delivery_method = saved_method
|
Chris@1115
|
393 end
|
Chris@441
|
394
|
Chris@1464
|
395 def mail(headers={}, &block)
|
Chris@1115
|
396 headers.merge! 'X-Mailer' => 'Redmine',
|
Chris@0
|
397 'X-Redmine-Host' => Setting.host_name,
|
Chris@0
|
398 'X-Redmine-Site' => Setting.app_title,
|
Chris@909
|
399 'X-Auto-Response-Suppress' => 'OOF',
|
Chris@1115
|
400 'Auto-Submitted' => 'auto-generated',
|
Chris@1115
|
401 'From' => Setting.mail_from,
|
Chris@1115
|
402 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>"
|
Chris@1115
|
403
|
Chris@1115
|
404 # Removes the author from the recipients and cc
|
Chris@1464
|
405 # if the author does not want to receive notifications
|
Chris@1464
|
406 # about what the author do
|
Chris@1464
|
407 if @author && @author.logged? && @author.pref.no_self_notified
|
Chris@1115
|
408 headers[:to].delete(@author.mail) if headers[:to].is_a?(Array)
|
Chris@1115
|
409 headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array)
|
Chris@1115
|
410 end
|
Chris@1115
|
411
|
Chris@1115
|
412 if @author && @author.logged?
|
Chris@1115
|
413 redmine_headers 'Sender' => @author.login
|
Chris@1115
|
414 end
|
Chris@1115
|
415
|
Chris@1115
|
416 # Blind carbon copy recipients
|
Chris@1115
|
417 if Setting.bcc_recipients?
|
Chris@1115
|
418 headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?)
|
Chris@1115
|
419 headers[:to] = nil
|
Chris@1115
|
420 headers[:cc] = nil
|
Chris@1115
|
421 end
|
Chris@1115
|
422
|
Chris@1115
|
423 if @message_id_object
|
Chris@1115
|
424 headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>"
|
Chris@1115
|
425 end
|
Chris@1115
|
426 if @references_objects
|
Chris@1464
|
427 headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o)}>"}.join(' ')
|
Chris@1115
|
428 end
|
Chris@1115
|
429
|
Chris@1464
|
430 m = if block_given?
|
Chris@1464
|
431 super headers, &block
|
Chris@1464
|
432 else
|
Chris@1464
|
433 super headers do |format|
|
Chris@1464
|
434 format.text
|
Chris@1464
|
435 format.html unless Setting.plain_text_mail?
|
Chris@1464
|
436 end
|
Chris@1115
|
437 end
|
Chris@1464
|
438 set_language_if_valid @initial_language
|
Chris@1115
|
439
|
Chris@1464
|
440 m
|
Chris@0
|
441 end
|
Chris@0
|
442
|
Chris@1115
|
443 def initialize(*args)
|
Chris@1115
|
444 @initial_language = current_language
|
Chris@1115
|
445 set_language_if_valid Setting.default_language
|
Chris@1115
|
446 super
|
Chris@1115
|
447 end
|
Chris@1464
|
448
|
Chris@1115
|
449 def self.deliver_mail(mail)
|
Chris@1115
|
450 return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
|
Chris@1464
|
451 begin
|
Chris@1464
|
452 # Log errors when raise_delivery_errors is set to false, Rails does not
|
Chris@1464
|
453 mail.raise_delivery_errors = true
|
Chris@1464
|
454 super
|
Chris@1464
|
455 rescue Exception => e
|
Chris@1464
|
456 if ActionMailer::Base.raise_delivery_errors
|
Chris@1464
|
457 raise e
|
Chris@1464
|
458 else
|
Chris@1464
|
459 Rails.logger.error "Email delivery error: #{e.message}"
|
Chris@1464
|
460 end
|
Chris@1464
|
461 end
|
Chris@1115
|
462 end
|
Chris@1115
|
463
|
Chris@1115
|
464 def self.method_missing(method, *args, &block)
|
Chris@1115
|
465 if m = method.to_s.match(%r{^deliver_(.+)$})
|
Chris@1115
|
466 ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead."
|
Chris@1115
|
467 send(m[1], *args).deliver
|
Chris@1115
|
468 else
|
Chris@1115
|
469 super
|
Chris@1115
|
470 end
|
Chris@1115
|
471 end
|
Chris@1115
|
472
|
Chris@1115
|
473 private
|
Chris@1115
|
474
|
Chris@0
|
475 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
|
Chris@0
|
476 def redmine_headers(h)
|
Chris@1115
|
477 h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s }
|
Chris@0
|
478 end
|
Chris@0
|
479
|
Chris@1464
|
480 def self.token_for(object, rand=true)
|
Chris@441
|
481 timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
|
Chris@1464
|
482 hash = [
|
Chris@1464
|
483 "redmine",
|
Chris@1464
|
484 "#{object.class.name.demodulize.underscore}-#{object.id}",
|
Chris@1464
|
485 timestamp.strftime("%Y%m%d%H%M%S")
|
Chris@1464
|
486 ]
|
Chris@1464
|
487 if rand
|
Chris@1464
|
488 hash << Redmine::Utils.random_hex(8)
|
Chris@1464
|
489 end
|
Chris@0
|
490 host = Setting.mail_from.to_s.gsub(%r{^.*@}, '')
|
Chris@0
|
491 host = "#{::Socket.gethostname}.redmine" if host.empty?
|
Chris@1464
|
492 "#{hash.join('.')}@#{host}"
|
Chris@1464
|
493 end
|
Chris@1464
|
494
|
Chris@1464
|
495 # Returns a Message-Id for the given object
|
Chris@1464
|
496 def self.message_id_for(object)
|
Chris@1464
|
497 token_for(object, true)
|
Chris@1464
|
498 end
|
Chris@1464
|
499
|
Chris@1464
|
500 # Returns a uniq token for a given object referenced by all notifications
|
Chris@1464
|
501 # related to this object
|
Chris@1464
|
502 def self.references_for(object)
|
Chris@1464
|
503 token_for(object, false)
|
Chris@0
|
504 end
|
Chris@441
|
505
|
Chris@0
|
506 def message_id(object)
|
Chris@0
|
507 @message_id_object = object
|
Chris@0
|
508 end
|
Chris@441
|
509
|
Chris@0
|
510 def references(object)
|
Chris@0
|
511 @references_objects ||= []
|
Chris@0
|
512 @references_objects << object
|
Chris@0
|
513 end
|
Chris@441
|
514
|
Chris@0
|
515 def mylogger
|
Chris@909
|
516 Rails.logger
|
Chris@0
|
517 end
|
Chris@0
|
518 end
|
Chris@929
|
519
|
Chris@929
|
520 # Patch TMail so that message_id is not overwritten
|
Chris@1116
|
521
|
Chris@1116
|
522 ### NB: Redmine 2.2 no longer uses TMail I think? This function has
|
Chris@1116
|
523 ### been removed there
|
Chris@1116
|
524
|
Chris@929
|
525 module TMail
|
Chris@929
|
526 class Mail
|
Chris@929
|
527 def add_message_id( fqdn = nil )
|
Chris@929
|
528 self.message_id ||= ::TMail::new_message_id(fqdn)
|
Chris@929
|
529 end
|
Chris@929
|
530 end
|
Chris@929
|
531 end
|
luis@291
|
532
|