Project

General

Profile

Feature #5913 » mailer-allow-invalid-from.patch

Allow RFC non-compliant emission email addresses - Go MAEDA, 2019-04-07 06:49

View differences:

app/models/mailer.rb
629 629
  def mail(headers={}, &block)
630 630
    # Add a display name to the From field if Setting.mail_from does not
631 631
    # include it
632
    mail_from = Mail::Address.new(Setting.mail_from)
633
    if mail_from.display_name.blank? && mail_from.comments.blank?
634
      mail_from.display_name =
635
        @author&.logged? ? @author.name : Setting.app_title
632
    begin
633
      mail_from = Mail::Address.new(Setting.mail_from)
634
      if mail_from.display_name.blank? && mail_from.comments.blank?
635
        mail_from.display_name =
636
          @author&.logged? ? @author.name : Setting.app_title
637
      end
638
      header_from = mail_from.format
639
      header_list_id = "<#{mail_from.address.to_s.tr('@', '.')}>"
640
    rescue Mail::Field::IncompleteParseError
641
       # Use Setting.mail_from as it is if Mail::Address cannot parse it
642
       # (probably the emission address is not RFC compliant)
643
      header_from = Setting.mail_from.to_s
644
      header_list_id = "<#{header_from.tr('@', '.')}>"
636 645
    end
637 646

  
638 647
    headers.reverse_merge! 'X-Mailer' => 'Redmine',
......
640 649
            'X-Redmine-Site' => Setting.app_title,
641 650
            'X-Auto-Response-Suppress' => 'All',
642 651
            'Auto-Submitted' => 'auto-generated',
643
            'From' => mail_from.format,
644
            'List-Id' => "<#{mail_from.address.to_s.tr('@', '.')}>"
652
            'From' => header_from,
653
            'List-Id' => header_list_id
645 654

  
646 655
    # Replaces users with their email addresses
647 656
    [:to, :cc, :bcc].each do |key|
test/unit/mailer_test.rb
252 252
    assert_equal 'Redmine app <redmine@example.net>', mail.header['From'].to_s
253 253
  end
254 254

  
255
  def test_from_header_with_rfc_non_compliant_phrase
256
    # Send out the email instead of raising an exception
257
    # no matter if the emission email address is not RFC compliant
258
    assert_nothing_raised do
259
      with_settings :mail_from => '[Redmine app] <redmine@example.net>' do
260
        Mailer.deliver_test_email(User.find(1))
261
      end
262
    end
263
    mail = last_email
264
    assert_match /<redmine@example\.net>/, mail.from_addrs.first
265
    assert_equal '[Redmine app] <redmine@example.net>', mail.header['From'].to_s
266
  end
255 267
  def test_from_header_with_author_name
256 268
    # Use the author's name or Setting.app_title as a display name
257 269
    # when Setting.mail_from does not include a display name
(4-4/4)