vaibhavpandeyvpz / phemail
MIME parser written in pure PHP for parsing raw emails (.eml) files.
Installs: 75 956
Dependents: 1
Suggesters: 0
Security: 0
Stars: 32
Watchers: 2
Forks: 8
Open Issues: 2
README
MIME parser written in pure PHP for parsing raw emails (.eml) files.
Install
composer require vaibhavpandeyvpz/phemail
Usage
Suppose this is your email file named sample.eml
:
Mime-Version: 1.0 Message-Id: <6B7EC235-5B17-4CA8-B2B8-39290DEB43A3@vaibhavpandey.com> From: Vaibhav Pandey <contact@vaibhavpandey.com> To: Vaibhav Pandey <me@vaibhavpandey.com> Subject: Testing simple email Date: Sat, 22 Nov 2008 15:04:59 +1100 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit This is simple as f*** plain text email message. Regards, Vaibhav Pandey
You can read & parse it as follows:
<?php $parser = new Phemail\MessageParser(); $message = $parser->parse(__DIR__ . '/sample.eml'); echo $message->getHeaderValue('subject'); # outputs 'Testing simple email' echo $message->getHeaderValue('date'); # outputs 'Sat, 22 Nov 2008 15:04:59 +1100' echo $message->getHeaderValue('content-type'); # outputs 'text/plain' echo $message->getHeaderAttribute('content-type', 'charset'); # outputs 'US-ASCII' echo $message->getContents(); /** * @desc To extract emails from headers, you could use any RFC 822 * internet address parser e.g., pear/mail. */ $addresses = (new Mail_RFC822())->parseAddressList($message->getHeaderValue('to')); foreach ($addresses as $address) { echo 'Name: ', $address->personal, '<br>', 'Email: ', $address->mailbox, '@', $address->host; }
License
See LICENSE.md file.