Chris@441: # Redmine - project management software Chris@441: # Copyright (C) 2006-2011 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: chris@22: require 'ar_condition' chris@22: Chris@0: class Mailer < ActionMailer::Base Chris@0: layout 'mailer' Chris@0: helper :application Chris@0: helper :issues Chris@0: helper :custom_fields Chris@0: Chris@0: include ActionController::UrlWriter Chris@0: include Redmine::I18n Chris@0: Chris@0: def self.default_url_options Chris@0: h = Setting.host_name Chris@0: h = h.to_s.gsub(%r{\/.*$}, '') unless Redmine::Utils.relative_url_root.blank? Chris@0: { :host => h, :protocol => Setting.protocol } Chris@0: end Chris@441: Chris@0: # Builds a tmail object used to email recipients of the added issue. Chris@0: # Chris@0: # Example: Chris@0: # issue_add(issue) => tmail object Chris@0: # Mailer.deliver_issue_add(issue) => sends an email to issue recipients Chris@0: def issue_add(issue) Chris@0: redmine_headers 'Project' => issue.project.identifier, Chris@0: 'Issue-Id' => issue.id, Chris@0: 'Issue-Author' => issue.author.login Chris@0: redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to Chris@0: message_id issue Chris@0: recipients issue.recipients Chris@0: cc(issue.watcher_recipients - @recipients) Chris@0: subject "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" Chris@0: body :issue => issue, Chris@0: :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue) Chris@0: render_multipart('issue_add', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email recipients of the edited issue. Chris@0: # Chris@0: # Example: Chris@0: # issue_edit(journal) => tmail object Chris@0: # Mailer.deliver_issue_edit(journal) => sends an email to issue recipients Chris@0: def issue_edit(journal) Chris@0: issue = journal.journalized.reload Chris@0: redmine_headers 'Project' => issue.project.identifier, Chris@0: 'Issue-Id' => issue.id, Chris@0: 'Issue-Author' => issue.author.login Chris@0: redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to Chris@0: message_id journal Chris@0: references issue Chris@0: @author = journal.user Chris@0: recipients issue.recipients Chris@0: # Watchers in cc Chris@0: cc(issue.watcher_recipients - @recipients) Chris@0: s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " Chris@0: s << "(#{issue.status.name}) " if journal.new_value_for('status_id') Chris@0: s << issue.subject Chris@0: subject s Chris@0: body :issue => issue, Chris@0: :journal => journal, Chris@909: :issue_url => url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") Chris@0: Chris@0: render_multipart('issue_edit', body) Chris@0: end Chris@0: Chris@0: def reminder(user, issues, days) Chris@0: set_language_if_valid user.language Chris@0: recipients user.mail Chris@14: subject l(:mail_subject_reminder, :count => issues.size, :days => days) Chris@0: body :issues => issues, Chris@0: :days => days, Chris@909: :issues_url => url_for(:controller => 'issues', :action => 'index', Chris@909: :set_filter => 1, :assigned_to_id => user.id, Chris@909: :sort => 'due_date:asc') Chris@0: render_multipart('reminder', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email users belonging to the added document's project. Chris@0: # Chris@0: # Example: Chris@0: # document_added(document) => tmail object Chris@0: # Mailer.deliver_document_added(document) => sends an email to the document's project recipients Chris@0: def document_added(document) Chris@0: redmine_headers 'Project' => document.project.identifier Chris@0: recipients document.recipients Chris@0: subject "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" Chris@0: body :document => document, Chris@0: :document_url => url_for(:controller => 'documents', :action => 'show', :id => document) Chris@0: render_multipart('document_added', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email recipients of a project when an attachements are added. Chris@0: # Chris@0: # Example: Chris@0: # attachments_added(attachments) => tmail object Chris@0: # Mailer.deliver_attachments_added(attachments) => sends an email to the project's recipients Chris@0: def attachments_added(attachments) Chris@0: container = attachments.first.container Chris@0: added_to = '' Chris@0: added_to_url = '' Chris@0: case container.class.name Chris@0: when 'Project' Chris@441: added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) Chris@0: added_to = "#{l(:label_project)}: #{container}" Chris@0: recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} Chris@0: when 'Version' Chris@441: added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) Chris@0: added_to = "#{l(:label_version)}: #{container.name}" Chris@0: recipients container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} Chris@0: when 'Document' Chris@0: added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) Chris@0: added_to = "#{l(:label_document)}: #{container.title}" Chris@0: recipients container.recipients Chris@0: end Chris@0: redmine_headers 'Project' => container.project.identifier Chris@0: subject "[#{container.project.name}] #{l(:label_attachment_new)}" Chris@0: body :attachments => attachments, Chris@0: :added_to => added_to, Chris@0: :added_to_url => added_to_url Chris@0: render_multipart('attachments_added', body) Chris@0: end Chris@441: Chris@0: # Builds a tmail object used to email recipients of a news' project when a news item is added. Chris@0: # Chris@0: # Example: Chris@0: # news_added(news) => tmail object Chris@0: # Mailer.deliver_news_added(news) => sends an email to the news' project recipients Chris@0: def news_added(news) Chris@0: redmine_headers 'Project' => news.project.identifier Chris@0: message_id news Chris@0: recipients news.recipients Chris@0: subject "[#{news.project.name}] #{l(:label_news)}: #{news.title}" Chris@0: body :news => news, Chris@0: :news_url => url_for(:controller => 'news', :action => 'show', :id => news) Chris@0: render_multipart('news_added', body) Chris@0: end Chris@0: Chris@441: # Builds a tmail object used to email recipients of a news' project when a news comment is added. Chris@441: # Chris@441: # Example: Chris@441: # news_comment_added(comment) => tmail object Chris@441: # Mailer.news_comment_added(comment) => sends an email to the news' project recipients Chris@441: def news_comment_added(comment) Chris@441: news = comment.commented Chris@441: redmine_headers 'Project' => news.project.identifier Chris@441: message_id comment Chris@441: recipients news.recipients Chris@441: cc news.watcher_recipients Chris@441: subject "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}" Chris@441: body :news => news, Chris@441: :comment => comment, Chris@441: :news_url => url_for(:controller => 'news', :action => 'show', :id => news) Chris@441: render_multipart('news_comment_added', body) Chris@441: end Chris@441: Chris@441: # Builds a tmail object used to email the recipients of the specified message that was posted. Chris@0: # Chris@0: # Example: Chris@0: # message_posted(message) => tmail object Chris@0: # Mailer.deliver_message_posted(message) => sends an email to the recipients Chris@0: def message_posted(message) Chris@0: redmine_headers 'Project' => message.project.identifier, Chris@0: 'Topic-Id' => (message.parent_id || message.id) Chris@0: message_id message Chris@0: references message.parent unless message.parent.nil? Chris@0: recipients(message.recipients) Chris@0: cc((message.root.watcher_recipients + message.board.watcher_recipients).uniq - @recipients) Chris@0: subject "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" Chris@0: body :message => message, Chris@0: :message_url => url_for(message.event_url) Chris@0: render_multipart('message_posted', body) Chris@0: end Chris@441: Chris@441: # Builds a tmail object used to email the recipients of a project of the specified wiki content was added. Chris@0: # Chris@0: # Example: Chris@0: # wiki_content_added(wiki_content) => tmail object Chris@0: # Mailer.deliver_wiki_content_added(wiki_content) => sends an email to the project's recipients Chris@0: def wiki_content_added(wiki_content) Chris@0: redmine_headers 'Project' => wiki_content.project.identifier, Chris@0: 'Wiki-Page-Id' => wiki_content.page.id Chris@0: message_id wiki_content Chris@0: recipients wiki_content.recipients Chris@0: cc(wiki_content.page.wiki.watcher_recipients - recipients) chris@37: subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" Chris@0: body :wiki_content => wiki_content, Chris@909: :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', Chris@909: :project_id => wiki_content.project, Chris@909: :id => wiki_content.page.title) Chris@0: render_multipart('wiki_content_added', body) Chris@0: end Chris@441: Chris@441: # Builds a tmail object used to email the recipients of a project of the specified wiki content was updated. Chris@0: # Chris@0: # Example: Chris@0: # wiki_content_updated(wiki_content) => tmail object Chris@0: # Mailer.deliver_wiki_content_updated(wiki_content) => sends an email to the project's recipients Chris@0: def wiki_content_updated(wiki_content) Chris@0: redmine_headers 'Project' => wiki_content.project.identifier, Chris@0: 'Wiki-Page-Id' => wiki_content.page.id Chris@0: message_id wiki_content Chris@0: recipients wiki_content.recipients Chris@0: cc(wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients) chris@37: subject "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}" Chris@0: body :wiki_content => wiki_content, Chris@909: :wiki_content_url => url_for(:controller => 'wiki', :action => 'show', Chris@909: :project_id => wiki_content.project, Chris@909: :id => wiki_content.page.title), Chris@909: :wiki_diff_url => url_for(:controller => 'wiki', :action => 'diff', Chris@909: :project_id => wiki_content.project, :id => wiki_content.page.title, Chris@909: :version => wiki_content.version) Chris@0: render_multipart('wiki_content_updated', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email the specified user their account information. Chris@0: # Chris@0: # Example: Chris@0: # account_information(user, password) => tmail object Chris@0: # Mailer.deliver_account_information(user, password) => sends account information to the user Chris@0: def account_information(user, password) Chris@0: set_language_if_valid user.language Chris@0: recipients user.mail Chris@0: subject l(:mail_subject_register, Setting.app_title) Chris@0: body :user => user, Chris@0: :password => password, Chris@0: :login_url => url_for(:controller => 'account', :action => 'login') Chris@0: render_multipart('account_information', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email all active administrators of an account activation request. Chris@0: # Chris@0: # Example: Chris@0: # account_activation_request(user) => tmail object Chris@0: # Mailer.deliver_account_activation_request(user)=> sends an email to all active administrators Chris@0: def account_activation_request(user) Chris@0: # Send the email to all active administrators Chris@0: recipients User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact Chris@0: subject l(:mail_subject_account_activation_request, Setting.app_title) Chris@0: body :user => user, Chris@909: :url => url_for(:controller => 'users', :action => 'index', Chris@909: :status => User::STATUS_REGISTERED, Chris@909: :sort_key => 'created_on', :sort_order => 'desc') Chris@0: render_multipart('account_activation_request', body) Chris@0: end Chris@0: Chris@0: # Builds a tmail object used to email the specified user that their account was activated by an administrator. Chris@0: # Chris@0: # Example: Chris@0: # account_activated(user) => tmail object Chris@0: # Mailer.deliver_account_activated(user) => sends an email to the registered user Chris@0: def account_activated(user) Chris@0: set_language_if_valid user.language Chris@0: recipients user.mail Chris@0: subject l(:mail_subject_register, Setting.app_title) Chris@0: body :user => user, Chris@0: :login_url => url_for(:controller => 'account', :action => 'login') Chris@0: render_multipart('account_activated', body) Chris@0: end Chris@0: Chris@0: def lost_password(token) Chris@0: set_language_if_valid(token.user.language) Chris@0: recipients token.user.mail Chris@0: subject l(:mail_subject_lost_password, Setting.app_title) Chris@0: body :token => token, Chris@0: :url => url_for(:controller => 'account', :action => 'lost_password', :token => token.value) Chris@0: render_multipart('lost_password', body) Chris@0: end Chris@0: Chris@0: def register(token) Chris@0: set_language_if_valid(token.user.language) Chris@0: recipients token.user.mail Chris@0: subject l(:mail_subject_register, Setting.app_title) Chris@0: body :token => token, Chris@0: :url => url_for(:controller => 'account', :action => 'activate', :token => token.value) Chris@0: render_multipart('register', body) Chris@0: end Chris@0: Chris@0: def test(user) Chris@0: set_language_if_valid(user.language) Chris@0: recipients user.mail Chris@0: subject 'Redmine test' Chris@0: body :url => url_for(:controller => 'welcome') Chris@0: render_multipart('test', body) Chris@0: end Chris@0: Chris@0: # Overrides default deliver! method to prevent from sending an email Chris@0: # with no recipient, cc or bcc Chris@0: def deliver!(mail = @mail) Chris@0: set_language_if_valid @initial_language Chris@0: return false if (recipients.nil? || recipients.empty?) && Chris@0: (cc.nil? || cc.empty?) && Chris@0: (bcc.nil? || bcc.empty?) Chris@441: Chris@0: # Set Message-Id and References Chris@0: if @message_id_object Chris@0: mail.message_id = self.class.message_id_for(@message_id_object) Chris@0: end Chris@0: if @references_objects Chris@0: mail.references = @references_objects.collect {|o| self.class.message_id_for(o)} Chris@0: end Chris@441: Chris@0: # Log errors when raise_delivery_errors is set to false, Rails does not Chris@0: raise_errors = self.class.raise_delivery_errors Chris@0: self.class.raise_delivery_errors = true Chris@0: begin Chris@0: return super(mail) Chris@0: rescue Exception => e Chris@0: if raise_errors Chris@0: raise e Chris@0: elsif mylogger Chris@210: mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml." Chris@0: end Chris@0: ensure Chris@0: self.class.raise_delivery_errors = raise_errors Chris@0: end Chris@0: end Chris@0: Chris@0: # Sends reminders to issue assignees Chris@0: # Available options: Chris@0: # * :days => how many days in the future to remind about (defaults to 7) Chris@0: # * :tracker => id of tracker for filtering issues (defaults to all trackers) Chris@0: # * :project => id or identifier of project to process (defaults to all projects) chris@22: # * :users => array of user ids who should be reminded Chris@0: def self.reminders(options={}) Chris@0: days = options[:days] || 7 Chris@0: project = options[:project] ? Project.find(options[:project]) : nil Chris@0: tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil chris@22: user_ids = options[:users] Chris@0: Chris@0: s = ARCondition.new ["#{IssueStatus.table_name}.is_closed = ? AND #{Issue.table_name}.due_date <= ?", false, days.day.from_now.to_date] Chris@0: s << "#{Issue.table_name}.assigned_to_id IS NOT NULL" chris@22: s << ["#{Issue.table_name}.assigned_to_id IN (?)", user_ids] if user_ids.present? Chris@0: s << "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}" Chris@0: s << "#{Issue.table_name}.project_id = #{project.id}" if project Chris@0: s << "#{Issue.table_name}.tracker_id = #{tracker.id}" if tracker Chris@0: Chris@0: issues_by_assignee = Issue.find(:all, :include => [:status, :assigned_to, :project, :tracker], Chris@0: :conditions => s.conditions Chris@0: ).group_by(&:assigned_to) Chris@0: issues_by_assignee.each do |assignee, issues| Chris@929: deliver_reminder(assignee, issues, days) if assignee.is_a?(User) && assignee.active? Chris@0: end Chris@0: end Chris@441: Chris@0: # Activates/desactivates email deliveries during +block+ Chris@0: def self.with_deliveries(enabled = true, &block) Chris@0: was_enabled = ActionMailer::Base.perform_deliveries Chris@0: ActionMailer::Base.perform_deliveries = !!enabled Chris@0: yield Chris@0: ensure Chris@0: ActionMailer::Base.perform_deliveries = was_enabled Chris@0: end Chris@0: Chris@0: private Chris@0: def initialize_defaults(method_name) Chris@0: super Chris@0: @initial_language = current_language Chris@0: set_language_if_valid Setting.default_language Chris@0: from Setting.mail_from Chris@441: Chris@0: # Common headers Chris@0: headers 'X-Mailer' => 'Redmine', Chris@0: 'X-Redmine-Host' => Setting.host_name, Chris@0: 'X-Redmine-Site' => Setting.app_title, Chris@909: 'X-Auto-Response-Suppress' => 'OOF', Chris@0: 'Auto-Submitted' => 'auto-generated' Chris@0: end Chris@0: Chris@0: # Appends a Redmine header field (name is prepended with 'X-Redmine-') Chris@0: def redmine_headers(h) Chris@0: h.each { |k,v| headers["X-Redmine-#{k}"] = v } Chris@0: end Chris@0: Chris@0: # Overrides the create_mail method Chris@0: def create_mail Chris@0: # Removes the current user from the recipients and cc Chris@0: # if he doesn't want to receive notifications about what he does Chris@0: @author ||= User.current Chris@0: if @author.pref[:no_self_notified] Chris@0: recipients.delete(@author.mail) if recipients Chris@0: cc.delete(@author.mail) if cc Chris@0: end Chris@441: Chris@0: notified_users = [recipients, cc].flatten.compact.uniq Chris@0: # Rails would log recipients only, not cc and bcc Chris@0: mylogger.info "Sending email notification to: #{notified_users.join(', ')}" if mylogger Chris@441: Chris@0: # Blind carbon copy recipients Chris@0: if Setting.bcc_recipients? Chris@0: bcc(notified_users) Chris@0: recipients [] Chris@0: cc [] Chris@0: end Chris@0: super Chris@0: end Chris@0: Chris@0: # Rails 2.3 has problems rendering implicit multipart messages with Chris@0: # layouts so this method will wrap an multipart messages with Chris@0: # explicit parts. Chris@0: # Chris@0: # https://rails.lighthouseapp.com/projects/8994/tickets/2338-actionmailer-mailer-views-and-content-type Chris@0: # https://rails.lighthouseapp.com/projects/8994/tickets/1799-actionmailer-doesnt-set-template_format-when-rendering-layouts Chris@441: Chris@0: def render_multipart(method_name, body) Chris@0: if Setting.plain_text_mail? Chris@0: content_type "text/plain" Chris@909: body render(:file => "#{method_name}.text.erb", Chris@909: :body => body, Chris@909: :layout => 'mailer.text.erb') Chris@0: else Chris@0: content_type "multipart/alternative" Chris@909: part :content_type => "text/plain", Chris@909: :body => render(:file => "#{method_name}.text.erb", Chris@909: :body => body, :layout => 'mailer.text.erb') Chris@909: part :content_type => "text/html", Chris@909: :body => render_message("#{method_name}.html.erb", body) Chris@0: end Chris@0: end Chris@0: Chris@0: # Makes partial rendering work with Rails 1.2 (retro-compatibility) Chris@0: def self.controller_path Chris@0: '' Chris@0: end unless respond_to?('controller_path') Chris@441: Chris@0: # Returns a predictable Message-Id for the given object Chris@0: def self.message_id_for(object) Chris@0: # id + timestamp should reduce the odds of a collision Chris@0: # as far as we don't send multiple emails for the same object Chris@441: timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) Chris@0: hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" Chris@0: host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') Chris@0: host = "#{::Socket.gethostname}.redmine" if host.empty? Chris@0: "<#{hash}@#{host}>" Chris@0: end Chris@441: Chris@0: private Chris@441: Chris@0: def message_id(object) Chris@0: @message_id_object = object Chris@0: end Chris@441: Chris@0: def references(object) Chris@0: @references_objects ||= [] Chris@0: @references_objects << object Chris@0: end Chris@441: Chris@0: def mylogger Chris@909: Rails.logger Chris@0: end Chris@0: end Chris@0: Chris@0: # Patch TMail so that message_id is not overwritten Chris@0: module TMail Chris@0: class Mail Chris@0: def add_message_id( fqdn = nil ) Chris@0: self.message_id ||= ::TMail::new_message_id(fqdn) Chris@0: end Chris@0: end Chris@0: end