diff --git a/app/models/mailer.rb b/app/models/mailer.rb index 492ca976d..8e9e1132c 100644 --- a/app/models/mailer.rb +++ b/app/models/mailer.rb @@ -615,13 +615,21 @@ class Mailer < ActionMailer::Base end def mail(headers={}, &block) + # Assign 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 && @author.logged?) ? @author.name : Setting.app_title + end + headers.reverse_merge! 'X-Mailer' => 'Redmine', 'X-Redmine-Host' => Setting.host_name, 'X-Redmine-Site' => Setting.app_title, 'X-Auto-Response-Suppress' => 'All', 'Auto-Submitted' => 'auto-generated', - 'From' => Setting.mail_from, - 'List-Id' => "<#{Setting.mail_from.to_s.tr('@', '.')}>" + 'From' => mail_from.format, + 'List-Id' => "<#{mail_from.address.to_s.tr('@', '.')}>" # Replaces users with their email addresses [:to, :cc, :bcc].each do |key| diff --git a/test/unit/mailer_test.rb b/test/unit/mailer_test.rb index 37e41e87b..f22f532f4 100644 --- a/test/unit/mailer_test.rb +++ b/test/unit/mailer_test.rb @@ -249,6 +249,25 @@ class MailerTest < ActiveSupport::TestCase assert_equal 'Redmine app ', mail.header['From'].to_s end + def test_from_header_with_author_name + # Use the author's name or Setting.app_title as a display name + # when Setting.mail_from does not include a display name + with_settings :mail_from => 'redmine@example.net', :app_title => 'Foo' do + # Use @author.name as a display name + Issue.create!(:project_id => 1, :tracker_id => 1, :status_id => 5, + :subject => 'Issue created by Dave Lopper', :author_id => 3) + mail = last_email + assert_equal 'redmine@example.net', mail.from_addrs.first + assert_equal 'Dave Lopper ', mail.header['From'].to_s + + # Use app_title if @author is nil or AnonymousUser + Mailer.deliver_test_email(User.find(1)) + mail = last_email + assert_equal 'redmine@example.net', mail.from_addrs.first + assert_equal "Foo ", mail.header['From'].to_s + end + end + def test_should_not_send_email_without_recipient news = News.first user = news.author