Chris@1296: # Redmine - project management software Chris@1296: # Copyright (C) 2006-2012 Jean-Philippe Lang Chris@1296: # Chris@1296: # This program is free software; you can redistribute it and/or Chris@1296: # modify it under the terms of the GNU General Public License Chris@1296: # as published by the Free Software Foundation; either version 2 Chris@1296: # of the License, or (at your option) any later version. Chris@1296: # Chris@1296: # This program is distributed in the hope that it will be useful, Chris@1296: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1296: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1296: # GNU General Public License for more details. Chris@1296: # Chris@1296: # You should have received a copy of the GNU General Public License Chris@1296: # along with this program; if not, write to the Free Software Chris@1296: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1296: Chris@1296: class Mailer < ActionMailer::Base Chris@1296: layout 'mailer' Chris@1296: helper :application Chris@1296: helper :issues Chris@1296: helper :custom_fields Chris@1296: Chris@1296: include Redmine::I18n Chris@1296: Chris@1296: def self.default_url_options Chris@1296: { :host => Setting.host_name, :protocol => Setting.protocol } Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email recipients of the added issue. Chris@1296: # Chris@1296: # Example: Chris@1296: # issue_add(issue) => Mail::Message object Chris@1296: # Mailer.issue_add(issue).deliver => sends an email to issue recipients Chris@1296: def issue_add(issue) Chris@1296: redmine_headers 'Project' => issue.project.identifier, Chris@1296: 'Issue-Id' => issue.id, Chris@1296: 'Issue-Author' => issue.author.login Chris@1296: redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to Chris@1296: message_id issue Chris@1296: @author = issue.author Chris@1296: @issue = issue Chris@1296: @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue) Chris@1296: recipients = issue.recipients Chris@1296: cc = issue.watcher_recipients - recipients Chris@1296: mail :to => recipients, Chris@1296: :cc => cc, Chris@1296: :subject => "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] (#{issue.status.name}) #{issue.subject}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email recipients of the edited issue. Chris@1296: # Chris@1296: # Example: Chris@1296: # issue_edit(journal) => Mail::Message object Chris@1296: # Mailer.issue_edit(journal).deliver => sends an email to issue recipients Chris@1296: def issue_edit(journal) Chris@1296: issue = journal.journalized.reload Chris@1296: redmine_headers 'Project' => issue.project.identifier, Chris@1296: 'Issue-Id' => issue.id, Chris@1296: 'Issue-Author' => issue.author.login Chris@1296: redmine_headers 'Issue-Assignee' => issue.assigned_to.login if issue.assigned_to Chris@1296: message_id journal Chris@1296: references issue Chris@1296: @author = journal.user Chris@1296: recipients = journal.recipients Chris@1296: # Watchers in cc Chris@1296: cc = journal.watcher_recipients - recipients Chris@1296: s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] " Chris@1296: s << "(#{issue.status.name}) " if journal.new_value_for('status_id') Chris@1296: s << issue.subject Chris@1296: @issue = issue Chris@1296: @journal = journal Chris@1296: @issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}") Chris@1296: mail :to => recipients, Chris@1296: :cc => cc, Chris@1296: :subject => s Chris@1296: end Chris@1296: Chris@1296: def reminder(user, issues, days) Chris@1296: set_language_if_valid user.language Chris@1296: @issues = issues Chris@1296: @days = days Chris@1296: @issues_url = url_for(:controller => 'issues', :action => 'index', Chris@1296: :set_filter => 1, :assigned_to_id => user.id, Chris@1296: :sort => 'due_date:asc') Chris@1296: mail :to => user.mail, Chris@1296: :subject => l(:mail_subject_reminder, :count => issues.size, :days => days) Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email users belonging to the added document's project. Chris@1296: # Chris@1296: # Example: Chris@1296: # document_added(document) => Mail::Message object Chris@1296: # Mailer.document_added(document).deliver => sends an email to the document's project recipients Chris@1296: def document_added(document) Chris@1296: redmine_headers 'Project' => document.project.identifier Chris@1296: @author = User.current Chris@1296: @document = document Chris@1296: @document_url = url_for(:controller => 'documents', :action => 'show', :id => document) Chris@1296: mail :to => document.recipients, Chris@1296: :subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email recipients of a project when an attachements are added. Chris@1296: # Chris@1296: # Example: Chris@1296: # attachments_added(attachments) => Mail::Message object Chris@1296: # Mailer.attachments_added(attachments).deliver => sends an email to the project's recipients Chris@1296: def attachments_added(attachments) Chris@1296: container = attachments.first.container Chris@1296: added_to = '' Chris@1296: added_to_url = '' Chris@1296: @author = attachments.first.author Chris@1296: case container.class.name Chris@1296: when 'Project' Chris@1296: added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container) Chris@1296: added_to = "#{l(:label_project)}: #{container}" Chris@1296: recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} Chris@1296: when 'Version' Chris@1296: added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project) Chris@1296: added_to = "#{l(:label_version)}: #{container.name}" Chris@1296: recipients = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}.collect {|u| u.mail} Chris@1296: when 'Document' Chris@1296: added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id) Chris@1296: added_to = "#{l(:label_document)}: #{container.title}" Chris@1296: recipients = container.recipients Chris@1296: end Chris@1296: redmine_headers 'Project' => container.project.identifier Chris@1296: @attachments = attachments Chris@1296: @added_to = added_to Chris@1296: @added_to_url = added_to_url Chris@1296: mail :to => recipients, Chris@1296: :subject => "[#{container.project.name}] #{l(:label_attachment_new)}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email recipients of a news' project when a news item is added. Chris@1296: # Chris@1296: # Example: Chris@1296: # news_added(news) => Mail::Message object Chris@1296: # Mailer.news_added(news).deliver => sends an email to the news' project recipients Chris@1296: def news_added(news) Chris@1296: redmine_headers 'Project' => news.project.identifier Chris@1296: @author = news.author Chris@1296: message_id news Chris@1296: @news = news Chris@1296: @news_url = url_for(:controller => 'news', :action => 'show', :id => news) Chris@1296: mail :to => news.recipients, Chris@1296: :subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email recipients of a news' project when a news comment is added. Chris@1296: # Chris@1296: # Example: Chris@1296: # news_comment_added(comment) => Mail::Message object Chris@1296: # Mailer.news_comment_added(comment) => sends an email to the news' project recipients Chris@1296: def news_comment_added(comment) Chris@1296: news = comment.commented Chris@1296: redmine_headers 'Project' => news.project.identifier Chris@1296: @author = comment.author Chris@1296: message_id comment Chris@1296: @news = news Chris@1296: @comment = comment Chris@1296: @news_url = url_for(:controller => 'news', :action => 'show', :id => news) Chris@1296: mail :to => news.recipients, Chris@1296: :cc => news.watcher_recipients, Chris@1296: :subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email the recipients of the specified message that was posted. Chris@1296: # Chris@1296: # Example: Chris@1296: # message_posted(message) => Mail::Message object Chris@1296: # Mailer.message_posted(message).deliver => sends an email to the recipients Chris@1296: def message_posted(message) Chris@1296: redmine_headers 'Project' => message.project.identifier, Chris@1296: 'Topic-Id' => (message.parent_id || message.id) Chris@1296: @author = message.author Chris@1296: message_id message Chris@1296: references message.parent unless message.parent.nil? Chris@1296: recipients = message.recipients Chris@1296: cc = ((message.root.watcher_recipients + message.board.watcher_recipients).uniq - recipients) Chris@1296: @message = message Chris@1296: @message_url = url_for(message.event_url) Chris@1296: mail :to => recipients, Chris@1296: :cc => cc, Chris@1296: :subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was added. Chris@1296: # Chris@1296: # Example: Chris@1296: # wiki_content_added(wiki_content) => Mail::Message object Chris@1296: # Mailer.wiki_content_added(wiki_content).deliver => sends an email to the project's recipients Chris@1296: def wiki_content_added(wiki_content) Chris@1296: redmine_headers 'Project' => wiki_content.project.identifier, Chris@1296: 'Wiki-Page-Id' => wiki_content.page.id Chris@1296: @author = wiki_content.author Chris@1296: message_id wiki_content Chris@1296: recipients = wiki_content.recipients Chris@1296: cc = wiki_content.page.wiki.watcher_recipients - recipients Chris@1296: @wiki_content = wiki_content Chris@1296: @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', Chris@1296: :project_id => wiki_content.project, Chris@1296: :id => wiki_content.page.title) Chris@1296: mail :to => recipients, Chris@1296: :cc => cc, Chris@1296: :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email the recipients of a project of the specified wiki content was updated. Chris@1296: # Chris@1296: # Example: Chris@1296: # wiki_content_updated(wiki_content) => Mail::Message object Chris@1296: # Mailer.wiki_content_updated(wiki_content).deliver => sends an email to the project's recipients Chris@1296: def wiki_content_updated(wiki_content) Chris@1296: redmine_headers 'Project' => wiki_content.project.identifier, Chris@1296: 'Wiki-Page-Id' => wiki_content.page.id Chris@1296: @author = wiki_content.author Chris@1296: message_id wiki_content Chris@1296: recipients = wiki_content.recipients Chris@1296: cc = wiki_content.page.wiki.watcher_recipients + wiki_content.page.watcher_recipients - recipients Chris@1296: @wiki_content = wiki_content Chris@1296: @wiki_content_url = url_for(:controller => 'wiki', :action => 'show', Chris@1296: :project_id => wiki_content.project, Chris@1296: :id => wiki_content.page.title) Chris@1296: @wiki_diff_url = url_for(:controller => 'wiki', :action => 'diff', Chris@1296: :project_id => wiki_content.project, :id => wiki_content.page.title, Chris@1296: :version => wiki_content.version) Chris@1296: mail :to => recipients, Chris@1296: :cc => cc, Chris@1296: :subject => "[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}" Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email the specified user their account information. Chris@1296: # Chris@1296: # Example: Chris@1296: # account_information(user, password) => Mail::Message object Chris@1296: # Mailer.account_information(user, password).deliver => sends account information to the user Chris@1296: def account_information(user, password) Chris@1296: set_language_if_valid user.language Chris@1296: @user = user Chris@1296: @password = password Chris@1296: @login_url = url_for(:controller => 'account', :action => 'login') Chris@1296: mail :to => user.mail, Chris@1296: :subject => l(:mail_subject_register, Setting.app_title) Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email all active administrators of an account activation request. Chris@1296: # Chris@1296: # Example: Chris@1296: # account_activation_request(user) => Mail::Message object Chris@1296: # Mailer.account_activation_request(user).deliver => sends an email to all active administrators Chris@1296: def account_activation_request(user) Chris@1296: # Send the email to all active administrators Chris@1296: recipients = User.active.find(:all, :conditions => {:admin => true}).collect { |u| u.mail }.compact Chris@1296: @user = user Chris@1296: @url = url_for(:controller => 'users', :action => 'index', Chris@1296: :status => User::STATUS_REGISTERED, Chris@1296: :sort_key => 'created_on', :sort_order => 'desc') Chris@1296: mail :to => recipients, Chris@1296: :subject => l(:mail_subject_account_activation_request, Setting.app_title) Chris@1296: end Chris@1296: Chris@1296: # Builds a Mail::Message object used to email the specified user that their account was activated by an administrator. Chris@1296: # Chris@1296: # Example: Chris@1296: # account_activated(user) => Mail::Message object Chris@1296: # Mailer.account_activated(user).deliver => sends an email to the registered user Chris@1296: def account_activated(user) Chris@1296: set_language_if_valid user.language Chris@1296: @user = user Chris@1296: @login_url = url_for(:controller => 'account', :action => 'login') Chris@1296: mail :to => user.mail, Chris@1296: :subject => l(:mail_subject_register, Setting.app_title) Chris@1296: end Chris@1296: Chris@1296: def lost_password(token) Chris@1296: set_language_if_valid(token.user.language) Chris@1296: @token = token Chris@1296: @url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value) Chris@1296: mail :to => token.user.mail, Chris@1296: :subject => l(:mail_subject_lost_password, Setting.app_title) Chris@1296: end Chris@1296: Chris@1296: def register(token) Chris@1296: set_language_if_valid(token.user.language) Chris@1296: @token = token Chris@1296: @url = url_for(:controller => 'account', :action => 'activate', :token => token.value) Chris@1296: mail :to => token.user.mail, Chris@1296: :subject => l(:mail_subject_register, Setting.app_title) Chris@1296: end Chris@1296: Chris@1296: def test_email(user) Chris@1296: set_language_if_valid(user.language) Chris@1296: @url = url_for(:controller => 'welcome') Chris@1296: mail :to => user.mail, Chris@1296: :subject => 'Redmine test' Chris@1296: end Chris@1296: Chris@1296: # Overrides default deliver! method to prevent from sending an email Chris@1296: # with no recipient, cc or bcc Chris@1296: def deliver!(mail = @mail) Chris@1296: set_language_if_valid @initial_language Chris@1296: return false if (recipients.nil? || recipients.empty?) && Chris@1296: (cc.nil? || cc.empty?) && Chris@1296: (bcc.nil? || bcc.empty?) Chris@1296: Chris@1296: Chris@1296: # Log errors when raise_delivery_errors is set to false, Rails does not Chris@1296: raise_errors = self.class.raise_delivery_errors Chris@1296: self.class.raise_delivery_errors = true Chris@1296: begin Chris@1296: return super(mail) Chris@1296: rescue Exception => e Chris@1296: if raise_errors Chris@1296: raise e Chris@1296: elsif mylogger Chris@1296: mylogger.error "The following error occured while sending email notification: \"#{e.message}\". Check your configuration in config/configuration.yml." Chris@1296: end Chris@1296: ensure Chris@1296: self.class.raise_delivery_errors = raise_errors Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Sends reminders to issue assignees Chris@1296: # Available options: Chris@1296: # * :days => how many days in the future to remind about (defaults to 7) Chris@1296: # * :tracker => id of tracker for filtering issues (defaults to all trackers) Chris@1296: # * :project => id or identifier of project to process (defaults to all projects) Chris@1296: # * :users => array of user/group ids who should be reminded Chris@1296: def self.reminders(options={}) Chris@1296: days = options[:days] || 7 Chris@1296: project = options[:project] ? Project.find(options[:project]) : nil Chris@1296: tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil Chris@1296: user_ids = options[:users] Chris@1296: Chris@1296: scope = Issue.open.where("#{Issue.table_name}.assigned_to_id IS NOT NULL" + Chris@1296: " AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" + Chris@1296: " AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date Chris@1296: ) Chris@1296: scope = scope.where(:assigned_to_id => user_ids) if user_ids.present? Chris@1296: scope = scope.where(:project_id => project.id) if project Chris@1296: scope = scope.where(:tracker_id => tracker.id) if tracker Chris@1296: Chris@1296: issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).all.group_by(&:assigned_to) Chris@1296: issues_by_assignee.keys.each do |assignee| Chris@1296: if assignee.is_a?(Group) Chris@1296: assignee.users.each do |user| Chris@1296: issues_by_assignee[user] ||= [] Chris@1296: issues_by_assignee[user] += issues_by_assignee[assignee] Chris@1296: end Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: issues_by_assignee.each do |assignee, issues| Chris@1296: reminder(assignee, issues, days).deliver if assignee.is_a?(User) && assignee.active? Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: # Activates/desactivates email deliveries during +block+ Chris@1296: def self.with_deliveries(enabled = true, &block) Chris@1296: was_enabled = ActionMailer::Base.perform_deliveries Chris@1296: ActionMailer::Base.perform_deliveries = !!enabled Chris@1296: yield Chris@1296: ensure Chris@1296: ActionMailer::Base.perform_deliveries = was_enabled Chris@1296: end Chris@1296: Chris@1296: # Sends emails synchronously in the given block Chris@1296: def self.with_synched_deliveries(&block) Chris@1296: saved_method = ActionMailer::Base.delivery_method Chris@1296: if m = saved_method.to_s.match(%r{^async_(.+)$}) Chris@1296: synched_method = m[1] Chris@1296: ActionMailer::Base.delivery_method = synched_method.to_sym Chris@1296: ActionMailer::Base.send "#{synched_method}_settings=", ActionMailer::Base.send("async_#{synched_method}_settings") Chris@1296: end Chris@1296: yield Chris@1296: ensure Chris@1296: ActionMailer::Base.delivery_method = saved_method Chris@1296: end Chris@1296: Chris@1296: def mail(headers={}) Chris@1296: headers.merge! 'X-Mailer' => 'Redmine', Chris@1296: 'X-Redmine-Host' => Setting.host_name, Chris@1296: 'X-Redmine-Site' => Setting.app_title, Chris@1296: 'X-Auto-Response-Suppress' => 'OOF', Chris@1296: 'Auto-Submitted' => 'auto-generated', Chris@1296: 'From' => Setting.mail_from, Chris@1296: 'List-Id' => "<#{Setting.mail_from.to_s.gsub('@', '.')}>" Chris@1296: Chris@1296: # Removes the author from the recipients and cc Chris@1296: # if he doesn't want to receive notifications about what he does Chris@1296: if @author && @author.logged? && @author.pref[:no_self_notified] Chris@1296: headers[:to].delete(@author.mail) if headers[:to].is_a?(Array) Chris@1296: headers[:cc].delete(@author.mail) if headers[:cc].is_a?(Array) Chris@1296: end Chris@1296: Chris@1296: if @author && @author.logged? Chris@1296: redmine_headers 'Sender' => @author.login Chris@1296: end Chris@1296: Chris@1296: # Blind carbon copy recipients Chris@1296: if Setting.bcc_recipients? Chris@1296: headers[:bcc] = [headers[:to], headers[:cc]].flatten.uniq.reject(&:blank?) Chris@1296: headers[:to] = nil Chris@1296: headers[:cc] = nil Chris@1296: end Chris@1296: Chris@1296: if @message_id_object Chris@1296: headers[:message_id] = "<#{self.class.message_id_for(@message_id_object)}>" Chris@1296: end Chris@1296: if @references_objects Chris@1296: headers[:references] = @references_objects.collect {|o| "<#{self.class.message_id_for(o)}>"}.join(' ') Chris@1296: end Chris@1296: Chris@1296: super headers do |format| Chris@1296: format.text Chris@1296: format.html unless Setting.plain_text_mail? Chris@1296: end Chris@1296: Chris@1296: set_language_if_valid @initial_language Chris@1296: end Chris@1296: Chris@1296: def initialize(*args) Chris@1296: @initial_language = current_language Chris@1296: set_language_if_valid Setting.default_language Chris@1296: super Chris@1296: end Chris@1296: Chris@1296: def self.deliver_mail(mail) Chris@1296: return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank? Chris@1296: super Chris@1296: end Chris@1296: Chris@1296: def self.method_missing(method, *args, &block) Chris@1296: if m = method.to_s.match(%r{^deliver_(.+)$}) Chris@1296: ActiveSupport::Deprecation.warn "Mailer.deliver_#{m[1]}(*args) is deprecated. Use Mailer.#{m[1]}(*args).deliver instead." Chris@1296: send(m[1], *args).deliver Chris@1296: else Chris@1296: super Chris@1296: end Chris@1296: end Chris@1296: Chris@1296: private Chris@1296: Chris@1296: # Appends a Redmine header field (name is prepended with 'X-Redmine-') Chris@1296: def redmine_headers(h) Chris@1296: h.each { |k,v| headers["X-Redmine-#{k}"] = v.to_s } Chris@1296: end Chris@1296: Chris@1296: # Returns a predictable Message-Id for the given object Chris@1296: def self.message_id_for(object) Chris@1296: # id + timestamp should reduce the odds of a collision Chris@1296: # as far as we don't send multiple emails for the same object Chris@1296: timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on) Chris@1296: hash = "redmine.#{object.class.name.demodulize.underscore}-#{object.id}.#{timestamp.strftime("%Y%m%d%H%M%S")}" Chris@1296: host = Setting.mail_from.to_s.gsub(%r{^.*@}, '') Chris@1296: host = "#{::Socket.gethostname}.redmine" if host.empty? Chris@1296: "#{hash}@#{host}" Chris@1296: end Chris@1296: Chris@1296: def message_id(object) Chris@1296: @message_id_object = object Chris@1296: end Chris@1296: Chris@1296: def references(object) Chris@1296: @references_objects ||= [] Chris@1296: @references_objects << object Chris@1296: end Chris@1296: Chris@1296: def mylogger Chris@1296: Rails.logger Chris@1296: end Chris@1296: end