diff --git a/app/models/mailer.rb b/app/models/mailer.rb index ddbc7b394..7721997f6 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -661,16 +661,25 @@ class Mailer < ActionMailer::Base end def mail(headers={}, &block) - # Add a display name to the From field if Setting.mail_from does not - # include it begin + # Add a display name to the From field if Setting.mail_from does not + # include it 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('@', '.')}>" + + # Construct the value of the List-Id header field + from_addr = mail_from.address.to_s + project_identifier = self.headers['X-Redmine-Project']&.value + list_id = if project_identifier.present? + "<#{project_identifier}.#{from_addr.tr('@', '.')}>" + else + # Emails outside of a project context + "<#{from_addr.tr('@', '.')}>" + end rescue Mail::Field::IncompleteParseError # Use Setting.mail_from as it is if Mail::Address cannot parse it # (probably the emission address is not RFC compliant) diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb index 0506e27d2..afead7a47 100644 --- a/test/unit/mailer_test.rb +++ b/test/unit/mailer_test.rb @@ -220,7 +220,7 @@ class MailerTest < ActiveSupport::TestCase assert_equal 'All', mail.header['X-Auto-Response-Suppress'].to_s assert_equal 'auto-generated', mail.header['Auto-Submitted'].to_s # List-Id should not include the display name "Redmine" - assert_equal '', mail.header['List-Id'].to_s + assert_equal '', mail.header['List-Id'].to_s assert_equal 'Bug', mail.header['X-Redmine-Issue-Tracker'].to_s assert_equal 'Low', mail.header['X-Redmine-Issue-Priority'].to_s end @@ -327,6 +327,23 @@ class MailerTest < ActiveSupport::TestCase end end + def test_list_id_header_should_include_project_identifier + with_settings :mail_from => 'redmine@example.net' do + content = WikiContent.find(1) + Mailer.deliver_wiki_content_added(content) + mail = last_email + assert_equal '', mail.header['List-Id'].to_s + end + end + + def test_list_id_header_excludes_project_identifier_for_non_project_emails + with_settings :mail_from => 'redmine@example.net' do + Mailer.deliver_test_email(User.find(1)) + mail = last_email + assert_equal '', mail.header['List-Id'].to_s + end + end + def test_should_not_send_email_without_recipient news = News.first user = news.author