Method: Mailer#mail

Defined in:
app/models/mailer.rb

#mail(headers = {}, &block) ⇒ Object



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# File 'app/models/mailer.rb', line 666

def mail(headers={}, &block)
  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

    # 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)
    from = Setting.mail_from.to_s
    list_id = "<#{from.tr('@', '.')}>"
  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' => from,
          'List-Id' => list_id

  # 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

  # Removes the author from the recipients and cc
  # if the author does not want to receive notifications
  # about what the author do
  if @author&.logged? && @author.pref.no_self_notified
    addresses = @author.mails
    headers[:to] -= addresses if headers[:to].is_a?(Array)
    headers[:cc] -= addresses if headers[:cc].is_a?(Array)
  end

  if @author&.logged?
    redmine_headers 'Sender' => @author.
  end

  if @message_id_object
    headers[:message_id] = "<#{self.class.message_id_for(@message_id_object, @user)}>"
  end
  if @references_objects
    headers[:references] = @references_objects.collect {|o| "<#{self.class.references_for(o, @user)}>"}.join(' ')
  end

  if block
    super
  else
    super do |format|
      format.text
      format.html unless Setting.plain_text_mail?
    end
  end
end