From f177a18a57c61ac3c56c4c7ae2acb7276bd7a668 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Sun, 2 Dec 2012 15:49:46 -0500
Subject: [PATCH] Add bulk/auto-reply headers to outgoing emails as needed

---
 include/class.mailer.php   | 15 ++++++++++++++-
 include/class.osticket.php |  2 +-
 include/class.ticket.php   | 24 ++++++++++++------------
 include/class.upgrader.php |  2 +-
 4 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/include/class.mailer.php b/include/class.mailer.php
index a227b8fc5..57242e616 100644
--- a/include/class.mailer.php
+++ b/include/class.mailer.php
@@ -107,7 +107,20 @@ class Mailer {
                 'Date'=> date('D, d M Y H:i:s O'),
                 'Message-ID' => $messageId,
                 'X-Mailer' =>'osTicket Mailer'
-                );
+               );
+
+        //Set bulk/auto-response headers.
+        if($options && ($options['autoreply'] or $options['bulk'])) {
+            $headers+= array(
+                    'X-Autoreply' => 'yes',
+                    'X-Auto-Response-Suppress' => 'ALL, AutoReply',
+                    'Auto-Submitted' => 'auto-replied');
+
+            if($options['bulk']) 
+                $headers+= array('Precedence' => 'bulk');
+            else
+                $headers+= array('Precedence' => 'auto_reply');
+        }
 
         $mime = new Mail_mime();
         $mime->setTXTBody($body);
diff --git a/include/class.osticket.php b/include/class.osticket.php
index 86d206f21..80d6216b1 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -242,7 +242,7 @@ class osTicket {
             $email=$this->getConfig()->getDefaultEmail(); //will take the default email.
 
         if($email) {
-            $email->send($to, $subject, $message);
+            $email->send($to, $subject, $message, null, array('bulk' => true));
         } else {//no luck - try the system mail.
             Email::sendmail($to, $subject, $message, sprintf('"osTicket Alerts"<%s>',$to));
         }
diff --git a/include/class.ticket.php b/include/class.ticket.php
index b4b7952b5..d77291f0f 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -868,7 +868,7 @@ class Ticket {
                 $msg['body'] ="\n$tag\n\n".$msg['body'];
             
             //TODO: add auto flags....be nice to mail servers and sysadmins!!
-            $email->send($this->getEmail(), $msg['subj'], $msg['body']);
+            $email->send($this->getEmail(), $msg['subj'], $msg['body'], null, array('autoreply' => true));
         }
         
         if(!($email=$cfg->getAlertEmail()))
@@ -885,7 +885,7 @@ class Ticket {
             //Alert admin??
             if($cfg->alertAdminONNewTicket()) {
                 $alert = str_replace('%{recipient}', 'Admin', $msg['body']);
-                $email->send($cfg->getAdminEmail(), $msg['subj'], $alert);
+                $email->send($cfg->getAdminEmail(), $msg['subj'], $alert, null, array('bulk' => true));
                 $sentlist[]=$cfg->getAdminEmail();
             }
               
@@ -901,7 +901,7 @@ class Ticket {
             foreach( $recipients as $k=>$staff){
                 if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(),$sentlist)) continue;
                 $alert = str_replace('%{recipient}', $staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, null, array('bulk' => true));
                 $sentlist[] = $staff->getEmail();
             }
            
@@ -934,7 +934,7 @@ class Ticket {
             $msg = $this->replaceVars($msg, 
                         array('signature' => ($dept && $dept->isPublic())?$dept->getSignature():''));
             
-            $email->send($this->getEmail(), $msg['subj'], $msg['body']);
+            $email->send($this->getEmail(), $msg['subj'], $msg['body'], null, array('bulk' => true));
         }
 
         $client= $this->getClient();
@@ -997,7 +997,7 @@ class Ticket {
             if($cfg->stripQuotedReply() && ($tag=$cfg->getReplySeparator()))
                 $msg['body'] ="\n$tag\n\n".$msg['body'];
         
-            $email->send($this->getEmail(), $msg['subj'], $msg['body']);
+            $email->send($this->getEmail(), $msg['subj'], $msg['body'], null, array('autoreply'=>true));
         }
     }
 
