Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

In case of errors in mail_handler, serialize mail object properly

Oliver Güntheree4fdf78a8fb
diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb
index 586709b18ee..eb62bd5ef2b 100644
--- a/app/mailers/user_mailer.rb
+++ b/app/mailers/user_mailer.rb
@@ -229,35 +229,24 @@ class UserMailer < ApplicationMailer
# a work package, or forum message for instance.
#
# @param [User] user User who sent the email
- # @param [Mail] mail Sent email
- # @param [Array<String>] List of logs collected during processing of the email
+ # @param [Object] mail The mail object prepared by the mail handler
+ # @param [Array<String>] logs List of logs collected during processing of the email
def incoming_email_error(user, mail, logs)
@user = user
- @mail = mail
@logs = logs
+ @mail_from = mail[:from]
@received_at = DateTime.now
- @incoming_text = incoming_email_text mail
- @quote = incoming_email_quote mail
+ @incoming_text = mail[:text]
+ @quote = mail[:quote]
- headers['References'] = ["<#{mail.message_id}>"]
- headers['In-Reply-To'] = ["<#{mail.message_id}>"]
+ headers['References'] = ["<#{mail[:message_id]}>"]
+ headers['In-Reply-To'] = ["<#{mail[:message_id]}>"]
- send_mail user, mail.subject.present? ? "Re: #{mail.subject}" : I18n.t("mail_subject_incoming_email_error")
+ send_mail user, mail[:subject].present? ? "Re: #{mail[:subject]}" : I18n.t("mail_subject_incoming_email_error")
end
private
- def incoming_email_text(mail)
- mail.text_part.present? ? mail.text_part.body.to_s : mail.body.to_s
- end
-
- def incoming_email_quote(mail)
- quote = incoming_email_text(mail)
- quoted = String(quote).lines.join("> ")
-
- "> #{quoted}"
- end
-
def open_project_wiki_headers(wiki_content)
open_project_headers 'Project' => wiki_content.project.identifier,
'Wiki-Page-Id' => wiki_content.page.id,
diff --git a/app/models/mail_handler.rb b/app/models/mail_handler.rb
index 358cfb82b8f..2d36b6207c6 100644
--- a/app/models/mail_handler.rb
+++ b/app/models/mail_handler.rb
@@ -613,7 +613,28 @@ class MailHandler < ActionMailer::Base
def report_errors
return if logs.empty?
- UserMailer.incoming_email_error(user, email, logs).deliver_later
+ UserMailer.incoming_email_error(user, mail_as_hash(email), logs).deliver_later
+ end
+
+ def mail_as_hash(email)
+ {
+ message_id: email.message_id,
+ subject: email.subject,
+ from: email.from&.first || '(unknown from address)',
+ quote: incoming_email_quote(email),
+ text: incoming_email_text(email)
+ }
+ end
+
+ def incoming_email_text(mail)
+ mail.text_part.present? ? mail.text_part.body.to_s : mail.body.to_s
+ end
+
+ def incoming_email_quote(mail)
+ quote = incoming_email_text(mail)
+ quoted = String(quote).lines.join("> ")
+
+ "> #{quoted}"
end
def work_package_create_contract_class
diff --git a/app/views/user_mailer/incoming_email_error.html.erb b/app/views/user_mailer/incoming_email_error.html.erb
index abec4dae15d..e3e8ccfe4ed 100644
--- a/app/views/user_mailer/incoming_email_error.html.erb
+++ b/app/views/user_mailer/incoming_email_error.html.erb
@@ -41,7 +41,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= t(
:mail_body_incoming_email_error_in_reply_to,
received_at: format_time(@received_at),
- from_email: @mail.from.first
+ from_email: @mail_from
)%>:
</p>
diff --git a/app/views/user_mailer/incoming_email_error.text.erb b/app/views/user_mailer/incoming_email_error.text.erb
index 6a651a8f2a0..d1892a72e12 100644
--- a/app/views/user_mailer/incoming_email_error.text.erb
+++ b/app/views/user_mailer/incoming_email_error.text.erb
@@ -36,7 +36,7 @@ See COPYRIGHT and LICENSE files for more details.
<%= t(
:mail_body_incoming_email_error_in_reply_to,
received_at: format_time(@received_at),
- from_email: @mail.from.first
+ from_email: @mail_from
)%>:
<%= @quote %>
diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb
index 32b99c06af8..08319947bd3 100644
--- a/spec/mailers/user_mailer_spec.rb
+++ b/spec/mailers/user_mailer_spec.rb
@@ -255,15 +255,21 @@ describe UserMailer, type: :mailer do
let(:logs) { ['info: foo', 'error: bar'] }
let(:recipient) { user }
let(:current_time) { "2022-11-03 9:15".to_time }
- let(:incoming_email) do
- Mail.new(subject: mail_subject, message_id:, body:, from:)
- end
-
let(:mail_subject) { 'New work package 42' }
- let(:message_id) { '<000501c8d452$a95cd7e0$0a00a8c0@osiris>' }
+ let(:message_id) { '000501c8d452$a95cd7e0$0a00a8c0@osiris' }
let(:from) { '[email protected]' }
let(:body) { "Project: demo-project" }
+ let(:incoming_email) do
+ {
+ message_id:,
+ from:,
+ subject: mail_subject,
+ quote: body,
+ text: body
+ }
+ end
+
let(:outgoing_email) { deliveries.first }
before do
diff --git a/spec/models/mail_handler_spec.rb b/spec/models/mail_handler_spec.rb
index 35c63356b52..70cee25abf0 100644
--- a/spec/models/mail_handler_spec.rb
+++ b/spec/models/mail_handler_spec.rb
@@ -509,7 +509,7 @@ describe MailHandler, type: :model do
it 'responds with an error email' do
expect(UserMailer).to have_received(:incoming_email_error) do |user, mail, logs|
expect(user).to eq anno_user
- expect(mail.subject).to eq "Ticket by unknown user"
+ expect(mail[:subject]).to eq "Ticket by unknown user"
expect(logs).to eq [expected.sub(/^MailHandler/, "error")]
end
end