From ff27fb2cbb287db47e76cdb17565b9467dba70fa Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 27 Mar 2015 11:22:55 -0500
Subject: [PATCH] email: Some bounce notices actually have rich content

Here's an example email structure:
```
multipart/mixed
 - multipart/report; delivery-status
   - multipart/alternative
     - text/plain
     - text/html
   - message/delivery-status
   - message/rfc822
```

The previous code would only find the body if the email main headers had:
Content-Type: multipart/report; report-type="delivery-status". In such a
case it would scan for a plain/text body.

This patch will scan for the usual body if the scan for the body as usual if
the report scan did not find anything.

Also, output errors to stderr when running API from the command line
---
 include/class.api.php       | 7 ++++++-
 include/class.mailfetch.php | 2 +-
 include/class.mailparse.php | 2 +-
 3 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/include/class.api.php b/include/class.api.php
index 082cc178d..fef6ddb47 100644
--- a/include/class.api.php
+++ b/include/class.api.php
@@ -297,7 +297,12 @@ class ApiController {
             $msg.="\n*[".$_SERVER['HTTP_X_API_KEY']."]*\n";
         $ost->logWarning(__('API Error')." ($code)", $msg, false);
 
-        $this->response($code, $error); //Responder should exit...
+        if (PHP_SAPI == 'cli') {
+            fwrite(STDERR, "({$code}) $error\n");
+        }
+        else {
+            $this->response($code, $error); //Responder should exit...
+        }
         return false;
     }
 
diff --git a/include/class.mailfetch.php b/include/class.mailfetch.php
index 363589b46..1882e2e76 100644
--- a/include/class.mailfetch.php
+++ b/include/class.mailfetch.php
@@ -642,7 +642,7 @@ class MailFetcher {
                 $vars['in-reply-to'] = @$headers['in-reply-to'] ?: null;
             }
             // Fetch deliver status report
-            $vars['message'] = $this->getDeliveryStatusMessage($mid);
+            $data['message'] = $this->getDeliveryStatusMessage($mid) ?: $this->getBody($mid);
             $vars['thread-type'] = 'N';
             $vars['flags']['bounce'] = true;
         }
diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 2a91faf7f..4b8bcde81 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -657,7 +657,7 @@ class EmailDataParser {
                 $data['in-reply-to'] = @$headers['in-reply-to'] ?: null;
             }
             // Fetch deliver status report
-            $data['message'] = $parser->getDeliveryStatusMessage();
+            $data['message'] = $parser->getDeliveryStatusMessage() ?: $parser->getBody();
             $data['thread-type'] = 'N';
             $data['flags']['bounce'] = true;
         }
-- 
GitLab