- Saved searches
- Use saved searches to filter your results more quickly
- License
- optimumweb/php-email-reader-parser
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- Mailparse
- Saved searches
- Use saved searches to filter your results more quickly
- License
- willdurand/EmailReplyParser
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- License
- zbateson/mail-mime-parser
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
PHP Email Reader (using IMAP) and Parser (using mimeDecode)
License
optimumweb/php-email-reader-parser
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
It does something that should be so simple but turns out to be very complex using PHP: reading emails from a mailbox using IMAP and then parsing the returned emails into a dev-friendly object.
- PHP 5.4+
- PHP PEAR
- PHP IMAP extension
- PHP mbstring extension (mb_convert_encoding)
Simply require both classes (Email_Reader and Email_Parser) inside your code.
require_once('PATH_TO_FILES/email_reader.php'); require_once('PATH_TO_FILES/email_parser.php'); ?>
- Note that you can use the Email_Parser class by itself without the Email_Reader class, but not the other way around.
Email_Reader is used to open an IMAP stream to a mailbox and then fetch messages.
$reader = new Email_Reader($mailbox, $username, $password); $messages = $reader->get_messages(); $unread = $reader->get_unread(); ?>
Email_Parser is used within Email_Reader to be able to decode the returned emails. It can also be used by itself to read emails, for example, from php://std in the case of email piping.
#! /usr/bin/php -q $fd = fopen("php://stdin", "r"); $raw = ""; while ( !feof($fd) ) < $raw .= fread($fd, 1024); > fclose($fd); $email = new Email_Parser($raw); doWhaterever($email->from, $email->subject, $email->body); ?>
In order to write these classes, I had a lot of reading of other people’s work and I would like to thank the following repository owners.
Jonathan Roy, web developper at OptimumWeb
Mailparse
/* parse the message and return a mime message resource */
$mime = mailparse_msg_parse_file ( $filename );
/* return an array of message parts — this contsists of the names of the parts
* only */
$struct = mailparse_msg_get_structure ( $mime );
echo «
$st | » . $info [ «content-type» ] . « | » . $info [ «content-disposition» ] . « | » . $info [ «disposition-filename» ] . « | » . $info [ «charset» ] . « |
» ;
/* if we were called to display a part, do so now */
if ( $showpart ) <
/* get a handle on the message resource for the desired part */
$sec = mailparse_msg_get_part ( $mime , $showpart );
echo «
Section $showpart |
---|
» ; ob_start (); /* extract the part from the message file and dump it to the output buff er * */ mailparse_msg_extract_part_file ( $sec , $filename ); $contents = ob_get_contents (); ob_end_clean (); /* quote the message for safe display in a browser */ echo nl2br ( htmlentities ( $contents )) . « |
» ;;
>
?>
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
PHP library for parsing plain text email content.
License
willdurand/EmailReplyParser
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
EmailReplyParser is a PHP library for parsing plain text email content, based on GitHub’s email_reply_parser library written in Ruby.
The recommended way to install EmailReplyParser is through Composer:
composer require willdurand/email-reply-parser
Instantiate an EmailParser object and parse your email:
use EmailReplyParser\Parser\EmailParser; $email = (new EmailParser())->parse($emailContent);
You get an Email object that contains a set of Fragment objects. The Email class exposes two methods:
- getFragments() : returns all fragments;
- getVisibleText() : returns a string which represents the content considered as «visible».
The Fragment represents a part of the full email content, and has the following API:
$fragment = current($email->getFragments()); $fragment->getContent(); $fragment->isSignature(); $fragment->isQuoted(); $fragment->isHidden(); $fragment->isEmpty();
Alternatively, you can rely on the EmailReplyParser to either parse an email or get its visible content in a single line of code:
$email = \EmailReplyParser\EmailReplyParser::read($emailContent); $visibleText = \EmailReplyParser\EmailReplyParser::parseReply($emailContent);
Quoted headers aren’t picked up if there’s an extra line break:
Also, they’re not picked up if the email client breaks it up into multiple lines. GMail breaks up any lines over 80 characters for you.
The above On . wrote: can be cleaned up with the following regex:
$fragment_without_date_author = preg_replace( '/\nOn(.*?)wrote:(.*?)$/si', "", $fragment->getContent() );
Note though that we’re search for «on» and «wrote». Therefore, it won’t work with other languages.
Possible solution: Remove «reply@reply.github.com» lines.
Lines starting with — or _ sometimes mark the beginning of signatures:
Not everyone follows this convention:
Hello Mr Rick Olson Galactic President Superstar Mc Awesomeville GitHub **********************DISCLAIMER*********************************** * Note: blah blah blah * **********************DISCLAIMER***********************************
Apparently, prefixing lines with > isn’t universal either:
Hello -- Rick ________________________________________ From: Bob [reply@reply.github.com] Sent: Monday, March 14, 2011 6:16 PM To: Rick
Setup the test suite using Composer:
EmailReplyParser is released under the MIT License. See the bundled LICENSE file for details.
About
PHP library for parsing plain text email content.
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
An email parser written in PHP
License
zbateson/mail-mime-parser
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
Testable and PSR-compliant mail mime parser alternative to PHP’s imap* functions and Pear libraries for reading messages in Internet Message Format RFC 822 (and later revisions RFC 2822, RFC 5322).
The goals of this project are to be:
To include it for use in your project, install it via composer:
composer require zbateson/mail-mime-parser
If this project’s helped you, please consider sponsoring me.
As of mail-mime-parser 2.3.0, support for php 5.4, 5.5, 5.6 and 7.0 has been dropped.
getContentResourceHandle , getTextResourceHandle , and getHtmlResourceHandle have all been deprecated in 1.2.1 and removed in 2.0.0. fread() will only return a single byte of a multibyte char, and so will cause potentially unexpected results/warnings in some cases, and psr7 streams should be used instead. Note that getBinaryContentResourceHandle and getResourceHandle are still available.
Upgrade to 2.0 to take advantage of the new on-demand parser which parses parts of a message as they’re requested. This means reading only the headers from a larger message is as fast as a smaller message because the whole message is no longer parsed (similarly reading just the content and not a message’s large attachments is also much faster.)
Because of the on-demand parsing, starting in 2.0, the passed resource handle or stream must remain open while the returned message object is still in use.
$handle = fopen('file.mime', 'r'); $message = $mailParser->parse($handle); // returned `Message` fclose($handle);
// attaches the resource handle to the returned `IMessage` if the second parameter // is true. The resource handle is closed when the IMessage is destroyed. $message = $mailParser->parse(fopen('file.mime', 'r'), true);
For a more complete list of changes, please visit the 2.0 Upgrade Guide.
MailMimeParser requires PHP 7.1 or newer. Tested on PHP 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2.
use ZBateson\MailMimeParser\MailMimeParser; use ZBateson\MailMimeParser\Message; use ZBateson\MailMimeParser\Header\HeaderConsts; // use an instance of MailMimeParser as a class dependency $mailParser = new MailMimeParser(); // parse() accepts a string, resource or Psr7 StreamInterface // pass `true` as the second argument to attach the passed $handle and close // it when the returned IMessage is destroyed. $handle = fopen('file.mime', 'r'); $message = $mailParser->parse($handle, false); // returns `IMessage` // OR: use this procedurally (Message::from also accepts a string, // resource or Psr7 StreamInterface // true or false as second parameter doesn't matter if passing a string. $string pl-s">Content-Type: text/plain\r\nSubject: Test\r\n\r\nMessage"; $message = Message::from($string, false); echo $message->getHeaderValue(HeaderConsts::FROM); // user@example.com echo $message ->getHeader(HeaderConsts::FROM) // AddressHeader ->getPersonName(); // Person Name echo $message->getHeaderValue(HeaderConsts::SUBJECT); // The email's subject echo $message ->getHeader(HeaderConsts::TO) // also AddressHeader ->getAddresses()[0] // AddressPart ->getPersonName(); // Person Name echo $message ->getHeader(HeaderConsts::CC) // also AddressHeader ->getAddresses()[0] // AddressPart ->getEmail(); // user@example.com echo $message->getTextContent(); // or getHtmlContent() echo $message->getHeader('X-Foo'); // for custom or undocumented headers $att = $message->getAttachmentPart(0); // first attachment echo $att->getHeaderValue(HeaderConsts::CONTENT_TYPE); // e.g. "text/plain" echo $att->getHeaderParameter( // value of "charset" part 'content-type', 'charset' ); echo $att->getContent(); // get the attached file's contents $stream = $att->getContentStream(); // the file is decoded automatically $dest = \GuzzleHttp\Psr7\stream_for( fopen('my-file.ext') ); \GuzzleHttp\Psr7\copy_to_stream( $stream, $dest ); // OR: more simply if saving or copying to another stream $att->saveContent('my-file.ext'); // writes to my-file.ext $att->saveContent($stream); // copies to the stream // close only when $message is no longer being used. fclose($handle);