LoginSignup
2

More than 3 years have passed since last update.

redmineメール通知をカスタマイズする

Last updated at Posted at 2020-11-25

redmineのメール通知機能をカスタマイズする方法をメモします。

優先度が最も高いチケットのみメールする

(旧)

models/issue.rb
  # Returns the users that should be notified
  def notified_users
    ...省略...
    notified += project.notified_users
    notified += project.users.preload(:preference).select(&:notify_about_high_priority_issues?) if priority.high?
    notified.uniq!
    ...省略...
  end
models/issue_priority.rb
  def high?
    position > self.class.default_or_middle.position
  end
views/users/_mail_notifications.html.erb
<% if IssuePriority.default_or_middle and high_priority = IssuePriority.where(['position > ?', IssuePriority.default_or_middle.position]).first %>
<p>
  <%= pref_fields.check_box :notify_about_high_priority_issues %>
  <label for="pref_notify_about_high_priority_issues"><%= t(:label_user_mail_notify_about_high_priority_issues_html, prio: high_priority.name.downcase) %></label>
</p>
<% end %>

(新)

models/issue.rb
  # Returns the users that should be notified
  def notified_users
    ...省略...
    notified += project.notified_users
    notified += project.users.preload(:preference).select(&:notify_about_high_priority_issues?) if priority.highest?
    notified.uniq!
    ...省略...
  end
models/issue_priority.rb
  def high?
    position > self.class.default_or_middle.position
  end

  def highest?
    position_name == "highest"
  end
views/users/_mail_notifications.html.erb
<% if IssuePriority.default_or_middle and highest_priority = IssuePriority.where(['position_name = ?', 'highest']).first %>
<p>
  <%= pref_fields.check_box :notify_about_high_priority_issues %>
  <label for="pref_notify_about_high_priority_issues"><%= t(:label_user_mail_notify_about_high_priority_issues_html, prio: highest_priority.name.downcase) %></label>
</p>
<% end %>

メールのタイトルにサーバ名を常に表示する

(旧)

models/mailer.rb
  def issue_add(user, issue)
    ...省略...
    subject = "[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]"
    subject += " (#{issue.status.name})" if Setting.show_status_changes_in_mail_subject?
    subject += " #{issue.subject}"
    mail :to => user,
      :subject => subject
  end

(新)

models/mailer.rb
  def issue_add(user, issue)
    ...省略...
    subject = "[#{Setting.host_name} #{issue.project.name}] "
    subject += "#{issue.tracker.name} ##{issue.id}"
    subject += " (#{issue.status.name})" if Setting.show_status_changes_in_mail_subject?
    subject += " #{issue.subject}"
    mail :to => user,
      :subject => subject
  end

いろいろな場所にこの設定ありますので,ソース見て適宜書き換えをお願いします。

メールの送信アドレスをチケット更新者にする

smtpサーバに認証がかかっている場合は,サーバ側で上書きされるため上手くいかない可能性があります。サーバ再起動後,送信されるメールが, 送信アドレスと表示名があっている ことを確認お願いします。

ドラフト版でばgmailは無理と記載しておりましたが,以下のように' <' + @author.mail + '>'と半角スペースはさむことで送信アドレス表示できるようになりました。

(旧)

models/mailer.rb
def mail(headers={}, &block)
    # Add a display name to the From field if Setting.mail_from does not
    # include it
    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
      ...省略...
    end

(新)

models/mailer.rb
def mail(headers={}, &block)
    # Add a display name to the From field if Setting.mail_from does not
    # include it
    begin
      mail_from = Mail::Address.new(Setting.mail_from)
      if mail_from.display_name.blank? && mail_from.comments.blank?
        if @author&.logged?
          mail_from.display_name = @author.name

          # Add author address 20201205
          from = @author.name + ' <' + @author.mail + '>'
        else
          mail_from.display_name = Setting.app_title
          from = mail_from.format
        end
      else
        from = mail_from.format
      end
      list_id = "<#{mail_from.address.to_s.tr('@', '.')}>"
    rescue Mail::Field::IncompleteParseError
      ...省略...
    end

環境

redmica 1.2 (redmine 4.2相当)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2