2019-03-17 14:36:03 +01:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 02:32:57 +01:00
|
|
|
|
2011-03-04 15:32:58 +01:00
|
|
|
# Redmine - project management software
|
2022-01-02 06:29:10 +01:00
|
|
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
2007-03-12 18:59:02 +01:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2008-11-07 16:37:17 +01:00
|
|
|
#
|
2007-03-12 18:59:02 +01:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2008-11-07 16:37:17 +01:00
|
|
|
#
|
2007-03-12 18:59:02 +01:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
2015-08-31 09:38:29 +02:00
|
|
|
require 'roadie'
|
|
|
|
|
2007-03-12 18:59:02 +01:00
|
|
|
class Mailer < ActionMailer::Base
|
2009-09-13 19:14:35 +02:00
|
|
|
layout 'mailer'
|
2008-02-21 20:30:27 +01:00
|
|
|
helper :application
|
|
|
|
helper :issues
|
|
|
|
helper :custom_fields
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2009-02-21 12:04:50 +01:00
|
|
|
include Redmine::I18n
|
2015-08-31 09:38:29 +02:00
|
|
|
include Roadie::Rails::Automatic
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Overrides ActionMailer::Base#process in order to set the recipient as the current user
|
2018-11-06 03:44:04 +01:00
|
|
|
# and his language as the default locale.
|
2018-10-10 19:13:09 +02:00
|
|
|
# The first argument of all actions of this Mailer must be a User (the recipient),
|
|
|
|
# otherwise an ArgumentError is raised.
|
2018-10-06 15:08:52 +02:00
|
|
|
def process(action, *args)
|
2018-10-10 19:13:09 +02:00
|
|
|
user = args.first
|
2018-10-06 15:08:52 +02:00
|
|
|
raise ArgumentError, "First argument has to be a user, was #{user.inspect}" unless user.is_a?(User)
|
|
|
|
|
|
|
|
initial_user = User.current
|
|
|
|
initial_language = ::I18n.locale
|
|
|
|
begin
|
|
|
|
User.current = user
|
|
|
|
|
|
|
|
lang = find_language(user.language) if user.logged?
|
|
|
|
lang ||= Setting.default_language
|
|
|
|
set_language_if_valid(lang)
|
|
|
|
|
|
|
|
super(action, *args)
|
|
|
|
ensure
|
|
|
|
User.current = initial_user
|
|
|
|
::I18n.locale = initial_language
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Default URL options for generating URLs in emails based on host_name and protocol
|
|
|
|
# defined in application settings.
|
2009-02-25 08:25:01 +01:00
|
|
|
def self.default_url_options
|
2015-03-14 09:52:48 +01:00
|
|
|
options = {:protocol => Setting.protocol}
|
|
|
|
if Setting.host_name.to_s =~ /\A(https?\:\/\/)?(.+?)(\:(\d+))?(\/.+)?\z/i
|
|
|
|
host, port, prefix = $2, $4, $5
|
2020-07-21 15:41:16 +02:00
|
|
|
options.merge!(
|
|
|
|
{
|
|
|
|
:host => host, :port => port, :script_name => prefix
|
|
|
|
}
|
|
|
|
)
|
2015-03-14 09:52:48 +01:00
|
|
|
else
|
|
|
|
options[:host] = Setting.host_name
|
|
|
|
end
|
|
|
|
options
|
2009-02-25 08:25:01 +01:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail for notifying user about a new issue
|
|
|
|
def issue_add(user, issue)
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => issue.project.identifier,
|
2019-09-12 23:52:21 +02:00
|
|
|
'Issue-Tracker' => issue.tracker.name,
|
2008-03-16 17:52:49 +01:00
|
|
|
'Issue-Id' => issue.id,
|
2021-05-18 07:18:23 +02:00
|
|
|
'Issue-Author' => issue.author.login,
|
|
|
|
'Issue-Assignee' => assignee_for_header(issue)
|
2009-01-18 16:16:31 +01:00
|
|
|
message_id issue
|
2013-07-13 11:20:11 +02:00
|
|
|
references issue
|
2012-02-05 13:57:19 +01:00
|
|
|
@author = issue.author
|
2012-04-25 19:17:49 +02:00
|
|
|
@issue = issue
|
2018-10-10 19:13:09 +02:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue)
|
2019-02-28 09:23:17 +01:00
|
|
|
subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]"
|
2019-03-17 14:36:03 +01:00
|
|
|
subject += " (#{issue.status.name})" if Setting.show_status_changes_in_mail_subject?
|
|
|
|
subject += " #{issue.subject}"
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2019-02-28 09:23:17 +01:00
|
|
|
:subject => subject
|
2007-03-12 18:59:02 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about a new issue.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_issue_add(issue)
|
2013-07-13 11:20:11 +02:00
|
|
|
def self.deliver_issue_add(issue)
|
2022-02-23 22:16:18 +01:00
|
|
|
users = issue.notified_users | issue.notified_watchers | issue.notified_mentions
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
2018-10-10 20:45:34 +02:00
|
|
|
issue_add(user, issue).deliver_later
|
2018-10-10 19:13:09 +02:00
|
|
|
end
|
2013-07-13 11:20:11 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail for notifying user about an issue update
|
|
|
|
def issue_edit(user, journal)
|
2013-07-13 11:20:11 +02:00
|
|
|
issue = journal.journalized
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => issue.project.identifier,
|
2019-09-12 23:52:21 +02:00
|
|
|
'Issue-Tracker' => issue.tracker.name,
|
2008-03-16 17:52:49 +01:00
|
|
|
'Issue-Id' => issue.id,
|
2021-05-18 07:18:23 +02:00
|
|
|
'Issue-Author' => issue.author.login,
|
|
|
|
'Issue-Assignee' => assignee_for_header(issue)
|
2009-01-18 16:16:31 +01:00
|
|
|
message_id journal
|
|
|
|
references issue
|
2009-01-03 17:03:12 +01:00
|
|
|
@author = journal.user
|
2008-01-20 11:42:16 +01:00
|
|
|
s = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}] "
|
2019-03-17 14:36:03 +01:00
|
|
|
s += "(#{issue.status.name}) " if journal.new_value_for('status_id') && Setting.show_status_changes_in_mail_subject?
|
|
|
|
s += issue.subject
|
2012-04-25 19:17:49 +02:00
|
|
|
@issue = issue
|
2018-10-10 19:13:09 +02:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@journal = journal
|
2018-10-06 15:08:52 +02:00
|
|
|
@journal_details = journal.visible_details
|
2012-04-25 19:17:49 +02:00
|
|
|
@issue_url = url_for(:controller => 'issues', :action => 'show', :id => issue, :anchor => "change-#{journal.id}")
|
2018-10-06 15:08:52 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => s
|
2007-03-12 18:59:02 +01:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about an issue update.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_issue_edit(journal)
|
|
|
|
def self.deliver_issue_edit(journal)
|
2022-02-23 22:16:18 +01:00
|
|
|
users = journal.notified_users | journal.notified_watchers | journal.notified_mentions | journal.journalized.notified_mentions
|
2018-10-06 15:08:52 +02:00
|
|
|
users.select! do |user|
|
|
|
|
journal.notes? || journal.visible_details(user).any?
|
|
|
|
end
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
2018-10-10 20:45:34 +02:00
|
|
|
issue_edit(user, journal).deliver_later
|
2018-10-10 19:13:09 +02:00
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about a new document.
|
|
|
|
def document_added(user, document, author)
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => document.project.identifier
|
2018-10-06 15:08:52 +02:00
|
|
|
@author = author
|
2012-04-25 19:17:49 +02:00
|
|
|
@document = document
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@document_url = url_for(:controller => 'documents', :action => 'show', :id => document)
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => "[#{document.project.name}] #{l(:label_document_new)}: #{document.title}"
|
2007-03-12 18:59:02 +01:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users that document was created by author
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_document_added(document, author)
|
|
|
|
def self.deliver_document_added(document, author)
|
2018-10-06 15:08:52 +02:00
|
|
|
users = document.notified_users
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
document_added(user, document, author).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about new attachements.
|
|
|
|
def attachments_added(user, attachments)
|
2007-03-12 18:59:02 +01:00
|
|
|
container = attachments.first.container
|
2007-08-25 22:09:33 +02:00
|
|
|
added_to = ''
|
2007-11-04 11:43:39 +01:00
|
|
|
added_to_url = ''
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = attachments.first.author
|
2007-08-25 22:09:33 +02:00
|
|
|
case container.class.name
|
2008-12-09 19:00:27 +01:00
|
|
|
when 'Project'
|
2011-03-27 18:38:08 +02:00
|
|
|
added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container)
|
2008-12-09 19:00:27 +01:00
|
|
|
added_to = "#{l(:label_project)}: #{container}"
|
2007-03-12 18:59:02 +01:00
|
|
|
when 'Version'
|
2011-03-27 18:38:08 +02:00
|
|
|
added_to_url = url_for(:controller => 'files', :action => 'index', :project_id => container.project)
|
2007-03-12 18:59:02 +01:00
|
|
|
added_to = "#{l(:label_version)}: #{container.name}"
|
|
|
|
when 'Document'
|
2007-11-04 11:43:39 +01:00
|
|
|
added_to_url = url_for(:controller => 'documents', :action => 'show', :id => container.id)
|
2007-03-12 18:59:02 +01:00
|
|
|
added_to = "#{l(:label_document)}: #{container.title}"
|
|
|
|
end
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => container.project.identifier
|
2012-04-25 19:17:49 +02:00
|
|
|
@attachments = attachments
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@added_to = added_to
|
|
|
|
@added_to_url = added_to_url
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => "[#{container.project.name}] #{l(:label_attachment_new)}"
|
2007-03-12 18:59:02 +01:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about new attachments
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_attachments_added(attachments)
|
|
|
|
def self.deliver_attachments_added(attachments)
|
2018-10-06 15:08:52 +02:00
|
|
|
container = attachments.first.container
|
|
|
|
case container.class.name
|
|
|
|
when 'Project', 'Version'
|
|
|
|
users = container.project.notified_users.select {|user| user.allowed_to?(:view_files, container.project)}
|
|
|
|
when 'Document'
|
|
|
|
users = container.notified_users
|
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
attachments_added(user, attachments).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about a new news.
|
|
|
|
def news_added(user, news)
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => news.project.identifier
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = news.author
|
2009-01-18 16:16:31 +01:00
|
|
|
message_id news
|
2013-07-13 11:20:11 +02:00
|
|
|
references news
|
2012-04-25 19:17:49 +02:00
|
|
|
@news = news
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@news_url = url_for(:controller => 'news', :action => 'show', :id => news)
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => "[#{news.project.name}] #{l(:label_news)}: #{news.title}"
|
2007-09-14 17:30:46 +02:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about new news
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_news_added(news)
|
|
|
|
def self.deliver_news_added(news)
|
2018-10-06 15:08:52 +02:00
|
|
|
users = news.notified_users | news.notified_watchers_for_added_news
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
news_added(user, news).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about a new news comment.
|
|
|
|
def news_comment_added(user, comment)
|
2011-03-04 15:32:58 +01:00
|
|
|
news = comment.commented
|
|
|
|
redmine_headers 'Project' => news.project.identifier
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = comment.author
|
2011-03-04 15:32:58 +01:00
|
|
|
message_id comment
|
2013-07-13 11:20:11 +02:00
|
|
|
references news
|
2012-04-25 19:17:49 +02:00
|
|
|
@news = news
|
|
|
|
@comment = comment
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@news_url = url_for(:controller => 'news', :action => 'show', :id => news)
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => "Re: [#{news.project.name}] #{l(:label_news)}: #{news.title}"
|
2011-03-04 15:32:58 +01:00
|
|
|
end
|
2007-11-04 11:43:39 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about a new comment on a news
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_news_comment_added(comment)
|
|
|
|
def self.deliver_news_comment_added(comment)
|
2018-10-06 15:08:52 +02:00
|
|
|
news = comment.commented
|
|
|
|
users = news.notified_users | news.notified_watchers
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
news_comment_added(user, comment).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about a new message.
|
|
|
|
def message_posted(user, message)
|
2008-03-16 17:52:49 +01:00
|
|
|
redmine_headers 'Project' => message.project.identifier,
|
|
|
|
'Topic-Id' => (message.parent_id || message.id)
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = message.author
|
2009-01-18 16:16:31 +01:00
|
|
|
message_id message
|
2013-07-13 11:20:11 +02:00
|
|
|
references message.root
|
2012-04-25 19:17:49 +02:00
|
|
|
@message = message
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@message_url = url_for(message.event_url)
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => "[#{message.board.project.name} - #{message.board.name} - msg#{message.root.id}] #{message.subject}"
|
2007-11-04 11:43:39 +01:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about a new forum message.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_message_posted(message)
|
|
|
|
def self.deliver_message_posted(message)
|
2018-10-06 15:08:52 +02:00
|
|
|
users = message.notified_users
|
|
|
|
users |= message.root.notified_watchers
|
|
|
|
users |= message.board.notified_watchers
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
message_posted(user, message).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about a new wiki content.
|
|
|
|
def wiki_content_added(user, wiki_content)
|
2009-05-17 11:55:13 +02:00
|
|
|
redmine_headers 'Project' => wiki_content.project.identifier,
|
|
|
|
'Wiki-Page-Id' => wiki_content.page.id
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = wiki_content.author
|
2009-05-17 11:55:13 +02:00
|
|
|
message_id wiki_content
|
2012-04-25 19:17:49 +02:00
|
|
|
@wiki_content = wiki_content
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@wiki_content_url = url_for(:controller => 'wiki', :action => 'show',
|
2011-09-23 08:53:57 +02:00
|
|
|
:project_id => wiki_content.project,
|
|
|
|
:id => wiki_content.page.title)
|
2020-12-11 15:17:42 +01:00
|
|
|
mail(
|
|
|
|
:to => user,
|
|
|
|
:subject =>
|
|
|
|
"[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_added, :id => wiki_content.page.pretty_title)}"
|
|
|
|
)
|
2009-05-17 11:55:13 +02:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about a new wiki content (wiki page added).
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_wiki_content_added(wiki_content)
|
|
|
|
def self.deliver_wiki_content_added(wiki_content)
|
2022-02-23 22:16:18 +01:00
|
|
|
users = wiki_content.notified_users | wiki_content.page.wiki.notified_watchers | wiki_content.notified_mentions
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
wiki_content_added(user, wiki_content).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about an update of the specified wiki content.
|
|
|
|
def wiki_content_updated(user, wiki_content)
|
2009-05-17 11:55:13 +02:00
|
|
|
redmine_headers 'Project' => wiki_content.project.identifier,
|
|
|
|
'Wiki-Page-Id' => wiki_content.page.id
|
2012-03-12 19:34:12 +01:00
|
|
|
@author = wiki_content.author
|
2009-05-17 11:55:13 +02:00
|
|
|
message_id wiki_content
|
2012-04-25 19:17:49 +02:00
|
|
|
@wiki_content = wiki_content
|
2019-03-04 15:36:54 +01:00
|
|
|
@user = user
|
2020-12-11 15:17:42 +01:00
|
|
|
@wiki_content_url =
|
|
|
|
url_for(:controller => 'wiki', :action => 'show',
|
|
|
|
:project_id => wiki_content.project,
|
|
|
|
:id => wiki_content.page.title)
|
|
|
|
@wiki_diff_url =
|
|
|
|
url_for(:controller => 'wiki', :action => 'diff',
|
|
|
|
:project_id => wiki_content.project, :id => wiki_content.page.title,
|
|
|
|
:version => wiki_content.version)
|
|
|
|
mail(
|
|
|
|
:to => user,
|
|
|
|
:subject =>
|
|
|
|
"[#{wiki_content.project.name}] #{l(:mail_subject_wiki_content_updated, :id => wiki_content.page.pretty_title)}"
|
|
|
|
)
|
2009-05-17 11:55:13 +02:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies users about the update of the specified wiki content
|
2009-03-21 01:39:53 +01:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_wiki_content_updated(wiki_content)
|
|
|
|
def self.deliver_wiki_content_updated(wiki_content)
|
2018-10-06 15:08:52 +02:00
|
|
|
users = wiki_content.notified_users
|
|
|
|
users |= wiki_content.page.notified_watchers
|
|
|
|
users |= wiki_content.page.wiki.notified_watchers
|
2022-02-23 22:16:18 +01:00
|
|
|
users |= wiki_content.notified_mentions
|
2018-10-06 15:08:52 +02:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
wiki_content_updated(user, wiki_content).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about his account information.
|
|
|
|
def account_information(user, password)
|
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@password = password
|
|
|
|
@login_url = url_for(:controller => 'account', :action => 'login')
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user.mail,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => l(:mail_subject_register, Setting.app_title)
|
2007-11-04 11:43:39 +01:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies user about his account information.
|
|
|
|
def self.deliver_account_information(user, password)
|
|
|
|
account_information(user, password).deliver_later
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user about an account activation request.
|
|
|
|
def account_activation_request(user, new_user)
|
|
|
|
@new_user = new_user
|
2012-04-25 19:17:49 +02:00
|
|
|
@url = url_for(:controller => 'users', :action => 'index',
|
2011-09-23 08:56:24 +02:00
|
|
|
:status => User::STATUS_REGISTERED,
|
|
|
|
:sort_key => 'created_on', :sort_order => 'desc')
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => l(:mail_subject_account_activation_request, Setting.app_title)
|
2007-11-18 18:46:55 +01:00
|
|
|
end
|
2007-11-04 11:43:39 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies admin users that an account activation request needs
|
|
|
|
# their approval.
|
2009-03-21 01:39:53 +01:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_account_activation_request(new_user)
|
|
|
|
def self.deliver_account_activation_request(new_user)
|
2018-10-06 15:08:52 +02:00
|
|
|
# Send the email to all active administrators
|
|
|
|
users = User.active.where(:admin => true)
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
account_activation_request(user, new_user).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to notify user that his account was activated.
|
|
|
|
def account_activated(user)
|
|
|
|
@user = user
|
2012-04-25 19:17:49 +02:00
|
|
|
@login_url = url_for(:controller => 'account', :action => 'login')
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user.mail,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => l(:mail_subject_register, Setting.app_title)
|
2009-02-20 18:04:47 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies user that his account was activated.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_account_activated(user)
|
|
|
|
def self.deliver_account_activated(user)
|
|
|
|
account_activated(user).deliver_later
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail with the password recovery link.
|
|
|
|
def lost_password(user, token, recipient=nil)
|
|
|
|
recipient ||= user.mail
|
2012-04-25 19:17:49 +02:00
|
|
|
@token = token
|
|
|
|
@url = url_for(:controller => 'account', :action => 'lost_password', :token => token.value)
|
2015-01-17 15:51:29 +01:00
|
|
|
mail :to => recipient,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => l(:mail_subject_lost_password, Setting.app_title)
|
2008-11-07 16:37:17 +01:00
|
|
|
end
|
2007-03-12 18:59:02 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Sends an email to user with a password recovery link.
|
|
|
|
# The email will be sent to the email address specifiedby recipient if provided.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
2020-10-25 10:42:58 +01:00
|
|
|
# Mailer.deliver_lost_password(user, token)
|
|
|
|
# Mailer.deliver_lost_password(user, token, 'foo@example.net')
|
2018-10-10 19:13:09 +02:00
|
|
|
def self.deliver_lost_password(user, token, recipient=nil)
|
|
|
|
lost_password(user, token, recipient).deliver_later
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies user that his password was updated by sender.
|
|
|
|
#
|
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_password_updated(user, sender)
|
|
|
|
def self.deliver_password_updated(user, sender)
|
2016-05-08 11:07:37 +02:00
|
|
|
# Don't send a notification to the dummy email address when changing the password
|
|
|
|
# of the default admin account which is required after the first login
|
|
|
|
# TODO: maybe not the best way to handle this
|
|
|
|
return if user.admin? && user.login == 'admin' && user.mail == 'admin@example.net'
|
|
|
|
|
2019-11-08 01:45:52 +01:00
|
|
|
deliver_security_notification(
|
|
|
|
user,
|
2018-10-10 19:13:09 +02:00
|
|
|
sender,
|
2016-05-08 10:27:24 +02:00
|
|
|
message: :mail_body_password_updated,
|
2016-03-20 08:35:08 +01:00
|
|
|
title: :button_change_password,
|
|
|
|
url: {controller: 'my', action: 'password'}
|
2018-10-10 19:13:09 +02:00
|
|
|
)
|
2016-03-20 08:35:08 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Builds a mail to user with his account activation link.
|
|
|
|
def register(user, token)
|
2012-04-25 19:17:49 +02:00
|
|
|
@token = token
|
|
|
|
@url = url_for(:controller => 'account', :action => 'activate', :token => token.value)
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user.mail,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => l(:mail_subject_register, Setting.app_title)
|
2007-05-14 19:03:59 +02:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Sends an mail to user with his account activation link.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_register(user, token)
|
|
|
|
def self.deliver_register(user, token)
|
|
|
|
register(user, token).deliver_later
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Build a mail to user and the additional recipients given in
|
|
|
|
# options[:recipients] about a security related event made by sender.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# security_notification(user,
|
|
|
|
# sender,
|
2018-10-06 15:08:52 +02:00
|
|
|
# message: :mail_body_security_notification_add,
|
|
|
|
# field: :field_mail,
|
|
|
|
# value: address
|
|
|
|
# ) => Mail::Message object
|
2018-10-10 19:13:09 +02:00
|
|
|
def security_notification(user, sender, options={})
|
2018-10-06 15:08:52 +02:00
|
|
|
@sender = sender
|
|
|
|
redmine_headers 'Sender' => sender.login
|
2019-11-08 01:45:52 +01:00
|
|
|
@message =
|
|
|
|
l(options[:message],
|
|
|
|
field: (options[:field] && l(options[:field])),
|
|
|
|
value: options[:value])
|
2016-02-05 08:33:24 +01:00
|
|
|
@title = options[:title] && l(options[:title])
|
2018-10-10 19:13:09 +02:00
|
|
|
@remote_ip = options[:remote_ip] || @sender.remote_ip
|
2016-02-05 08:33:24 +01:00
|
|
|
@url = options[:url] && (options[:url].is_a?(Hash) ? url_for(options[:url]) : options[:url])
|
2018-04-07 09:49:43 +02:00
|
|
|
redmine_headers 'Url' => @url
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => [user, *options[:recipients]].uniq,
|
2016-03-20 08:49:15 +01:00
|
|
|
:subject => "[#{Setting.app_title}] #{l(:mail_subject_security_notification)}"
|
2016-02-05 08:33:24 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies the given users about a security related event made by sender.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
|
|
|
# You can specify additional recipients in options[:recipients]. These will be
|
|
|
|
# added to all generated mails for all given users. Usually, you'll want to
|
|
|
|
# give only a single user when setting the additional recipients.
|
|
|
|
#
|
|
|
|
# Example:
|
2018-10-10 19:13:09 +02:00
|
|
|
# Mailer.deliver_security_notification(users,
|
|
|
|
# sender,
|
2018-10-06 15:08:52 +02:00
|
|
|
# message: :mail_body_security_notification_add,
|
|
|
|
# field: :field_mail,
|
|
|
|
# value: address
|
2018-10-10 19:13:09 +02:00
|
|
|
# )
|
|
|
|
def self.deliver_security_notification(users, sender, options={})
|
|
|
|
# Symbols cannot be serialized:
|
|
|
|
# ActiveJob::SerializationError: Unsupported argument type: Symbol
|
2019-11-24 17:44:29 +01:00
|
|
|
options = options.transform_values {|v| v.is_a?(Symbol) ? v.to_s : v}
|
2018-10-10 19:13:09 +02:00
|
|
|
# sender's remote_ip would be lost on serialization/deserialization
|
|
|
|
# we have to pass it with options
|
|
|
|
options[:remote_ip] ||= sender.remote_ip
|
|
|
|
|
|
|
|
Array.wrap(users).each do |user|
|
|
|
|
security_notification(user, sender, options).deliver_later
|
|
|
|
end
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Build a mail to user about application settings changes made by sender.
|
|
|
|
def settings_updated(user, sender, changes, options={})
|
2018-10-06 15:08:52 +02:00
|
|
|
@sender = sender
|
|
|
|
redmine_headers 'Sender' => sender.login
|
2016-02-05 09:50:21 +01:00
|
|
|
@changes = changes
|
2018-10-10 19:13:09 +02:00
|
|
|
@remote_ip = options[:remote_ip] || @sender.remote_ip
|
2016-02-05 09:50:21 +01:00
|
|
|
@url = url_for(controller: 'settings', action: 'index')
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2016-03-20 08:49:15 +01:00
|
|
|
:subject => "[#{Setting.app_title}] #{l(:mail_subject_security_notification)}"
|
2016-02-05 09:50:21 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Notifies admins about application settings changes made by sender, where
|
|
|
|
# changes is an array of settings names.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_settings_updated(sender, [:login_required, :self_registration])
|
|
|
|
def self.deliver_settings_updated(sender, changes, options={})
|
2016-02-05 09:50:21 +01:00
|
|
|
return unless changes.present?
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Symbols cannot be serialized:
|
|
|
|
# ActiveJob::SerializationError: Unsupported argument type: Symbol
|
|
|
|
changes = changes.map(&:to_s)
|
|
|
|
# sender's remote_ip would be lost on serialization/deserialization
|
|
|
|
# we have to pass it with options
|
|
|
|
options[:remote_ip] ||= sender.remote_ip
|
|
|
|
|
2016-02-05 09:50:21 +01:00
|
|
|
users = User.active.where(admin: true).to_a
|
2018-10-10 19:13:09 +02:00
|
|
|
users.each do |user|
|
|
|
|
settings_updated(user, sender, changes, options).deliver_later
|
|
|
|
end
|
2016-02-05 09:50:21 +01:00
|
|
|
end
|
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Build a test email to user.
|
2018-10-06 18:07:45 +02:00
|
|
|
def test_email(user)
|
2012-04-25 19:17:49 +02:00
|
|
|
@url = url_for(:controller => 'welcome')
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
2012-04-25 19:17:49 +02:00
|
|
|
:subject => 'Redmine test'
|
2007-11-04 11:43:39 +01:00
|
|
|
end
|
2008-02-26 19:30:24 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Send a test email to user. Will raise error that may occur during delivery.
|
2018-10-06 15:08:52 +02:00
|
|
|
#
|
2018-10-10 19:13:09 +02:00
|
|
|
# Exemple:
|
|
|
|
# Mailer.deliver_test_email(user)
|
|
|
|
def self.deliver_test_email(user)
|
|
|
|
raise_delivery_errors_was = self.raise_delivery_errors
|
|
|
|
self.raise_delivery_errors = true
|
|
|
|
# Email must be delivered synchronously so we can catch errors
|
|
|
|
test_email(user).deliver_now
|
|
|
|
ensure
|
|
|
|
self.raise_delivery_errors = raise_delivery_errors_was
|
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a reminder mail to user about issues that are due in the next days.
|
|
|
|
def reminder(user, issues, days)
|
|
|
|
@issues = issues
|
|
|
|
@days = days
|
2020-03-07 09:20:26 +01:00
|
|
|
@open_issues_url = url_for(:controller => 'issues', :action => 'index',
|
2019-03-03 23:31:53 +01:00
|
|
|
:set_filter => 1, :assigned_to_id => 'me',
|
2018-10-10 19:13:09 +02:00
|
|
|
:sort => 'due_date:asc')
|
2020-03-07 09:20:26 +01:00
|
|
|
@reminder_issues_url = url_for(:controller => 'issues', :action => 'index',
|
|
|
|
:set_filter => 1, :sort => 'due_date:asc',
|
|
|
|
:f => ['status_id', 'assigned_to_id', "due_date"],
|
|
|
|
:op => {'status_id' => 'o', 'assigned_to_id' => '=', 'due_date' => '<t+'},
|
|
|
|
:v =>{'assigned_to_id' => ['me'], 'due_date' => [days]})
|
|
|
|
|
2019-04-18 17:32:20 +02:00
|
|
|
query = IssueQuery.new(:name => '_')
|
|
|
|
query.add_filter('assigned_to_id', '=', ['me'])
|
|
|
|
@open_issues_count = query.issue_count
|
2018-10-10 19:13:09 +02:00
|
|
|
mail :to => user,
|
|
|
|
:subject => l(:mail_subject_reminder, :count => issues.size, :days => days)
|
2018-10-06 15:08:52 +02:00
|
|
|
end
|
|
|
|
|
2008-05-25 19:31:50 +02:00
|
|
|
# Sends reminders to issue assignees
|
|
|
|
# Available options:
|
|
|
|
# * :days => how many days in the future to remind about (defaults to 7)
|
|
|
|
# * :tracker => id of tracker for filtering issues (defaults to all trackers)
|
|
|
|
# * :project => id or identifier of project to process (defaults to all projects)
|
2012-09-09 13:02:13 +02:00
|
|
|
# * :users => array of user/group ids who should be reminded
|
2015-02-07 08:56:58 +01:00
|
|
|
# * :version => name of target version for filtering issues (defaults to none)
|
2008-05-25 19:31:50 +02:00
|
|
|
def self.reminders(options={})
|
|
|
|
days = options[:days] || 7
|
|
|
|
project = options[:project] ? Project.find(options[:project]) : nil
|
|
|
|
tracker = options[:tracker] ? Tracker.find(options[:tracker]) : nil
|
2015-02-07 09:12:06 +01:00
|
|
|
target_version_id = options[:version] ? Version.named(options[:version]).pluck(:id) : nil
|
|
|
|
if options[:version] && target_version_id.blank?
|
2018-01-24 04:53:59 +01:00
|
|
|
raise ActiveRecord::RecordNotFound.new("Couldn't find Version named #{options[:version]}")
|
2015-02-07 09:12:06 +01:00
|
|
|
end
|
2019-11-22 15:55:04 +01:00
|
|
|
|
2010-09-21 01:17:51 +02:00
|
|
|
user_ids = options[:users]
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2020-12-11 15:17:42 +01:00
|
|
|
scope =
|
|
|
|
Issue.open.where(
|
|
|
|
"#{Issue.table_name}.assigned_to_id IS NOT NULL" \
|
|
|
|
" AND #{Project.table_name}.status = #{Project::STATUS_ACTIVE}" \
|
|
|
|
" AND #{Issue.table_name}.due_date <= ?", days.day.from_now.to_date
|
|
|
|
)
|
2012-09-09 13:34:35 +02:00
|
|
|
scope = scope.where(:assigned_to_id => user_ids) if user_ids.present?
|
|
|
|
scope = scope.where(:project_id => project.id) if project
|
2015-02-07 09:12:06 +01:00
|
|
|
scope = scope.where(:fixed_version_id => target_version_id) if target_version_id.present?
|
2012-09-09 13:34:35 +02:00
|
|
|
scope = scope.where(:tracker_id => tracker.id) if tracker
|
2014-01-27 02:09:09 +01:00
|
|
|
issues_by_assignee = scope.includes(:status, :assigned_to, :project, :tracker).
|
|
|
|
group_by(&:assigned_to)
|
2012-09-09 13:02:13 +02:00
|
|
|
issues_by_assignee.keys.each do |assignee|
|
|
|
|
if assignee.is_a?(Group)
|
|
|
|
assignee.users.each do |user|
|
|
|
|
issues_by_assignee[user] ||= []
|
|
|
|
issues_by_assignee[user] += issues_by_assignee[assignee]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-25 19:31:50 +02:00
|
|
|
issues_by_assignee.each do |assignee, issues|
|
2017-05-13 11:38:05 +02:00
|
|
|
if assignee.is_a?(User) && assignee.active? && issues.present?
|
|
|
|
visible_issues = issues.select {|i| i.visible?(assignee)}
|
2018-11-06 04:13:46 +01:00
|
|
|
visible_issues.sort!{|a, b| (a.due_date <=> b.due_date).nonzero? || (a.id <=> b.id)}
|
2018-10-10 20:45:34 +02:00
|
|
|
reminder(assignee, visible_issues, days).deliver_later if visible_issues.present?
|
2017-05-13 11:38:05 +02:00
|
|
|
end
|
2008-05-25 19:31:50 +02:00
|
|
|
end
|
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2010-03-21 12:17:29 +01:00
|
|
|
# Activates/desactivates email deliveries during +block+
|
|
|
|
def self.with_deliveries(enabled = true, &block)
|
|
|
|
was_enabled = ActionMailer::Base.perform_deliveries
|
|
|
|
ActionMailer::Base.perform_deliveries = !!enabled
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActionMailer::Base.perform_deliveries = was_enabled
|
|
|
|
end
|
2008-02-26 19:30:24 +01:00
|
|
|
|
2018-10-10 19:13:09 +02:00
|
|
|
# Execute the given block with inline sending of emails if the default Async
|
|
|
|
# queue is used for the mailer. See the Rails guide:
|
|
|
|
# Using the asynchronous queue from a Rake task will generally not work because
|
|
|
|
# Rake will likely end, causing the in-process thread pool to be deleted, before
|
|
|
|
# any/all of the .deliver_later emails are processed
|
2012-04-06 20:57:05 +02:00
|
|
|
def self.with_synched_deliveries(&block)
|
2021-04-01 16:41:03 +02:00
|
|
|
adapter = ActionMailer::MailDeliveryJob.queue_adapter
|
2022-01-12 16:13:47 +01:00
|
|
|
ActionMailer::MailDeliveryJob.queue_adapter = ActiveJob::QueueAdapters::InlineAdapter.new
|
2012-04-06 20:57:05 +02:00
|
|
|
yield
|
|
|
|
ensure
|
2021-04-01 16:41:03 +02:00
|
|
|
ActionMailer::MailDeliveryJob.queue_adapter = adapter
|
2012-04-06 20:57:05 +02:00
|
|
|
end
|
|
|
|
|
2013-10-13 09:58:33 +02:00
|
|
|
def mail(headers={}, &block)
|
2019-02-16 00:52:30 +01:00
|
|
|
# Add a display name to the From field if Setting.mail_from does not
|
|
|
|
# include it
|
2019-04-08 15:28:34 +02:00
|
|
|
begin
|
|
|
|
mail_from = Mail::Address.new(Setting.mail_from)
|
|
|
|
if mail_from.display_name.blank? && mail_from.comments.blank?
|
|
|
|
mail_from.display_name =
|
|
|
|
@author&.logged? ? @author.name : Setting.app_title
|
|
|
|
end
|
|
|
|
from = mail_from.format
|
|
|
|
list_id = "<#{mail_from.address.to_s.tr('@', '.')}>"
|
|
|
|
rescue Mail::Field::IncompleteParseError
|
2019-10-19 14:25:34 +02:00
|
|
|
# Use Setting.mail_from as it is if Mail::Address cannot parse it
|
|
|
|
# (probably the emission address is not RFC compliant)
|
2019-04-08 15:28:34 +02:00
|
|
|
from = Setting.mail_from.to_s
|
|
|
|
list_id = "<#{from.tr('@', '.')}>"
|
2019-02-16 00:52:30 +01:00
|
|
|
end
|
|
|
|
|
2014-12-21 20:46:33 +01:00
|
|
|
headers.reverse_merge! 'X-Mailer' => 'Redmine',
|
2008-03-16 17:52:49 +01:00
|
|
|
'X-Redmine-Host' => Setting.host_name,
|
2009-03-07 16:14:10 +01:00
|
|
|
'X-Redmine-Site' => Setting.app_title,
|
2015-04-11 09:15:38 +02:00
|
|
|
'X-Auto-Response-Suppress' => 'All',
|
2012-04-25 19:17:49 +02:00
|
|
|
'Auto-Submitted' => 'auto-generated',
|
2019-04-08 15:28:34 +02:00
|
|
|
'From' => from,
|
|
|
|
'List-Id' => list_id
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2015-01-17 15:14:12 +01:00
|
|
|
# Replaces users with their email addresses
|
|
|
|
[:to, :cc, :bcc].each do |key|
|
|
|
|
if headers[key].present?
|
|
|
|
headers[key] = self.class.email_addresses(headers[key])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-12 19:34:12 +01:00
|
|
|
# Removes the author from the recipients and cc
|
2013-07-06 02:04:47 +02:00
|
|
|
# if the author does not want to receive notifications
|
|
|
|
# about what the author do
|
2019-04-06 08:17:03 +02:00
|
|
|
if @author&.logged? && @author.pref.no_self_notified
|
2015-01-17 15:14:12 +01:00
|
|
|
addresses = @author.mails
|
|
|
|
headers[:to] -= addresses if headers[:to].is_a?(Array)
|
|
|
|
headers[:cc] -= addresses if headers[:cc].is_a?(Array)
|
2007-12-02 14:52:16 +01:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2019-04-06 08:17:03 +02:00
|
|
|
if @author&.logged?
|
2012-02-05 13:57:19 +01:00
|
|
|
redmine_headers 'Sender' => @author.login
|
|
|
|
end
|
|
|
|
|
2012-04-25 19:17:49 +02:00
|
|
|
if @message_id_object
|
2019-03-04 15:36:54 +01:00
|
|
|
headers[:message_id] = "<#{self.class.message_id_for(@message_id_object, @user)}>"
|
2012-04-25 19:17:49 +02:00
|
|
|
end
|
|
|
|
if @references_objects
|
2019-03-04 15:36:54 +01:00
|
|
|
headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o, @user)}>"}.join(' ')
|
2012-04-25 19:17:49 +02:00
|
|
|
end
|
|
|
|
|
2018-10-06 15:08:52 +02:00
|
|
|
if block_given?
|
2013-10-13 09:58:33 +02:00
|
|
|
super headers, &block
|
|
|
|
else
|
|
|
|
super headers do |format|
|
|
|
|
format.text
|
|
|
|
format.html unless Setting.plain_text_mail?
|
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
end
|
2012-04-25 19:17:49 +02:00
|
|
|
end
|
2013-05-25 07:35:13 +02:00
|
|
|
|
2012-04-25 19:17:49 +02:00
|
|
|
def self.deliver_mail(mail)
|
|
|
|
return false if mail.to.blank? && mail.cc.blank? && mail.bcc.blank?
|
2019-11-22 15:55:04 +01:00
|
|
|
|
2013-07-13 10:11:20 +02:00
|
|
|
begin
|
|
|
|
# Log errors when raise_delivery_errors is set to false, Rails does not
|
|
|
|
mail.raise_delivery_errors = true
|
|
|
|
super
|
2019-05-25 08:50:25 +02:00
|
|
|
rescue => e
|
2013-07-13 10:11:20 +02:00
|
|
|
if ActionMailer::Base.raise_delivery_errors
|
|
|
|
raise e
|
|
|
|
else
|
|
|
|
Rails.logger.error "Email delivery error: #{e.message}"
|
|
|
|
end
|
|
|
|
end
|
2007-11-12 17:43:49 +01:00
|
|
|
end
|
2008-11-07 16:37:17 +01:00
|
|
|
|
2015-01-17 15:14:12 +01:00
|
|
|
# Returns an array of email addresses to notify by
|
|
|
|
# replacing users in arg with their notified email addresses
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# Mailer.email_addresses(users)
|
|
|
|
# => ["foo@example.net", "bar@example.net"]
|
|
|
|
def self.email_addresses(arg)
|
|
|
|
arr = Array.wrap(arg)
|
|
|
|
mails = arr.reject {|a| a.is_a? Principal}
|
|
|
|
users = arr - mails
|
|
|
|
if users.any?
|
|
|
|
mails += EmailAddress.
|
|
|
|
where(:user_id => users.map(&:id)).
|
|
|
|
where("is_default = ? OR notify = ?", true, true).
|
|
|
|
pluck(:address)
|
|
|
|
end
|
|
|
|
mails
|
|
|
|
end
|
|
|
|
|
2012-04-25 19:17:49 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
# Appends a Redmine header field (name is prepended with 'X-Redmine-')
|
|
|
|
def redmine_headers(h)
|
2021-05-18 07:18:23 +02:00
|
|
|
h.compact.each {|k, v| headers["X-Redmine-#{k}"] = v.to_s}
|
|
|
|
end
|
|
|
|
|
|
|
|
def assignee_for_header(issue)
|
|
|
|
case issue.assigned_to
|
|
|
|
when User
|
|
|
|
issue.assigned_to.login
|
|
|
|
when Group
|
|
|
|
"Group (#{issue.assigned_to.name})"
|
|
|
|
end
|
2012-04-25 19:17:49 +02:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2019-10-19 15:33:21 +02:00
|
|
|
# Singleton class method is public
|
|
|
|
class << self
|
|
|
|
def token_for(object, user)
|
|
|
|
timestamp = object.send(object.respond_to?(:created_on) ? :created_on : :updated_on)
|
|
|
|
hash = [
|
|
|
|
"redmine",
|
|
|
|
"#{object.class.name.demodulize.underscore}-#{object.id}",
|
|
|
|
timestamp.utc.strftime("%Y%m%d%H%M%S")
|
|
|
|
]
|
|
|
|
hash << user.id if user
|
|
|
|
host = Setting.mail_from.to_s.strip.gsub(%r{^.*@|>}, '')
|
|
|
|
host = "#{::Socket.gethostname}.redmine" if host.empty?
|
|
|
|
"#{hash.join('.')}@#{host}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a Message-Id for the given object
|
|
|
|
def message_id_for(object, user)
|
|
|
|
token_for(object, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a uniq token for a given object referenced by all notifications
|
|
|
|
# related to this object
|
|
|
|
def references_for(object, user)
|
|
|
|
token_for(object, user)
|
|
|
|
end
|
2009-01-18 16:16:31 +01:00
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2009-01-18 16:16:31 +01:00
|
|
|
def message_id(object)
|
|
|
|
@message_id_object = object
|
|
|
|
end
|
2011-05-17 05:57:10 +02:00
|
|
|
|
2009-01-18 16:16:31 +01:00
|
|
|
def references(object)
|
|
|
|
@references_objects ||= []
|
|
|
|
@references_objects << object
|
|
|
|
end
|
|
|
|
end
|