You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mailer.rb 20KB

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