Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved fMailbox Bounce Message Handling #153

Closed
wbond opened this issue Sep 12, 2012 · 0 comments
Closed

Improved fMailbox Bounce Message Handling #153

wbond opened this issue Sep 12, 2012 · 0 comments

Comments

@wbond
Copy link
Member

wbond commented Sep 12, 2012

The following patch provides improved bounce message parsing for fMailbox.

diff --git a/fMailbox.php b/fMailbox.php
index c223ec6..c1b4f0c 100644
--- a/fMailbox.php
+++ b/fMailbox.php
@@ -489,7 +489,11 @@ class fMailbox
     */
    static private function parseHeaders($headers, $filter=NULL)
    {
-       $header_lines = preg_split("#\r\n(?!\s)#", trim($headers));
+       $headers = trim($headers);
+       if (!strlen($headers)) {
+           return array();
+       }
+       $header_lines = preg_split("#\r\n(?!\s)#", $headers);

        $single_email_fields    = array('from', 'sender', 'reply-to');
        $multi_email_fields     = array('to', 'cc');
@@ -704,7 +708,7 @@ class fMailbox
    static private function parseStructure($data, $headers=NULL)
    {
        if (!$headers) {
-           list ($headers, $data) = explode("\r\n\r\n", $data, 2);
+           list ($headers, $data) = preg_split("#^\r\n|\r\n\r\n#", $data, 2);
            $headers = self::parseHeaders($headers);
        }

@@ -1137,6 +1141,54 @@ class fMailbox
    {
        $this->connect();

+       $source = $this->fetchMessageSource($uid);
+           
+       $info = self::parseMessage($source, $convert_newlines);
+       $info['uid'] = $uid;
+       
+       return $info;
+   }
+
+
+   /**
+    * Retrieves a single message from the server
+    * 
+    * The output includes the following keys:
+    * 
+    *  - `'uid'`: The UID of the message
+    *  - `'received'`: The date the message was received by the server
+    *  - `'headers'`: An associative array of mail headers, the keys are the header names, in lowercase
+    * 
+    * And one or more of the following:
+    * 
+    *  - `'text'`: The plaintext body
+    *  - `'html'`: The HTML body
+    *  - `'attachment'`: An array of attachments, each containing:
+    *   - `'filename'`: The name of the file
+    *   - `'mimetype'`: The mimetype of the file
+    *   - `'data'`: The raw contents of the file
+    *  - `'inline'`: An array of inline files, each containing:
+    *   - `'filename'`: The name of the file
+    *   - `'mimetype'`: The mimetype of the file
+    *   - `'data'`: The raw contents of the file
+    *  - `'related'`: An associative array of related files, such as embedded images, with the key `'cid:{content-id}'` and an array value containing:
+    *   - `'mimetype'`: The mimetype of the file
+    *   - `'data'`: The raw contents of the file
+    *  - `'verified'`: If the message contents were verified via an S/MIME certificate - if not verified the smime.p7s will be listed as an attachment
+    *  - `'decrypted'`: If the message contents were decrypted via an S/MIME private key - if not decrypted the smime.p7m will be listed as an attachment
+    * 
+    * All values in `headers`, `text` and `body` will have been decoded to
+    * UTF-8. Files in the `attachment`, `inline` and `related` array will all
+    * retain their original encodings.
+    * 
+    * @param  integer $uid               The UID of the message to retrieve
+    * @param  boolean $convert_newlines  If `\r\n` should be converted to `\n` in the `text` and `html` parts the message
+    * @return array  The parsed email message - see method description for details
+    */
+   public function fetchMessageSource($uid)
+   {
+       $this->connect();
+       
        if ($this->type == 'imap') {
            $response = $this->write('UID FETCH ' . $uid . ' (BODY[])');
            preg_match('#\{(\d+)\}$#', $response[0], $match);
@@ -1151,19 +1203,15 @@ class fMailbox
                }
            }

-           $info = self::parseMessage($message, $convert_newlines);
-           $info['uid'] = $uid;
+           return $message;

        } elseif ($this->type == 'pop3') {
            $response = $this->write('RETR ' . $uid);
            array_shift($response);
            $response = join("\r\n", $response);

-           $info = self::parseMessage($response, $convert_newlines);
-           $info['uid'] = $uid;
+           return $response;
        }
-       
-       return $info;
    }

@wbond wbond closed this as completed in 3802067 Sep 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant