Merge pull request #2465 from riking/email_rejections

Give specific message for each kind of email rejection
This commit is contained in:
Sam 2014-06-26 09:55:24 +10:00
commit 67804cb56b
4 changed files with 94 additions and 21 deletions

View File

@ -22,19 +22,39 @@ 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
# 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)
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 Email::Receiver::EmailLogNotFound
message_template = :email_reject_reply_key
when ActiveRecord::Rollback
message_template = :email_reject_post_error
else
message_template = nil
end
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
# (Add to above case with a good error message!)
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

View File

@ -3,11 +3,11 @@ require_dependency 'email/message_builder'
class RejectionMailer < ActionMailer::Base
include Email::BuildEmailHelper
def send_rejection(from, body)
build_email(from, template: 'email_error_notification', from: from, body: body)
def send_rejection(from, body, template, error)
build_email(from, from: from, template: template, error: error, source: body)
end
def send_trust_level(from, body, to)
build_email(from, template: 'email_reject_trust_level', to: to)
def send_trust_level(from, template)
build_email(from, template: 'email_reject_trust_level')
end
end

View File

@ -1304,19 +1304,64 @@ 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.
---
%{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.
%{error}
email_reject_reply_key:
subject_template: "Message rejected: Bad Reply Key"
text_body_template: |
The message you've send to %{to} was rejected by the system.
The provided reply key is invalid.
too_many_spam_flags:
subject_template: "New account blocked"
text_body_template: |

View File

@ -75,7 +75,7 @@ describe Jobs::PollMailbox do
client_message = mock
sender = mock
RejectionMailer.expects(:send_trust_level).returns(client_message)
RejectionMailer.expects(:send_rejection).returns(client_message)
Email::Sender.expects(:new).with(client_message, :email_reject_trust_level).returns(sender)
sender.expects(:send)
@ -90,6 +90,8 @@ describe Jobs::PollMailbox do
Email::Receiver::EmptyEmailError,
Email::Receiver::UserNotFoundError,
Email::Receiver::EmailLogNotFound,
ActiveRecord::Rollback,
TypeError
].each do |exception|
it "deletes email on #{exception}" do
@ -103,10 +105,16 @@ describe Jobs::PollMailbox do
it "informs admins on any other error" do
error = TypeError.new
data = { limit_once_per: false, message_params: { source: email, error: error }}
error.set_backtrace(["Hi", "Bye"])
receiver.expects(:process).raises(error)
email.expects(:delete)
Mail::Message.expects(:new).with(email_string).returns(email)
email.expects(:from).twice
email.expects(:body).twice
data = { limit_once_per: false, message_params: { from: email.from, source: email.body, error: "#{error.message}\n\n#{error.backtrace.join("\n")}" }}
GroupMessage.expects(:create).with("admins", :email_error_notification, data)
poller.handle_mail(email)