diff --git a/app/jobs/scheduled/poll_mailbox.rb b/app/jobs/scheduled/poll_mailbox.rb index e1f161139..abaf26343 100644 --- a/app/jobs/scheduled/poll_mailbox.rb +++ b/app/jobs/scheduled/poll_mailbox.rb @@ -22,23 +22,36 @@ module Jobs begin mail_string = mail.pop Email::Receiver.new(mail_string).process - rescue Email::Receiver::UserNotSufficientTrustLevelError + rescue => e # inform the user about the rejection message = Mail::Message.new(mail_string) - client_message = RejectionMailer.send_trust_level(message.from, message.body) - Email::Sender.new(client_message, :email_reject_trust_level).send - rescue Email::Receiver::ProcessingError => e - Rails.logger.error e - message = Mail::Message.new(mail_string) - client_message = RejectionMailer.send_rejection(message.from, message.body, e.message) + message_template = nil + case e + when Email::Receiver::UserNotSufficientTrustLevelError + message_template = :email_reject_trust_level + when Email::Receiver::UserNotFoundError + message_template = :email_reject_no_account + when Email::Receiver::EmptyEmailError + message_template = :email_reject_empty + when Email::Receiver::EmailUnparsableError + message_template = :email_reject_parsing + when ActiveRecord::Rollback + message_template = :email_reject_post_error + else + nil + end - # inform admins about the error - data = { limit_once_per: false, message_params: { source: mail, error: e }} - GroupMessage.create(Group[:admins].name, :email_error_notification, data) - rescue StandardError => e - # inform admins about the error - data = { limit_once_per: false, message_params: { source: mail, error: e }} - GroupMessage.create(Group[:admins].name, :email_error_notification, data) + if message_template + # Send message to the user + client_message = RejectionMailer.send_rejection(message.from, message.body, message_template.to_s, "#{e.message}\n\n#{e.backtrace.join("\n")}") + Email::Sender.new(client_message, message_template).send + else + Rails.logger.error e + + # If not known type, inform admins about the error + data = { limit_once_per: false, message_params: { from: message.from, source: message.body, error: "#{e.message}\n\n#{e.backtrace.join("\n")}" }} + GroupMessage.create(Group[:admins].name, :email_error_notification, data) + end ensure mail.delete end diff --git a/app/mailers/rejection_mailer.rb b/app/mailers/rejection_mailer.rb index 0bf9e2b6a..324da3961 100644 --- a/app/mailers/rejection_mailer.rb +++ b/app/mailers/rejection_mailer.rb @@ -3,11 +3,11 @@ require_dependency 'email/message_builder' class RejectionMailer < ActionMailer::Base include Email::BuildEmailHelper - def send_rejection(from, body, error) - build_email(from, template: 'email_error_notification', error: "#{error.message}\n\n#{error.backtrace.join("\n")}", source: body) + def send_rejection(from, body, template, error) + build_email(from, template: template, error: error, source: body) end - def send_trust_level(from, body) + def send_trust_level(from, template) build_email(from, template: 'email_reject_trust_level') end end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 860627621..c859cb3a4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1302,9 +1302,13 @@ en: text_body_template: | This is an automated message. - Parsing an incoming email failed. Please review the following Error: + Parsing an incoming email from `%{from}` failed. Please review the following Error: - `%{error}` + --- + + %{error} + + --- The original message follows. @@ -1313,12 +1317,40 @@ en: %{source} email_reject_trust_level: - subject_template: "Message rejected" + subject_template: "Message rejected: Insufficient Trust Level" text_body_template: | The message you've send to %{to} was rejected by the system. You do not have the required trust to post new topics to this email address. + email_reject_no_account: + subject_template: "Message rejected: No Account" + text_body_template: | + The message you've send to %{to} was rejected by the system. + + You do not have an account on the forum with this email address. + + email_reject_empty: + subject_template: "Message rejected: No Content" + text_body_template: | + The message you've send to %{to} was rejected by the system. + + There was no recognized content in the email. + + email_reject_parsing: + subject_template: "Message rejected: Unparsable" + text_body_template: | + The message you've send to %{to} was rejected by the system. + + The encoding was unknown or not supported. Try sending in UTF-8 plain text. + + email_reject_post_error: + subject_template: "Message rejected: Posting error" + text_body_template: | + The message you've send to %{to} was rejected by the system. + + The Markdown could not be processed. + too_many_spam_flags: subject_template: "New account blocked" text_body_template: |