All Projects → vaibhavpandeyvpz → phemail

vaibhavpandeyvpz / phemail

Licence: MIT License
MIME parser written in pure PHP for parsing raw emails (.eml) files.

Programming Languages

PHP
23972 projects - #3 most used programming language

vaibhavpandeyvpz/phemail

MIME parser written in pure PHP for parsing raw emails (.eml) files.

Latest Version Build Status SensioLabsInsight Scrutinizer Code Quality Coverage Status Total Downloads Software License

Install

composer require vaibhavpandeyvpz/phemail

Usage

Suppose this is your email file named sample.eml:

Mime-Version: 1.0
Message-Id: <[email protected]>
From: Vaibhav Pandey <[email protected]>
To: Vaibhav Pandey <[email protected]>
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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].