@@ -1056,7 +1056,7 @@ class Ticket {
             foreach( $recipients as $k=>$staff) {
                 if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(),$sentlist)) continue;
                 $alert = str_replace('%{recipient}', $staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, null, array('autoreply' => true));
                 $sentlist[] = $staff->getEmail();
             }
         }
@@ -1109,7 +1109,7 @@ class Ticket {
             foreach( $recipients as $k=>$staff){
                 if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(),$sentlist)) continue;
                 $alert = str_replace("%{recipient}", $staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, null, array('bulk' => true));
                 $sentlist[] = $staff->getEmail();
             }
 
@@ -1294,7 +1294,7 @@ class Ticket {
             foreach( $recipients as $k=>$staff){
                 if(!is_object($staff) || !$staff->isAvailable() || in_array($staff->getEmail(),$sentlist)) continue;
                 $alert = str_replace('%{recipient}',$staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, null, array('bulk' => true));
                 $sentlist[] = $staff->getEmail();
             }
          }
@@ -1453,7 +1453,7 @@ class Ticket {
             foreach( $recipients as $k=>$staff){
                 if(!$staff || !$staff->getEmail() || !$staff->isAvailable() || in_array($staff->getEmail(), $sentlist)) continue;
                 $alert = str_replace('%{recipient}', $staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, null, array('bulk' => true));
                 $sentlist[] = $staff->getEmail();
             }
         }
@@ -1503,7 +1503,7 @@ class Ticket {
                 $msg['body'] ="\n$tag\n\n".$msg['body'];
 
             $attachments =($cfg->emailAttachments() && $files)?$this->getAttachments($respId, 'R'):array();
-            $email->send($this->getEmail(), $msg['subj'], $msg['body'], $attachments);
+            $email->send($this->getEmail(), $msg['subj'], $msg['body'], $attachments, array('autoreply' => true));
         }
 
         return $respId;
@@ -1717,7 +1717,7 @@ class Ticket {
                 if(!$staff || !is_object($staff) || !$staff->getEmail() || !$staff->isAvailable()) continue;
                 if(in_array($staff->getEmail(),$sentlist) || ($staffId && $staffId==$staff->getId())) continue; 
                 $alert = str_replace('%{recipient}',$staff->getFirstName(), $msg['body']);
-                $email->send($staff->getEmail(), $msg['subj'], $alert, $attachments);
+                $email->send($staff->getEmail(), $msg['subj'], $alert, $attachments, array('bulk' => true));
                 $sentlist[] = $staff->getEmail();
             }
         }
@@ -2312,7 +2312,7 @@ class Ticket {
                 $msg['body'] ="\n$tag\n\n".$msg['body'];
 
             $attachments =($cfg->emailAttachments() && $respId)?$ticket->getAttachments($respId,'R'):array();
-            $email->send($ticket->getEmail(), $msg['subj'], $msg['body'], $attachments);
+            $email->send($ticket->getEmail(), $msg['subj'], $msg['body'], $attachments, array('bulk' => true));
         }
 
         return $ticket;
diff --git a/include/class.upgrader.php b/include/class.upgrader.php
index 012f88d11..265ae7c9a 100644
--- a/include/class.upgrader.php
+++ b/include/class.upgrader.php
@@ -67,7 +67,7 @@ class Upgrader extends SetupWizard {
 
         $subject = 'Upgrader Error';
         if($email) {
-            $email->send($thisstaff->getEmail(), $subject, $error);
+            $email->send($thisstaff->getEmail(), $subject, $error, null, array('bulk' => true));
         } else {//no luck - try the system mail.
             Mailer::sendmail($thisstaff->getEmail(), $subject, $error, sprintf('"osTicket Alerts"<%s>', $thisstaff->getEmail()));
         }
-- 
GitLab