From 431a16129f8e31cb888f8c45ab5ed8cc93b08add Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 9 Oct 2013 17:36:25 +0000
Subject: [PATCH] Don't handle text email messages like HTML

This patch changes the behavior of the mailer to support a 'text' option to
hint if the message is a text-only message. If so, no HTML processing will
be performed on the message and a text-only email (with no inline
attachments) will be emitted and sent.
---
 include/class.mailer.php   | 19 +++++++++++++++----
 include/class.osticket.php |  2 +-
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/include/class.mailer.php b/include/class.mailer.php
index 889ecb39f..e13eff86e 100644
--- a/include/class.mailer.php
+++ b/include/class.mailer.php
@@ -136,12 +136,23 @@ class Mailer {
 
         $mime = new Mail_mime();
 
-        // Make sure nothing unsafe has creeped into the message
-        $message = Format::safe_html($message);
-        $mime->setTXTBody(convert_html_to_text($message));
+        $isHtml = true;
+        // Ensure that the 'text' option / hint is not set to true and that
+        // the message appears to be HTML -- that is, the first
+        // non-whitespace char is a '<' character
+        if (!(isset($options['text']) && $options['text'])
+                && preg_match('/^\s*</', $message)) {
+            // Make sure nothing unsafe has creeped into the message
+            $message = Format::safe_html($message);
+            $mime->setTXTBody(convert_html_to_text($message));
+        }
+        else {
+            $mime->setTXTBody($message);
+            $isHtml = false;
+        }
 
         $domain = 'local';
-        if ($ost->getConfig()->isHtmlThreadEnabled()) {
+        if ($isHtml && $ost->getConfig()->isHtmlThreadEnabled()) {
             // TODO: Lookup helpdesk domain
             $domain = substr(md5($ost->getConfig()->getURL()), -12);
             // Format content-ids with the domain, and add the inline images
diff --git a/include/class.osticket.php b/include/class.osticket.php
index a2d2205de..fd46d2134 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -232,7 +232,7 @@ class osTicket {
             $email=$this->getConfig()->getDefaultEmail(); //will take the default email.
 
         if($email) {
-            $email->sendAlert($to, $subject, $message);
+            $email->sendAlert($to, $subject, $message, null, array('text'=>true));
         } else {//no luck - try the system mail.
             Mailer::sendmail($to, $subject, $message, sprintf('"osTicket Alerts"<%s>',$to));
         }
-- 
GitLab