##// END OF EJS Templates
Add the following headers to email notifications (#830, #247):...
Jean-Philippe Lang -
r1250:f271e772afba
parent child
Show More
@@ -1,172 +1,194
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
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 class Mailer < ActionMailer::Base
18 class Mailer < ActionMailer::Base
19 helper :application
19 helper :application
20 helper :issues
20 helper :issues
21 helper :custom_fields
21 helper :custom_fields
22
22
23 include ActionController::UrlWriter
23 include ActionController::UrlWriter
24
24
25 def issue_add(issue)
25 def issue_add(issue)
26 redmine_headers 'Project' => issue.project.identifier,
27 'Issue-Id' => issue.id,
28 'Issue-Author' => issue.author.login
29 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
26 recipients issue.recipients
30 recipients issue.recipients
27 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
31 subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}"
28 body :issue => issue,
32 body :issue => issue,
29 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
33 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
30 end
34 end
31
35
32 def issue_edit(journal)
36 def issue_edit(journal)
33 issue = journal.journalized
37 issue = journal.journalized
38 redmine_headers 'Project' => issue.project.identifier,
39 'Issue-Id' => issue.id,
40 'Issue-Author' => issue.author.login
41 redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to
34 recipients issue.recipients
42 recipients issue.recipients
35 # Watchers in cc
43 # Watchers in cc
36 cc(issue.watcher_recipients - @recipients)
44 cc(issue.watcher_recipients - @recipients)
37 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
45 s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
38 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
46 s << "(#{issue.status.name}) " if journal.new_value_for('status_id')
39 s << issue.subject
47 s << issue.subject
40 subject s
48 subject s
41 body :issue => issue,
49 body :issue => issue,
42 :journal => journal,
50 :journal => journal,
43 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
51 :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue)
44 end
52 end
45
53
46 def document_added(document)
54 def document_added(document)
55 redmine_headers 'Project' => document.project.identifier
47 recipients document.project.recipients
56 recipients document.project.recipients
48 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
57 subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
49 body :document => document,
58 body :document => document,
50 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
59 :document_url => url_for(:controller => 'documents', :action => 'show', :id => document)
51 end
60 end
52
61
53 def attachments_added(attachments)
62 def attachments_added(attachments)
54 container = attachments.first.container
63 container = attachments.first.container
55 added_to = ''
64 added_to = ''
56 added_to_url = ''
65 added_to_url = ''
57 case container.class.name
66 case container.class.name
58 when 'Version'
67 when 'Version'
59 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
68 added_to_url = url_for(:controller => 'projects', :action => 'list_files', :id => container.project_id)
60 added_to = "#{l(:label_version)}: #{container.name}"
69 added_to = "#{l(:label_version)}: #{container.name}"
61 when 'Document'
70 when 'Document'
62 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
71 added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
63 added_to = "#{l(:label_document)}: #{container.title}"
72 added_to = "#{l(:label_document)}: #{container.title}"
64 end
73 end
74 redmine_headers 'Project' => container.project.identifier
65 recipients container.project.recipients
75 recipients container.project.recipients
66 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
76 subject "[#{container.project.name}] #{l(:label_attachment_new)}"
67 body :attachments => attachments,
77 body :attachments => attachments,
68 :added_to => added_to,
78 :added_to => added_to,
69 :added_to_url => added_to_url
79 :added_to_url => added_to_url
70 end
80 end
71
81
72 def news_added(news)
82 def news_added(news)
83 redmine_headers 'Project' => news.project.identifier
73 recipients news.project.recipients
84 recipients news.project.recipients
74 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
85 subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
75 body :news => news,
86 body :news => news,
76 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
87 :news_url => url_for(:controller => 'news', :action => 'show', :id => news)
77 end
88 end
78
89
79 def message_posted(message, recipients)
90 def message_posted(message, recipients)
91 redmine_headers 'Project' => message.project.identifier,
92 'Topic-Id' => (message.parent_id || message.id)
80 recipients(recipients)
93 recipients(recipients)
81 subject "[#{message.board.project.name} - #{message.board.name}] #{message.subject}"
94 subject "[#{message.board.project.name} - #{message.board.name}] #{message.subject}"
82 body :message => message,
95 body :message => message,
83 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
96 :message_url => url_for(:controller => 'messages', :action => 'show', :board_id => message.board_id, :id => message.root)
84 end
97 end
85
98
86 def account_information(user, password)
99 def account_information(user, password)
87 set_language_if_valid user.language
100 set_language_if_valid user.language
88 recipients user.mail
101 recipients user.mail
89 subject l(:mail_subject_register, Setting.app_title)
102 subject l(:mail_subject_register, Setting.app_title)
90 body :user => user,
103 body :user => user,
91 :password => password,
104 :password => password,
92 :login_url => url_for(:controller => 'account', :action => 'login')
105 :login_url => url_for(:controller => 'account', :action => 'login')
93 end
106 end
94
107
95 def account_activation_request(user)
108 def account_activation_request(user)
96 # Send the email to all active administrators
109 # Send the email to all active administrators
97 recipients User.find_active(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
110 recipients User.find_active(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact
98 subject l(:mail_subject_account_activation_request, Setting.app_title)
111 subject l(:mail_subject_account_activation_request, Setting.app_title)
99 body :user => user,
112 body :user => user,
100 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
113 :url => url_for(:controller => 'users', :action => 'index', :status => User::STATUS_REGISTERED, :sort_key => 'created_on', :sort_order => 'desc')
101 end
114 end
102
115
103 def lost_password(token)
116 def lost_password(token)
104 set_language_if_valid(token.user.language)
117 set_language_if_valid(token.user.language)
105 recipients token.user.mail
118 recipients token.user.mail
106 subject l(:mail_subject_lost_password, Setting.app_title)
119 subject l(:mail_subject_lost_password, Setting.app_title)
107 body :token => token,
120 body :token => token,
108 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
121 :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
109 end
122 end
110
123
111 def register(token)
124 def register(token)
112 set_language_if_valid(token.user.language)
125 set_language_if_valid(token.user.language)
113 recipients token.user.mail
126 recipients token.user.mail
114 subject l(:mail_subject_register, Setting.app_title)
127 subject l(:mail_subject_register, Setting.app_title)
115 body :token => token,
128 body :token => token,
116 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
129 :url => url_for(:controller => 'account', :action => 'activate', :token => token.value)
117 end
130 end
118
131
119 def test(user)
132 def test(user)
120 set_language_if_valid(user.language)
133 set_language_if_valid(user.language)
121 recipients user.mail
134 recipients user.mail
122 subject 'Redmine test'
135 subject 'Redmine test'
123 body :url => url_for(:controller => 'welcome')
136 body :url => url_for(:controller => 'welcome')
124 end
137 end
125
138
126 # Overrides default deliver! method to prevent from sending an email
139 # Overrides default deliver! method to prevent from sending an email
127 # with no recipient, cc or bcc
140 # with no recipient, cc or bcc
128 def deliver!(mail = @mail)
141 def deliver!(mail = @mail)
129 return false if (recipients.nil? || recipients.empty?) &&
142 return false if (recipients.nil? || recipients.empty?) &&
130 (cc.nil? || cc.empty?) &&
143 (cc.nil? || cc.empty?) &&
131 (bcc.nil? || bcc.empty?)
144 (bcc.nil? || bcc.empty?)
132 super
145 super
133 end
146 end
134
147
135 private
148 private
136 def initialize_defaults(method_name)
149 def initialize_defaults(method_name)
137 super
150 super
138 set_language_if_valid Setting.default_language
151 set_language_if_valid Setting.default_language
139 from Setting.mail_from
152 from Setting.mail_from
140 default_url_options[:host] = Setting.host_name
153 default_url_options[:host] = Setting.host_name
141 default_url_options[:protocol] = Setting.protocol
154 default_url_options[:protocol] = Setting.protocol
155 # Common headers
156 headers 'X-Mailer' => 'Redmine',
157 'X-Redmine-Host' => Setting.host_name,
158 'X-Redmine-Site' => Setting.app_title
159 end
160
161 # Appends a Redmine header field (name is prepended with 'X-Redmine-')
162 def redmine_headers(h)
163 h.each { |k,v| headers["X-Redmine-#{k}"] = v }
142 end
164 end
143
165
144 # Overrides the create_mail method
166 # Overrides the create_mail method
145 def create_mail
167 def create_mail
146 # Removes the current user from the recipients and cc
168 # Removes the current user from the recipients and cc
147 # if he doesn't want to receive notifications about what he does
169 # if he doesn't want to receive notifications about what he does
148 if User.current.pref[:no_self_notified]
170 if User.current.pref[:no_self_notified]
149 recipients.delete(User.current.mail) if recipients
171 recipients.delete(User.current.mail) if recipients
150 cc.delete(User.current.mail) if cc
172 cc.delete(User.current.mail) if cc
151 end
173 end
152 # Blind carbon copy recipients
174 # Blind carbon copy recipients
153 if Setting.bcc_recipients?
175 if Setting.bcc_recipients?
154 bcc([recipients, cc].flatten.compact.uniq)
176 bcc([recipients, cc].flatten.compact.uniq)
155 recipients []
177 recipients []
156 cc []
178 cc []
157 end
179 end
158 super
180 super
159 end
181 end
160
182
161 # Renders a message with the corresponding layout
183 # Renders a message with the corresponding layout
162 def render_message(method_name, body)
184 def render_message(method_name, body)
163 layout = method_name.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
185 layout = method_name.match(%r{text\.html\.(rhtml|rxml)}) ? 'layout.text.html.rhtml' : 'layout.text.plain.rhtml'
164 body[:content_for_layout] = render(:file => method_name, :body => body)
186 body[:content_for_layout] = render(:file => method_name, :body => body)
165 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}")
187 ActionView::Base.new(template_root, body, self).render(:file => "mailer/#{layout}")
166 end
188 end
167
189
168 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
190 # Makes partial rendering work with Rails 1.2 (retro-compatibility)
169 def self.controller_path
191 def self.controller_path
170 ''
192 ''
171 end unless respond_to?('controller_path')
193 end unless respond_to?('controller_path')
172 end
194 end
General Comments 0
You need to be logged in to leave comments. Login now