How to extract email attack using PHP?

I retrieve the letters from the database, where they are stored as strings. I need to parse these letters in order to extract their attachments. I assume that perhaps a library is enough for this, but I cannot find it.

+3
source share
6 answers

PEAR :: Mail :: mimeDecode should do what you are looking for

+3
source

PHP has the MailParse extension, which is much faster than using the PEAR alternative, which is native PHP.

Here is the library that wraps this extension:

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

Example:

// require mime parser library require_once('MimeMailParser.class.php'); // instantiate the mime parser $Parser = new MimeMailParser(); // set the email text for parsing $Parser->setText($text); // get attachments $attachments = $Parser->getAttachments(); 
+3
source

This can be done using the Zend_Mail component in the Zend Framework.

Perhaps this example, which can also be found in the documentation, helps:

 // get the first none multipart part $part = $message; while ($part->isMultipart()) { $part = $message->getPart(1); } echo 'Type of this part is ' . strtok($part->contentType, ';') . "\n"; echo "Content:\n"; echo $part->getContent(); 

I donโ€™t know how you can tell Zend Mail to read the lines, maybe there is some kind of work necessary for this, but then you will have a fully functional library that will do what you want, and a few more topics reading, etc. d.).

Edit

I only have a second look at it and realized that all you need to do is write your own storage implementation (subclass Zend_Mail_Storage_Abstract), which is not so difficult to do.

I think the cleanest solution you will get, although it takes a bit of effort to make it work.

If you are looking for a faster โ€œdirtyโ€ solution, someone can help you.

Hope this helps.

+1
source

PhpMimeParser - parsing the mime multiplayer message (attachments, embedded images, base64, quotation marks) https://github.com/breakermind/PhpMimeParser You can cut mime messages from files, a string.

 // Load .eml mime message from file $str = file_get_contents('mime-mixed-related-alternative.eml'); // Format output echo "<pre>"; // Create object MimeParser $m = new PhpMimeParser($str); // Show Emails print_r($m->mTo); print_r($m->mFrom); print_r($m->mBcc); print_r($m->mCc); // Show Message echo $m->mSubject; echo $m->mHtml; echo $m->mText; print_r($m->mInlineList); // Show Files print_r($m->mFiles); 
+1
source

Email attachments are MIME encoded and are added to the message body using headers. The PEAR MIME decoding package will do what you need: http://pear.php.net/package/Mail_mimeDecode

0
source

There is a better library there: https://github.com/php-mime-mail-parser/php-mime-mail-parser

It is installed through Composer.

Since this has the same name as the Google Code that was associated with other places in this SO post, I consider it to be the successor, but I cannot say. Authorship information is more difficult to find in Google Code, so I cannot confirm it with the same author.

Sample code (from the README project):

 // Include the library first require_once __DIR__.'/vendor/autoload.php'; $path = 'path/to/mail.txt'; $Parser = new PhpMimeMailParser\Parser(); $Parser->setStream(fopen($path, "r")); // Loop through all the Attachments if (count($attachments) > 0) { foreach ($attachments as $attachment) { echo 'Filename : '.$attachment->getFilename().'<br />'; // logo.jpg echo 'Filesize : '.filesize($attach_dir.$attachment->getFilename()).'<br />'; // 1000 echo 'Filetype : '.$attachment->getContentType().'<br />'; // image/jpeg echo 'MIME part string : '.$attachment->getMimePartStr().'<br />'; // (the whole MIME part of the attachment) } } 
0
source

All Articles