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 18KB

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