PHP Classes

MIME mail parser: Parse and extract contents of MIME email message

Recommend this page to a friend!
  Info   View files Documentation   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum (1)   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 56%Total: 404 All time: 6,513 This week: 105Up
Version License PHP version Categories
mime-mail-parser 0.9.20Custom (specified...5Email, PHP 5, Parsers
Description 

Author

This class can parse and extract contents of MIME email message.

It takes a string with complete email messages with headers and body and parses it to extract its details.

Currently it can return the message types of each part of the message, the message headers, and the content and headers of each part.

Picture of Peter Kahl
  Performance   Level  
Name: Peter Kahl <contact>
Classes: 37 packages by
Country: United Kingdom
Age: ???
All time rank: 41721 in United Kingdom
Week rank: 91 Up3 in United Kingdom Up
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Documentation

MIME Mail Parser

Downloads Download per Month License If this project has business value for you then don't hesitate to support me with a small donation.

Lightweight parser of MIME and non-MIME mails.

Usage Example

use peterkahl\MIMEmailParser\MIMEmailParser;

$str = 'To: =?utf-8?B?6Zmz5aSn5YGJ?= <nobody@nowhere>
Subject: =?utf-8?B?6JiL5p6c?=
Date: Sun, 16 Oct 2016 14:18:13 +0000
Message-Id: <58FEFDGKQ.1F9JAF7X8CFNO@everywhere>
From: "Mao \"Chairman\" =?utf-8?B?5q+b5rO95Lic?=" (office) <mao@backintime.sample>
MIME-Version: 1.0
Content-Type: multipart/alternative;
   boundary="__13DC118A:67185A22ABDB3C963911BA465620407F8721AD92__"

--__13DC118A:67185A22ABDB3C963911BA465620407F8721AD92__
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: base64
Content-Language: zh-tw
Content-Disposition: inline

6JiL5p6c5qi55piv6JC96JGJ5Zas5pyo77yM6YCa5bi45qi55pyo5Y+v6auY6IezMTXnsbPvvIzk
vYbmoL3ln7nmqLnmnKjkuIDoiKzlj6rpq5gzLTXnsbPlt6blj7PjgILmqLnlubnlkYjngbDopJDo
ibLvvIzmqLnnmq7mnInkuIDlrprnqIvluqbnmoTohKvokL3jgILmnpzlr6bkuIDoiKzlkYjntIXo
ibLvvIzkvYbpnIDoppbkuY7lk4HnqK7ogIzlrprjgIIKCQkK6JiL5p6c6ZaL6Iqx5pyf5piv5Z+6
5pa85ZCE5Zyw5rCj5YCZ6ICM5a6a77yM5L2G5LiA6Iis6ZuG5Lit5ZyoNC015pyI5Lu944CC6JiL
5p6c5piv55Ww6Iqx5o6I57KJ5qSN54mp77yM5aSn6YOo5YiG5ZOB56iu6Ieq6Iqx5LiN6IO957WQ
5oiQ5p6c5a+m44CC

--__13DC118A:67185A22ABDB3C963911BA465620407F8721AD92__
Content-Type: multipart/related;
   boundary="__1FBAC6BE:67185A22ABDB3C963911BA465620407F8721AD92__"

--__1FBAC6BE:67185A22ABDB3C963911BA465620407F8721AD92__
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: base64
Content-Language: zh-tw
Content-Disposition: inline

PGJvZHkgbWFyZ2lud2lkdGg9IjAiIG1hcmdpbmhlaWdodD0iMCIgbGVmdG1hcmdpbj0iMCIgdG9w
bWFyZ2luPSIwIiBzdHlsZT0iYmFja2dyb3VuZC1jb2xvcjojNGRkZGIxOyI+PG1ldGEgY2hhcnNl
dD0idXRmLTgiPjx0YWJsZSB3aWR0aD0iMTAwJSIgY2VsbHBhZGRpbmc9IjAiIGNlbGxzcGFjaW5n
PSIwIiBib3JkZXI9IjAiIGFsaWduPSJjZW50ZXIiIGJnY29sb3I9IiM0ZGRkYjEiPjx0cj48dGQg
d2lkdGg9IjEwMCUiIHZhbGlnbj0idG9wIiBhbGlnbj0iY2VudGVyIj48dGFibGUgd2lkdGg9IjYw
MCIgY2VsbHBhZGRpbmc9IjAiIGNlbGxzcGFjaW5nPSIwIiBhbGlnbj0iY2VudGVyIiBib3JkZXI9
IjAiIGNsYXNzPSJ3cmFwcGVyIiBiZ2NvbG9yPSIjNDY0ZjU4Ij48dGJvZHk+PHRyPjx0ZCBhbGln
bj0iY2VudGVyIj48dGFibGUgd2lkdGg9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C90YWJsZT48L2JvZHk+

--__1FBAC6BE:67185A22ABDB3C963911BA465620407F8721AD92__--

--__13DC118A:67185A22ABDB3C963911BA465620407F8721AD92__--

';

$mpars = new MIMEmailParser;
$mpars->parse_messageString($str);

if (!empty($mpars->messageType)) {
  echo 'This message contains the following parts:'. PHP_EOL . PHP_EOL;
  echo implode(', ', $mpars->messageType). PHP_EOL . PHP_EOL;
  echo '====================================================================' . PHP_EOL . PHP_EOL;
}

if (!empty($mpars->headers)) {
  echo 'I found these top-most headers:'. PHP_EOL . PHP_EOL;
  foreach ($mpars->headers as $hdrKey => $hdrVal) {
    echo '"'. $hdrKey .'" => "'. $mpars->decodeRFC2047($hdrVal) .'"'. PHP_EOL;
  }
  echo '====================================================================' . PHP_EOL . PHP_EOL;
}

if (!empty($mpars->content)) {
  foreach ($mpars->content as $partKey => $partVal) {
    if (substr($partVal['content-type'], 0, 10) == 'text/plain' && !empty($partVal['content'])) {
      echo 'Body in plain text:'. PHP_EOL . PHP_EOL . $partVal['content'] . PHP_EOL . PHP_EOL;
      if (!empty($partVal['content-language'])) {
        echo 'The above string\'s language ........ '. $partVal['content-language'] . PHP_EOL . PHP_EOL;
        echo '====================================================================' . PHP_EOL . PHP_EOL;
      }
    }
    if (substr($partVal['content-type'], 0, 9) == 'text/html' && !empty($partVal['content'])) {
      echo 'Body in HTML text:'. PHP_EOL . PHP_EOL . $partVal['content'] . PHP_EOL . PHP_EOL;
      if (!empty($partVal['content-language'])) {
        echo 'The above string\'s language ........ '. $partVal['content-language'] . PHP_EOL . PHP_EOL;
        echo '====================================================================' . PHP_EOL . PHP_EOL;
      }
    }
  }
}
else {
  die('Parsing failed. Something must be wrong with the mail string.');
}

The above example produces:

This message contains the following parts:

multipart/alternative, text/plain, multipart/related, text/html

====================================================================

I found these top-most headers:

"content-type" => "multipart/alternative; boundary="__13DC118A:67185A22ABDB3C963911BA465620407F8721AD92__""
"mime-version" => "1.0"
"from" => ""Mao \"Chairman\" ???" (office) <mao@backintime.sample>"
"message-id" => "<58FEFDGKQ.1F9JAF7X8CFNO@everywhere>"
"date" => "Sun, 16 Oct 2016 14:18:13 +0000"
"subject" => "??"
"to" => "??? <nobody@nowhere>"
====================================================================

Body in plain text:

????????????????15???????????3-5???????????????????????????????????????

?????????????????????4-5???????????????????????????

The above string's language ........ zh-tw

====================================================================

<body marginwidth="0" marginheight="0" leftmargin="0" topmargin="0" style="background-color:#4dddb1;"><meta charset="utf-8"><table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" bgcolor="#4dddb1"><tr><td width="100%" valign="top" align="center"><table width="600" cellpadding="0" cellspacing="0" align="center" border="0" class="wrapper" bgcolor="#464f58"><tbody><tr><td align="center"><table width="600" cellpadding="0" cellspacing="0" align="center" border="0" class="container"><tr><td height="20" style="line-height:20px; font-size:20px;"> </td></tr><tr><td align="center" style="font-family: Calibri,sans-serif; font-size: 26px; line-height: 26px; color: #ffffff; font-weight: bold; padding: 0 20px;"><p>????????????????15???????????3-5???????????????????????????????????????</p><p>?????????????????????4-5???????????????????????????</p></td></tr><tr><td height="20" style="line-height:20px; font-size:20px;"> </td></tr></table></td></tr></tbody></table></td></tr></table></body>

The above string's language ........ zh-tw

====================================================================



  Files folder image Files  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file MIMEmailParser.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:404
This week:0
All time:6,513
This week:105Up
User Ratings User Comments (1)
 All time
Utility:81%StarStarStarStarStar
Consistency:81%StarStarStarStarStar
Documentation:81%StarStarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:56%StarStarStar
Rank:1719
 
nice
6 years ago (muabshir)
70%StarStarStarStar