Home > Programmering > PHP script to read attachements in mails

PHP script to read attachements in mails


$debug = 0;  // 0 to disable;

//script will fetch an email identified by $msgid, and parse the its parts into an
//array $partsarray
//structure of array:
//$partsarray[<name of part>][<attachment/text>]
//if attachment- subarray is [filename][binary data]
//if text- subarray is [type of text(HTML/PLAIN)][text string]
//$partsarray[3.1][attachment][filename]=filename of attachment in part 3.1
//$partsarray[3.1][attachment][binary]=binary data of attachment in part 3.1
//$partsarray[2][text][type]=type of text in part 2
//$partsarray[2][text][string]=decoded text string in part 2
//$partsarray[not multipart][text][string]=decoded text string in message that isn't multipart
$domain = 'example.com';
$userpart = 'mailboxname';
$password = 'mysecret';
$server = 'mail.example.com';
$username = $userpart.'@'.$domain;
function parsepart($p,$i){
        global $tck,$link,$msgid,$partsarray,$db,$hdr;
        //where to write file attachments to:
        $filestore = '[full/path/to/attachment/store/(chmod777)]';
        //fetch part

        //if type is not text
        if ($p->type!=0){
                //DECODE PART
                //decode if base64
                if ($p->encoding==3)$part=base64_decode($part);
                //decode if quoted printable
                if ($p->encoding==4)$part=quoted_printable_decode($part);
                //no need to decode binary or 8bit!

                //get filename of attachment if present
                // if there are any dparameters present in this part
                if (count($p->dparameters)>0){
                        foreach ($p->dparameters as $dparam){
                                if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;
                //if no filename found
                if ($filename==''){
                        // if there are any parameters present in this part
                        if (count($p->parameters)>0){
                                foreach ($p->parameters as $param){
                                        if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;
                //write to disk and set partsarray variable
                if ($filename!=''){
                        $partsarray[$i][attachment] = array('filename'=>$filename,'binary'=>$part);
                        if (!file_exists($filestore.$tck)) system("mkdir $filestore".$tck);
                        $hdr .= "Attachment: $filename<br />\n";
        } //end if type!=0

        //if part is text
        else if($p->type==0){
                //decode text
                //if QUOTED-PRINTABLE
                if ($p->encoding==4) $part=quoted_printable_decode($part);
                //if base 64
                if ($p->encoding==3) $part=base64_decode($part);

                //OPTIONAL PROCESSING e.g. nl2br for plain text
                //if plain text
                if (strtoupper($p->subtype)=='PLAIN') $plain=$part;
                //if HTML
                else if (strtoupper($p->subtype)=='HTML') $html=$part;
                $partsarray[$i][text] = array('type'=>$p->subtype,'string'=>$part);

        //if subparts... recurse into function and parse them too!
        if (count($p->parts)>0){
                foreach ($p->parts as $pno=>$parr){
                        list($pln,$htm)= parsepart($parr,($i.'.'.($pno+1)));
                        $plain .= $pln;
                        $html .= $htm;
        return array($plain,$html);

function getUser($fromObj) {
        $user = $fromObj[0]->mailbox . "@" . $fromObj[0]->host;
        return $user;

//open resource
if ($debug) echo "Opening mailbox\n";

$num = imap_num_msg($link);

if ($debug) echo "Looking through $num messages\n";

for ($msgid=1; $msgid<=$num; $msgid++) {

 if ($debug)   echo ".";
  $header = imap_headerinfo($link,$msgid);
  if ($debug>1) var_dump($header);

  $hdr = "From: ".$header->fromaddress."<br />\n";
  if ($header->fromaddress<>$header->senderaddress)  $hdr .= "Sender: ".$header->senderaddress."<br />\n";
  if ($header->fromaddress<>$header->reply_toaddress)  $hdr .= "ReplyTo: ".$header->reply_toaddress."<br />\n";
  $hdr .= "Subject: ".$header->subject."<br />\n";
  $hdr .= "Date: ".$header->date."<hr />\n";

  $html = $plain = "";

  if ($match=1) { //process


    //fetch structure of message
    //see if there are any parts
    if (count($s->parts)>0){
        foreach ($s->parts as $partno=>$partarr){
                //parse parts of email
                list($p,$h) = parsepart($partarr,$partno+1);
                $plain .= $p;
                $html .= $h;
    //for not multipart messages
        //get body of message
        //decode if quoted-printable
        if ($s->encoding==4) $text=quoted_printable_decode($text);
        if (strtoupper($s->subtype)=='PLAIN') $plain=$text;
        if (strtoupper($s->subtype)=='HTML') $html=$text;

        $partsarray['not multipart'][text]=array('type'=>$s->subtype,'string'=>$text);
        $lines = explode("\n",$text);
        foreach ($lines as $line) {
        print $text;
    if ($debug>2)  print_r($partsarray);
    $body = $html ? $html : $plain;
    $cmt = addslashes($hdr.$body);
    $usr = getUser($head->from);

Categories: Programmering Tags:
  1. No comments yet.
  1. No trackbacks yet.