Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract mail atachment with PHP?

Tags:

php

email

parsing

I'm extracting emails from a database where they're stored as strings. I need to parse these emails to extract their attachments. I guess there must already be some library to do this easily but I can't find any.

like image 970
Arkh Avatar asked Jan 23 '23 08:01

Arkh


2 Answers

PHP has a MailParse extension that is a lot faster then using the PEAR alternative which is native PHP.

Here is a 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();
like image 93
bucabay Avatar answered Jan 30 '23 02:01

bucabay


PEAR::Mail::mimeDecode should do what you're looking for

like image 43
Mez Avatar answered Jan 30 '23 01:01

Mez