Project

General

Profile

Feature #32519 ยป mail-err.patch

Report error message in rdm-mailhandler.rb - Anthony Mallet, 2019-11-26 17:36

View differences:

app/controllers/mail_handler_controller.rb
26 26
  def index
27 27
    options = params.dup
28 28
    email = options.delete(:email)
29
    if MailHandler.safe_receive(email, options)
29
    error = []
30
    if MailHandler.safe_receive(email, options, error)
30 31
      head :created
31 32
    else
32
      head :unprocessable_entity
33
      render :plain => error.join('\n'), :status => :unprocessable_entity
33 34
    end
34 35
  end
35 36

  
app/models/mail_handler.rb
24 24

  
25 25
  attr_reader :email, :user, :handler_options
26 26

  
27
  def self.receive(raw_mail, options={})
27
  def self.receive(raw_mail, options={}, error=[])
28 28
    options = options.deep_dup
29 29

  
30 30
    options[:issue] ||= {}
......
46 46
    ActiveSupport::Notifications.instrument("receive.action_mailer") do |payload|
47 47
      mail = Mail.new(raw_mail)
48 48
      set_payload_for_mail(payload, mail)
49
      new.receive(mail, options)
49
      new.receive(mail, options, error)
50 50
    end
51 51
  end
52 52

  
......
86 86

  
87 87
  # Processes incoming emails
88 88
  # Returns the created object (eg. an issue, a message) or false
89
  def receive(email, options={})
89
  def receive(email, options={}, error=[])
90 90
    @email = email
91 91
    @handler_options = options
92 92
    sender_email = email.from.to_a.first.to_s.strip
93 93
    # Ignore emails received from the application emission address to avoid hell cycles
94 94
    emission_address = Setting.mail_from.to_s.gsub(/(?:.*<|>.*|\(.*\))/, '').strip
95 95
    if sender_email.casecmp(emission_address) == 0
96
      error << "Ignoring email from Redmine emission address [#{sender_email}]."
96 97
      if logger
97 98
        logger.info  "MailHandler: ignoring email from Redmine emission address [#{sender_email}]"
98 99
      end
......
104 105
      if value
105 106
        value = value.to_s.downcase
106 107
        if (ignored_value.is_a?(Regexp) && value.match(ignored_value)) || value == ignored_value
108
          error << "Ignoring email with #{key}:#{value} header."
107 109
          if logger
108 110
            logger.info "MailHandler: ignoring email with #{key}:#{value} header"
109 111
          end
......
113 115
    end
114 116
    @user = User.find_by_mail(sender_email) if sender_email.present?
115 117
    if @user && !@user.active?
118
      error << "Ignoring email from non-active user [#{@user.login}]."
116 119
      if logger
117 120
        logger.info  "MailHandler: ignoring email from non-active user [#{@user.login}]"
118 121
      end
......
134 137
            ::Mailer.deliver_account_information(@user, @user.password)
135 138
          end
136 139
        else
140
          error << "Could not create account for [#{sender_email}]."
137 141
          if logger
138 142
            logger.error "MailHandler: could not create account for [#{sender_email}]"
139 143
          end
......
141 145
        end
142 146
      else
143 147
        # Default behaviour, emails from unknown users are ignored
148
        error << "Ignoring email from unknown user [#{sender_email}]."
144 149
        if logger
145 150
          logger.info  "MailHandler: ignoring email from unknown user [#{sender_email}]"
146 151
        end
......
148 153
      end
149 154
    end
150 155
    User.current = @user
151
    dispatch
156
    dispatch(error)
152 157
  end
153 158

  
154 159
  private
......
157 162
  ISSUE_REPLY_SUBJECT_RE = %r{\[(?:[^\]]*\s+)?#(\d+)\]}
158 163
  MESSAGE_REPLY_SUBJECT_RE = %r{\[[^\]]*msg(\d+)\]}
159 164

  
160
  def dispatch
165
  def dispatch(error)
161 166
    headers = [email.in_reply_to, email.references].flatten.compact
162 167
    subject = email.subject.to_s
163 168
    if headers.detect {|h| h.to_s =~ MESSAGE_ID_RE}
......
177 182
    end
178 183
  rescue ActiveRecord::RecordInvalid => e
179 184
    # TODO: send a email to the user
185
    error << "#{e.message}."
180 186
    logger.error "MailHandler: #{e.message}" if logger
181 187
    false
182 188
  rescue MissingInformation => e
189
    error << "Missing information from #{user}: #{e.message}."
183 190
    logger.error "MailHandler: missing information from #{user}: #{e.message}" if logger
184 191
    false
185 192
  rescue UnauthorizedAction => e
193
    error << "Unauthorized attempt from #{user}."
186 194
    logger.error "MailHandler: unauthorized attempt from #{user}" if logger
187 195
    false
188 196
  end
extra/mail_handler/rdm-mailhandler.rb
176 176
             "Make sure that 'WS for incoming emails' is enabled in application settings and that you provided the correct API key."
177 177
        return 77
178 178
      when 422
179
        warn "Request was denied by your Redmine server. " +
180
             "Possible reasons: email is sent from an invalid email address or is missing some information."
179
        warn response.body
180
        warn "Request was denied by your Redmine server."
181 181
        return 77
182 182
      when 400..499
183 183
        warn "Request was denied by your Redmine server (#{response.code})."
    (1-1/1)