From d87edce6c35d3e833c0653f5d3d5e68849f1b075 Mon Sep 17 00:00:00 2001
From: riking <rikingcoding@gmail.com>
Date: Fri, 1 Aug 2014 09:56:15 -0700
Subject: [PATCH] Pass rejection message along in rejection mail if present

---
 app/jobs/scheduled/poll_mailbox.rb | 15 ++++++++++++---
 app/mailers/rejection_mailer.rb    | 27 +++++++++++++++++++++------
 config/locales/server.en.yml       | 13 ++++++++++++-
 lib/email/message_builder.rb       |  2 +-
 4 files changed, 46 insertions(+), 11 deletions(-)

diff --git a/app/jobs/scheduled/poll_mailbox.rb b/app/jobs/scheduled/poll_mailbox.rb
index 82bdc6795..36cd759f9 100644
--- a/app/jobs/scheduled/poll_mailbox.rb
+++ b/app/jobs/scheduled/poll_mailbox.rb
@@ -25,6 +25,7 @@ module Jobs
         Email::Receiver.new(mail_string).process
       rescue => e
         message_template = nil
+        template_args = {}
         case e
           when Email::Receiver::UserNotSufficientTrustLevelError
             message_template = :email_reject_trust_level
@@ -39,8 +40,13 @@ module Jobs
           when ActiveRecord::Rollback
             message_template = :email_reject_post_error
           when Email::Receiver::InvalidPost
-            # TODO there is a message in this exception, place it in email
-            message_template = :email_reject_post_error
+            if e.message.length < 6
+              message_template = :email_reject_post_error
+            else
+              message_template = :email_reject_post_error_specified
+              template_args[:post_error] = e.message
+            end
+
           else
             message_template = nil
         end
@@ -48,7 +54,10 @@ module Jobs
         if message_template
           # inform the user about the rejection
           message = Mail::Message.new(mail_string)
-          client_message = RejectionMailer.send_rejection(message.from, message.body, message.subject, message.to, message_template)
+          template_args[:former_title] = message.subject
+          template_args[:destination] = message.to
+
+          client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
           Email::Sender.new(client_message, message_template).send
         else
           Discourse.handle_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
diff --git a/app/mailers/rejection_mailer.rb b/app/mailers/rejection_mailer.rb
index 20727b459..6eaf4b915 100644
--- a/app/mailers/rejection_mailer.rb
+++ b/app/mailers/rejection_mailer.rb
@@ -3,12 +3,27 @@ require_dependency 'email/message_builder'
 class RejectionMailer < ActionMailer::Base
   include Email::BuildEmailHelper
 
-  def send_rejection(message_from, message_body, message_subject, forum_address, template)
-    build_email(message_from,
-                template: "system_messages.#{template}",
-                source: message_body,
-                former_title: message_subject,
-                destination: forum_address)
+  DISALLOWED_TEMPLATE_ARGS = [:to, :from, :site_name, :base_url,
+                              :user_preferences_url,
+                              :include_respond_instructions, :html_override,
+                              :add_unsubscribe_link, :respond_instructions,
+                              :style, :body, :post_id, :topic_id, :subject,
+                              :template, :allow_reply_by_email,
+                              :private_reply, :from_alias]
+
+  # Send an email rejection message.
+  #
+  # template - i18n key under system_messages
+  # message_from - Who to send the rejection messsage to
+  # template_args - arguments to pass to i18n for interpolation into the message
+  #     Certain keys are disallowed in template_args to avoid confusing the
+  #     BuildEmailHelper. You can see the list in DISALLOWED_TEMPLATE_ARGS.
+  def send_rejection(template, message_from, template_args)
+    if template_args.keys.any? { |k| DISALLOWED_TEMPLATE_ARGS.include? k }
+      raise ArgumentError.new('Reserved key in template arguments')
+    end
+
+    build_email(message_from, template_args.merge(template: "system_messages.#{template}"))
   end
 
 end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index e97675c58..f3e1c6941 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1419,7 +1419,18 @@ en:
       text_body_template: |
         We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.
 
-        Some possible causes are: complex formatting, message too large, message too small. Please try again.
+        Some possible causes are: complex formatting, message too large, message too small. Please try again, or post via the website if this continues.
+
+    email_reject_post_error_specified:
+      subject_template: "Email issue -- Posting error"
+      text_body_template: |
+        We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.
+
+        Rejection message:
+
+        %{post_error}
+
+        Please attempt to fix the errors and try again.
 
     email_reject_reply_key:
       subject_template: "Email issue -- Bad Reply Key"
diff --git a/lib/email/message_builder.rb b/lib/email/message_builder.rb
index 310f97f9e..204827886 100644
--- a/lib/email/message_builder.rb
+++ b/lib/email/message_builder.rb
@@ -49,7 +49,7 @@ module Email
       return unless html_override = @opts[:html_override]
       if @opts[:add_unsubscribe_link]
 
-        if response_instructions = @template_args[:respond_instructions]
+        if response_instructions = @opts[:respond_instructions]
           respond_instructions = PrettyText.cook(response_instructions).html_safe
           html_override.gsub!("%{respond_instructions}", respond_instructions)
         end