How to parse a .eml file in php? Is there any PHP libriary or PHP extension ?
I want to display the mail header information such as sender, receiver, title, attachement and eml body content in browser.
How to parse a .eml file in php? Is there any PHP libriary or PHP extension ?
I want to display the mail header information such as sender, receiver, title, attachement and eml body content in browser.
There are a couple of ways to do it. One way is to simply do it yourself, it's not that complicated.
Otherwise, you might want to have a look at the Mailparse library:
http://php.net/manual/en/book.mailparse.php
And there is also this one:
This is what I use:
composer require php-mime-mail-parser/php-mime-mail-parser
And then PHP:
$parser = new \PhpMimeMailParser\Parser();
$emailFile = 'myEmailFile.eml';
$parser->setText(file_get_contents($emailFile));
Then, to get addresses:
$toAddressesQ = $parser->getAddresses('to');
Or the body:
$text = $parser->getMessageBody('text');
$html = $parser->getMessageBody('html');
Or the header:
$subject = $parser->getHeader('subject');
Or the attachments:
$attachments = $parser->getAttachments();