Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a .eml file in php?

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.

like image 802
maarten Avatar asked Sep 05 '25 03:09

maarten


2 Answers

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();
like image 85
coderama Avatar answered Sep 07 '25 21:09

coderama


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:

http://code.google.com/p/php-mime-mail-parser/

like image 33
Doa Avatar answered Sep 07 '25 19:09

Doa