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

1.3 2023-11-14 06:32 UTC

This package is auto-updated.

Last update: 2024-11-14 08:48:23 UTC


README

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

Latest Version 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: <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.