2016-06-22 09:50:49 -04:00
|
|
|
module Email
|
|
|
|
|
|
|
|
class Processor
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(mail, retry_on_rate_limit = true)
|
2016-06-22 09:50:49 -04:00
|
|
|
@mail = mail
|
2016-08-08 16:28:27 -04:00
|
|
|
@retry_on_rate_limit = retry_on_rate_limit
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def self.process!(mail, retry_on_rate_limit = true)
|
2016-08-08 16:28:27 -04:00
|
|
|
Email::Processor.new(mail, retry_on_rate_limit).process!
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def process!
|
|
|
|
begin
|
|
|
|
receiver = Email::Receiver.new(@mail)
|
|
|
|
receiver.process!
|
2016-08-08 16:28:27 -04:00
|
|
|
rescue RateLimiter::LimitExceeded
|
|
|
|
@retry_on_rate_limit ? Jobs.enqueue(:process_email, mail: @mail) : raise
|
2016-06-22 09:50:49 -04:00
|
|
|
rescue Email::Receiver::BouncedEmailError => e
|
2016-06-28 10:42:05 -04:00
|
|
|
# never reply to bounced emails
|
2016-06-22 09:50:49 -04:00
|
|
|
log_email_process_failure(@mail, e)
|
|
|
|
set_incoming_email_rejection_message(receiver.incoming_email, I18n.t("emails.incoming.errors.bounced_email_error"))
|
|
|
|
rescue => e
|
|
|
|
log_email_process_failure(@mail, e)
|
2016-06-28 10:42:05 -04:00
|
|
|
incoming_email = receiver.try(:incoming_email)
|
|
|
|
rejection_message = handle_failure(@mail, e, incoming_email)
|
|
|
|
if rejection_message.present?
|
2016-06-22 09:50:49 -04:00
|
|
|
set_incoming_email_rejection_message(incoming_email, rejection_message.body.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-06-28 10:42:05 -04:00
|
|
|
def handle_failure(mail_string, e, incoming_email)
|
2016-06-22 09:50:49 -04:00
|
|
|
message_template = case e
|
2017-07-27 21:20:09 -04:00
|
|
|
when Email::Receiver::EmptyEmailError then :email_reject_empty
|
|
|
|
when Email::Receiver::NoBodyDetectedError then :email_reject_empty
|
|
|
|
when Email::Receiver::UserNotFoundError then :email_reject_user_not_found
|
|
|
|
when Email::Receiver::ScreenedEmailError then :email_reject_screened_email
|
|
|
|
when Email::Receiver::AutoGeneratedEmailError then :email_reject_auto_generated
|
|
|
|
when Email::Receiver::InactiveUserError then :email_reject_inactive_user
|
|
|
|
when Email::Receiver::BlockedUserError then :email_reject_blocked_user
|
|
|
|
when Email::Receiver::BadDestinationAddress then :email_reject_bad_destination_address
|
|
|
|
when Email::Receiver::StrangersNotAllowedError then :email_reject_strangers_not_allowed
|
|
|
|
when Email::Receiver::InsufficientTrustLevelError then :email_reject_insufficient_trust_level
|
|
|
|
when Email::Receiver::ReplyUserNotMatchingError then :email_reject_reply_user_not_matching
|
|
|
|
when Email::Receiver::TopicNotFoundError then :email_reject_topic_not_found
|
|
|
|
when Email::Receiver::TopicClosedError then :email_reject_topic_closed
|
|
|
|
when Email::Receiver::InvalidPost then :email_reject_invalid_post
|
|
|
|
when ActiveRecord::Rollback then :email_reject_invalid_post
|
|
|
|
when Email::Receiver::InvalidPostAction then :email_reject_invalid_post_action
|
|
|
|
when Discourse::InvalidAccess then :email_reject_invalid_access
|
2017-07-21 13:24:31 -04:00
|
|
|
else :email_reject_unrecognized_error
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
template_args = {}
|
|
|
|
client_message = nil
|
|
|
|
|
|
|
|
# there might be more information available in the exception
|
|
|
|
if message_template == :email_reject_invalid_post && e.message.size > 6
|
|
|
|
message_template = :email_reject_invalid_post_specified
|
|
|
|
template_args[:post_error] = e.message
|
|
|
|
end
|
|
|
|
|
2017-07-21 13:24:31 -04:00
|
|
|
if message_template == :email_reject_unrecognized_error
|
|
|
|
msg = "Unrecognized error type (#{e.class}: #{e.message}) when processing incoming email"
|
|
|
|
msg += "\n\nBacktrace:\n#{e.backtrace.map { |l| " #{l}" }.join("\n")}"
|
|
|
|
msg += "\n\nMail:\n#{mail_string}"
|
|
|
|
|
|
|
|
Rails.logger.error(msg)
|
|
|
|
end
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
if message_template
|
|
|
|
# inform the user about the rejection
|
|
|
|
message = Mail::Message.new(mail_string)
|
|
|
|
template_args[:former_title] = message.subject
|
|
|
|
template_args[:destination] = message.to
|
|
|
|
template_args[:site_name] = SiteSetting.title
|
|
|
|
|
|
|
|
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
|
2016-06-28 10:42:05 -04:00
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
# only send one rejection email per day to the same email address
|
|
|
|
if can_send_rejection_email?(message.from, message_template)
|
2016-06-28 10:42:05 -04:00
|
|
|
Email::Sender.new(client_message, message_template).send
|
|
|
|
end
|
2016-06-22 09:50:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
client_message
|
|
|
|
end
|
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
def can_send_rejection_email?(email, type)
|
2017-07-21 13:24:31 -04:00
|
|
|
return true if type == :email_reject_unrecognized_error
|
|
|
|
|
2016-07-15 07:18:55 -04:00
|
|
|
key = "rejection_email:#{email}:#{type}:#{Date.today}"
|
2016-06-28 10:42:05 -04:00
|
|
|
|
|
|
|
if $redis.setnx(key, "1")
|
|
|
|
$redis.expire(key, 25.hours)
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-22 09:50:49 -04:00
|
|
|
def set_incoming_email_rejection_message(incoming_email, message)
|
|
|
|
incoming_email.update_attributes!(rejection_message: message)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_email_process_failure(mail_string, exception)
|
|
|
|
if SiteSetting.log_mail_processing_failures
|
|
|
|
Rails.logger.warn("Email can not be processed: #{exception}\n\n#{mail_string}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|