From 8d2c878b3486b519a616fc97f51d2c7e95976fdb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9gis=20Hanol?= <regis@hanol.fr>
Date: Fri, 15 Jul 2016 13:18:55 +0200
Subject: [PATCH] FIX: only send 1 rejection email per day to prevent loops

---
 lib/email/processor.rb | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/lib/email/processor.rb b/lib/email/processor.rb
index 081121e02f1..3005b00d75f 100644
--- a/lib/email/processor.rb
+++ b/lib/email/processor.rb
@@ -74,8 +74,8 @@ module Email
 
         client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
 
-        # don't send more than 1 reply per day to auto-generated emails
-        if !incoming_email.try(:is_auto_generated) || can_reply_to_auto_generated?(message.from)
+        # only send one rejection email per day to the same email address
+        if can_send_rejection_email?(message.from, message_template)
           Email::Sender.new(client_message, message_template).send
         end
       else
@@ -89,8 +89,8 @@ module Email
       client_message
     end
 
-    def can_reply_to_auto_generated?(email)
-      key = "auto_generated_reply:#{email}:#{Date.today}"
+    def can_send_rejection_email?(email, type)
+      key = "rejection_email:#{email}:#{type}:#{Date.today}"
 
       if $redis.setnx(key, "1")
         $redis.expire(key, 25.hours)