开发者

Any library to handle incoming email and parse it?

I'm wondering if there is any php class to handle pipe incoming mail and split headers, body, and split the headers into p开发者_如何学运维arts too to easily gather subject and stuffs.

Any recommandations?

Thanks


class HTMLParserIterator
{
    var $contents;
    var $pos=0;
    var $endPos=0;
    function HTMLParserIterator( $contents )
    {
        $this->contents=$contents;
        $this->pos=0;
        $this->endPos=strlen($contents);
    }
    function setPos( $pos )
    {
        $this->pos=$pos;
    }
    function getPos()
    {
        return $this->pos;
    }
    function hasMore()
    {
        return $this->pos <$this->endPos;
    }
    function getChar()
    {
        return $this->hasMore()?$this->contents[$this->pos++]:'';
    }
    function peek()
    {
        return $this->hasMore()?$this->contents[$this->pos]:'';
    }
    function skip($num=1 )
    {
        $this->pos=min($this->pos+$num,$this->endPos);
    }
    function getChars($num)
    {
        $out='';
        $len=min($num,$this->endPos-$this->pos);
        $out=substr($this->contents,$this->pos,$len);
        $this->pos+=$len;
        return $out;
    }
    function readUntil( $until,$ignoreCase=true )
    {
        $end=$ignoreCase?stripos ( $this->contents, $until ,$this->pos ):strpos ( $this->contents, $until ,$this->pos );
        if($end===false)
        {
            $end=$this->endPos;
        }
        $out=substr($this->contents,$this->pos,$end-$this->pos);
        $this->pos=$end;
        return $out;
    }
    function skipWhiteSpace()
    {
        $out=0;
        while( $this->hasMore() && isWhiteSpace( $this->contents[$this->pos] ) )
        {
            $out++;
            $this->pos++;
        }
        return $out;
    }
    function match( $str,$ignoreCase=true )
    {
        if( sWith($this->contents,$str,$ignoreCase,$this->pos ) )
        {
            $out=substr($this->contents,$this->pos,strlen($str));
            $this->pos+=strlen($str);
            return true;
        }
        return false;
    }
}
define("HTMLParserTag_CONTENT",0);
define("HTMLParserTag_OPEN",1);
define("HTMLParserTag_CLOSE",2);
define("HTMLParserTag_STANDALONE",3);
define("HTMLParserTag_COMMENT",4);
define("HTMLParserTag_EXTENDED_COMMENT",5);
define("HTMLParserTag_SCRIPT",6);
define("HTMLParserTag_STYLE",7);
define("HTMLParserTag_TEXTAREA",8);

class HTMLParserSectionTag
{

    var $type;
    var $value;
    var $attrs=Array();
    function HTMLParserSectionTag( &$iterator )
    {
        if( ($value= $iterator->match( '<!--' ))!==false)
        {
            $this->type=HTMLParserTag_EXTENDED_COMMENT;
            $this->value=$iterator->readUntil('-->');
            $iterator->skip(3);
        }
        else if( ($value= $iterator->match( '<!' ))!==false)
        {
            $this->type=HTMLParserTag_COMMENT;
            $this->value=$iterator->readUntil('>');
            $iterator->skip(1);
        }
        else if( ($value= $iterator->match( '</' ))!==false)
        {
            $this->type=HTMLParserTag_CLOSE;
            $ch=$iterator->getChar();
            $buffer='';
            while( $ch!='' && !isWhiteSpace($ch) && $ch!='>' && $ch!='/')
            {
                $this->value.=$ch;
                $ch=$iterator->getChar();
            }
            if( $this->value=='' )
            {
                $this->value='<'.$ch;
                $this->type=HTMLParserTag_CONTENT;
            }
            else if( $ch=='/' && ($value= $iterator->match( '>' ))!==false )
            {
            }
            else if($ch!='>')
            {
                $endOfTag=$this->readAttrs($iterator);
            }
        }
        else if( ($value= $iterator->match( '<' ))!==false)
        {
            $this->type=HTMLParserTag_OPEN;
            $ch=$iterator->getChar();
            $buffer='';
            while( $ch!='' && !isWhiteSpace($ch) && $ch!='>' && $ch!='/')
            {
                $this->value.=$ch;
                $ch=$iterator->getChar();
            }
            if( $this->value=='' )
            {
                $this->value='<'.$ch;
                $this->type=HTMLParserTag_CONTENT;
            }
            else if( $ch=='/' && ($value= $iterator->match( '>' ))!==false )
            {
                $this->type=HTMLParserTag_STANDALONE;
            }
            else if($ch!='>')
            {
                $endOfTag=$this->readAttrs($iterator);
                if($endOfTag=='/>')
                {
                    $this->type=HTMLParserTag_STANDALONE;
                }
            }
        }
        else
        {
            $this->value=$iterator->readUntil('<');
            $this->type=HTMLParserTag_CONTENT;
        }
        if( $this->type==HTMLParserTag_OPEN && strtolower($this->value)=="script" )
        {
            $this->readScript($iterator);
        }
        else if( $this->type==HTMLParserTag_STANDALONE && strtolower($this->value)=="script" )
        {
            $this->type=HTMLParserTag_SCRIPT;
            $this->value='';
        }
        else if( $this->type==HTMLParserTag_OPEN && strtolower($this->value)=="textarea" )
        {
            $this->type=HTMLParserTag_TEXTAREA;
            $this->readUntilEndTag($iterator,"textarea");
        }
        else if( $this->type==HTMLParserTag_STANDALONE && strtolower($this->value)=="textarea" )
        {
            $this->type=HTMLParserTag_TEXTAREA;
            $this->value='';
        }
        else if( $this->type==HTMLParserTag_OPEN && strtolower($this->value)=="style" )
        {
            $this->type=HTMLParserTag_STYLE;
            $this->readUntilEndTag($iterator,"style");
        }
        else if( $this->type==HTMLParserTag_STANDALONE && strtolower($this->value)=="style" )
        {
            $this->type=HTMLParserTag_STYLE;
            $this->value='';
        }
    }
    function readScript(&$iterator)
    {
        $this->type=HTMLParserTag_SCRIPT;
        $this->readUntilEndTag($iterator,"script");
    }
    function readUntilEndTag(&$iterator,$tag)
    {

        $this->value='';
        while ($iterator->hasMore() )
        {
            $this->value.=$iterator->readUntil( '</'.$tag );
            if( $iterator->match("</$tag>") )
            {
                return;
            }
            else
            {
            $pos=$iterator->getPos();
                $section=new HTMLParserSectionTag($iterator);
                if( $section->type==HTMLParserTag_STANDALONE && strtolower($section->value)==$tag )
                {
                    return;
                }
                else
                {
                    $iterator->setPos($pos);
                    $this->value.=$iterator->getChar();
                }
            }
        }   
    }
    function getAttribute( $name )
    {
        return array_key_exists($name,$this->attrs)?$this->attrs[$name]:null;
    }
    function readAttrs(&$iterator)
    {
        while( $iterator->hasMore() )
        {
            if( $iterator->match( '>' ) )
            {
                return '>';
            }
            else if( $iterator->match( '/>' ) )
            {
                return '/>';
            }
            $iterator->skipWhiteSpace();
            $name='';
            $value='';
            $ch=$iterator->getChar();
            while( $ch!='' && !isWhiteSpace($ch) && $ch!='>' && $ch!='=' )
            {
                if( $ch=='/' && $iterator->peek()=='>')
                {
                    $ch='/>';
                    $iterator->getChar();
                    break;
                }
                $name.=$ch;
                $ch=$iterator->getChar();
            }
            if( $ch=='>' || $ch=='/>')
            {
                $this->attrs[$name]=false;
                return $ch;
            }
            $whitespace=(isWhiteSpace($ch)?1:0)+$iterator->skipWhiteSpace();
            if( $iterator->peek()=='=' )
            {
                $ch='=';
                $iterator->skip();
                $whitespace=0;
            }
            $whitespace=$whitespace+$iterator->skipWhiteSpace();
            $value=false;
            if( $iterator->peek()=='\'' )
            {
                $iterator->skip();
                $ch=$iterator->getChar();
                while( $ch!='\'' )
                {
                    $value.=$ch;
                    $ch=$iterator->getChar();
                }
            }
            else if( $iterator->peek()=='"' )
            {
                $iterator->skip();
                $ch=$iterator->getChar();
                while( $ch!='"' )
                {
                    $value.=$ch;
                    $ch=$iterator->getChar();
                }

            }
            else
            {
                if( $whitespace==0 )
                {
                    $value='';
                    $ch=$iterator->getChar();
                    while( $ch!='' && !isWhiteSpace($ch) && $ch!='>' && $ch!='=' )
                    {
                        if( $ch=='/' && $iterator->peek()=='>')
                        {
                            $ch='/>';
                            $iterator->getChar();
                            break;
                        }
                        $value.=$ch;
                        $ch=$iterator->getChar();
                    }
                    if( $ch=='>' || $ch=='/>')
                    {
                        $this->attrs[$name]=$value;
                        return $ch;
                    }
                }
            }
            if( $name!='' )
            {
                $this->attrs[$name]=$value;
            }


        }
    }
    function isEntirelyWhiteSpace()
    {
        if( count($this->attrs)==0 )
        {
            for( $i=0;$i<strlen($this->value);$i++)
            {
                if( !isWhiteSpace($this->value[$i]) )
                {
                    return false;
                }
            }
            return true;
        }
        else
        {
            return false;
        }
    }
    /*
    define("HTMLParserTag_CONTENT",0);
define("HTMLParserTag_OPEN",1);
define("HTMLParserTag_CLOSE",2);
define("HTMLParserTag_STANDALONE",3);
define("HTMLParserTag_COMMENT",4);
define("HTMLParserTag_EXTENDED_COMMENT",5);
define("HTMLParserTag_SCRIPT",6);
define("HTMLParserTag_STYLE",7);
define("HTMLParserTag_TEXTAREA",8);
*/
    function getAttributesText()
    {
        $out='';
        foreach( $this->attrs as $name=>$value )
        {
            $out.=' '.$name;
            if( $value!==false)
            {
                $out.='="'.str_replace('"','&#34;',$value ).'"';
            }
        }
        return $out;
    }
    function getContent()
    {
        $out='';
        switch( $this->type)
        {
            case HTMLParserTag_CONTENT:
            {
                $out=$this->value;
            }
            break;
            case HTMLParserTag_OPEN:
            case HTMLParserTag_STANDALONE:
            {
                $out='<'.$this->value;
                $attr=$this->getAttributesText();
                if( $attr!='')
                {
                    $out.=$attr.' ';
                }
                $out.=$this->type==HTMLParserTag_STANDALONE?'/>':'>';
            }
            break;
            case HTMLParserTag_CLOSE:
            {

                $out='</'.$this->value.">";
            }
            break;
            case HTMLParserTag_SCRIPT:
            {
                $out='<script';
                $attr=$this->getAttributesText();
                if( $attr!='')
                {
                    $out.=$attr.' ';
                }
                $out.='>'.$this->value."</script>";
            }
            break;
            case HTMLParserTag_TEXTAREA:
            {
                $out='<textarea';
                $attr=$this->getAttributesText();
                if( $attr!='')
                {
                    $out.=$attr.' ';
                }
                $out.='>'.$this->value."</textarea>";
            }
            break;
            case HTMLParserTag_STYLE:
            {
                $out='<style';
                $attr=$this->getAttributesText();
                if( $attr!='')
                {
                    $out.=$attr.' ';
                }
                $out.='>'.$this->value."</style>";
            }
            break;
            case HTMLParserTag_COMMENT:
            {
                $out.='<!'.$this->value.">";
            }
            break;
            case HTMLParserTag_EXTENDED_COMMENT:
            {
                $out.='<!--'.$this->value."-->";
            }
            break;
        }
        return $out;
    }
    function hasTagName( $name )
    {
        $out=false;
        switch( $this->type)
        {
            case HTMLParserTag_OPEN:
            case HTMLParserTag_STANDALONE:
            case HTMLParserTag_CLOSE:
            {
                $out=strtolower($this->value)==strtolower($name);
            }
            break;
            case HTMLParserTag_SCRIPT:
            {
                $out=strtolower($name)=='script';
            }
            break;
            case HTMLParserTag_TEXTAREA:
            {
                $out=strtolower($name)=='textarea';
            }
            break;
            case HTMLParserTag_STYLE:
            {
                $out=strtolower($name)=='style';
            }
            break;
            case HTMLParserTag_COMMENT:
            case HTMLParserTag_EXTENDED_COMMENT:
            {
                if( strtolower($name)=='~comment' )
                {
                    $out=true;
                }
                else if( sWith(strtolower($name),'~comment:' ) )
                {
                    $out=$this->value==substr($name,strlen('~comment:'));
                }
            }
            break;
            case HTMLParserTag_CONTENT:
            {
                $out=strtolower($name)=='~content';
            }
            break;
            default:
            {
                $out=false;
            }
            break;
        }
        return $out;
    }
    function removeAttributes( $name )
    {
        $newAttrs=Array();
        $name=strtolower($name);
        foreach( $this->attrs as $attrName=>$attrValue )
        {
            if( strtolower($name)!=strtolower($attrName) )
            {
                $newAttrs[$attrName]=$attrValue;
            }
        }
        $this->attrs=$newAttrs;
    }
    function removeAttributesStartingWith( $name )
    {
        $newAttrs=Array();
        $name=strtolower($name);
        foreach( $this->attrs as $attrName=>$attrValue )
        {
            if( !sWith(strtolower($attrName),strtolower($name)) )
            {
                $newAttrs[$attrName]=$attrValue;
            }
        }
        $this->attrs=$newAttrs;
    }   
    function removeStyle( $name )
    {
        $name=strtolower($name);
        $styleKey='';
        foreach( $this->attrs as $attrName=>$attrValue )
        {
            if( strtolower($attrName)=='style' )
            {
                $styleKey=$attrName;
            }
        }
        if( $styleKey!='' )
        {
            $styleDef=new HTMLParserIteratorStyleDefintion( $this->attrs[$styleKey] );
            $styleDef->removeStyle($name);
            $this->attrs[$styleKey]=$styleDef->getContent();
        }
    }
    function removeStyleStartingWith( $name )
    {
        $name=strtolower($name);
        $styleKey='';
        foreach( $this->attrs as $attrName=>$attrValue )
        {
            if( strtolower($attrName)=='style' )
            {
                $styleKey=$attrName;
            }
        }
        if( $styleKey!='' )
        {
            $styleDef=new HTMLParserIteratorStyleDefintion( $this->attrs[$styleKey] );
            $styleDef->removeStyleStartingWith($name);
            $this->attrs[$styleKey]=$styleDef->getContent();
        }
    }
    function replaceSources( $srcs )
    {

        foreach($this->attrs as $name=>$val )
        {
            if(strtolower($name)=='src' && array_key_exists($val,$srcs ))
            {
                $this->attrs[$name]=$srcs[$val];
            }
        }
    }
    function getSources(&$srcs)
    {
        foreach($this->attrs as $name=>$val )
        {
            if(strtolower($name)=='src' )
            {
                $srcs[$this->attrs[$name]]=true;
            }
        }
    }
}


class HTMLParser
{
    var $sections=Array();
    function HTMLParser( $contents )
    {
        $iterator=new HTMLParserIterator($contents);
        $this->sections=Array();
        while( $iterator->hasMore() )
        {

            $startPos=$iterator->getPos();
            $section=new HTMLParserSectionTag($iterator);
            $this->sections[]=$section;
            if( $startPos==$iterator->getPos() )
            {
                break;
            }
        }
    }
    function getContent()
    {
        $out='';
        foreach( $this->sections as $section )
        {
            $out.=$section->getContent();
        }
        return $out;
    }
    function removeTags( $name )
    {
        $newSections=Array();
        foreach( $this->sections as $section )
        {
            if( !$section->hasTagName($name) )
            {
                $newSections[]=$section;
            }
        }
        $this->sections=$newSections;
    }
    function removeAttributes( $name )
    {
        for( $i=0;$i<count($this->sections);$i++)
        {
            $this->sections[$i]->removeAttributes($name);
        }
    }
    function removeAttributesStartingWith( $name )
    {
        for( $i=0;$i<count($this->sections);$i++)
        {
            $this->sections[$i]->removeAttributesStartingWith($name);
        }
    }
    function removeStyle( $name )
    {
        for( $i=0;$i<count($this->sections);$i++)
        {
            $this->sections[$i]->removeStyle($name);
        }
    }
    function removeStyleStartingWith( $name )
    {
        for( $i=0;$i<count($this->sections);$i++)
        {
            $this->sections[$i]->removeStyleStartingWith($name);
        }
    }
    function removeSections( $name )
    {
        $newSections=Array();
        $openTags=0;
        foreach( $this->sections as $section )
        {
            if( !$section->hasTagName($name) )
            {
                if( $openTags==0 )
                {
                    $newSections[]=$section;
                }
            }
            else
            {
                if( $section->type==HTMLParserTag_OPEN )
                {
                    $openTags++;
                }
                else if( $section->type==HTMLParserTag_CLOSE && $openTags>0)
                {
                    $openTags--;
                }
            }
        }
        $this->sections=$newSections;
    }
    function replaceSources( $srcs )
    {
        foreach( $this->sections as $id=>$section )
        {
            $this->sections[$id]->replaceSources( $srcs );  
        }
    }
    function moveSources( $basePath,$oldFolder,$newFolder )
    {
        $srcs=$this->getSources();
        $newSrcs=Array();
        foreach($srcs as $src=>$junk )
        {
            if( substr( $src,0,strlen($oldFolder) )==$oldFolder )
            {
                if( !file_exists($basePath.$newFolder ) )
                {
                    mkdir($basePath.$newFolder );
                }
                $newSrc=$newFolder.substr( $src,strlen($oldFolder) );
                rename($basePath.$src,$basePath.$newSrc);
                $newSrcs[$src]=$newSrc;

            }
        }
        $this->replaceSources( $newSrcs );
    }
    function getSources()
    {
        $srcs=Array();
        foreach( $this->sections as $id=>$section )
        {
            $this->sections[$id]->getSources( $srcs) ;  
        }
        return $srcs;
    }
    function compact( )
    {
        $newSections=Array();
        $openTags=0;
        foreach( $this->sections as $section )
        {
            if( !$section->isEntirelyWhiteSpace() )
            {
                $newSections[]=$section;
            }
        }
        $this->sections=$newSections;
    }

    function removeUpto( $name, $inclusive=false)
    {
        $newSections=Array();
        $found=false;
        foreach( $this->sections as $section )
        {
            if( !$inclusive && $section->hasTagName($name) )
            {
                $found=true;
            }
            if( $found)
            {
                $newSections[]=$section;
            }
            if( $inclusive && $section->hasTagName($name) )
            {
                $found=true;
            }
        }
        $this->sections=$newSections;
    }
    function removeAfter( $name, $inclusive=false)
    {
        $newSections=Array();
        $found=false;
        foreach( $this->sections as $section )
        {
            if( $inclusive && $section->hasTagName($name) )
            {
                $found=true;
            }
            if( !$found)
            {
                $newSections[]=$section;
            }
            if( !$inclusive && $section->hasTagName($name) )
            {
                $found=true;
            }
        }
        $this->sections=$newSections;
    }
}


?>

EDIT: This is actually the part you want, the part above parses the HTML emails.

<?php
define('ATTACHMENT_UPLOAD_SERVER_DIRECTORY',dirname(__FILE__).'/storedimages');
define('ATTACHMENT_UPLOAD_WEB_DIRECTORY','storedimages');
    if( !function_exists('randomString') )
    {
        function randomString( $len,$chrs='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
        {
          $out="";
          for($i=0;$i<$len;$i++)
          {
            $out=$out.substr($chrs,rand()%strlen($chrs),1);
        }
        return $out;
      }
    }
class EmailDownloader
{
    var $imageTypesAllowed=Array('JPEG'=>'.jpg','GIF'=>'.gif','PNG'=>'.png');
    var $mbox           =   NULL;   /* mailbox resource */
    function EmailDownloader($username,$password,$mailserver='localhost',$servertype='pop',$port='default')
    {
        if($port=='default')
        {
            $imap_port='143';
            $pop_port='110';
        }
        else
        {
            $imap_port=$port;
            $pop_port=$port;
        }
        if($servertype=='pop')
        {
             $strconnect= '{'.$mailserver.':'.$pop_port. '/pop3}INBOX';
        }
        else if($servertype=='imap') 
        {
            $strconnect= $strconnect='{'.$mailserver.':'.$imap_port. '}INBOX'; 
        }
        else 
        {
            die("*** error, mailserver type should be either 'pop' or 'imap'\n");
        }
        $this->mbox=imap_open($strconnect,$username,$password);
    }
    function getEmails($deleteMessages=false,$attachmentLocationServer=ATTACHMENT_UPLOAD_SERVER_DIRECTORY,$attachmentLocationWeb=ATTACHMENT_UPLOAD_WEB_DIRECTORY)
    {
        $headers=imap_headers($this->mbox);
        //print_r($headers);
        $emails=Array();

        for($idx=0,$mid=1;$idx<count($headers);$idx++,$mid++)
        {   
            $tmpFolder='';
            while ($tmpFolder=='' )
            {
                $tmpFolder='/tmp_'.randomString( 5,'abcdefghijklmnopqrstuvwxyz0123456789');
                if( file_exists($attachmentLocationServer.$tmpFolder) )
                {
                    $tmpFolder='';
                }
            }
            mkdir($attachmentLocationServer.$tmpFolder );

            $images=Array();
            $mail_header=imap_header($this->mbox,$mid);
            $fromAddress=($mail_header->from[0]->mailbox).'@'.($mail_header->from[0]->host);
            //print_r($mail_header);
            // $message=imap_body($this->mbox,$mid); # yeah, not so simple, some voodoo needed
            $mob=imap_fetchstructure($this->mbox,$mid);
            if(($mob->type)==0)
            {
                // simple text message so, no problemo!
                $message=imap_body($this->mbox,$mid);
            }else
            {
                // oops, multipart message
            //  echo  get_part($this->mbox, $mid, "MULTIPART");
                $contentParts = count($mob->parts);
                $message=get_part($this->mbox, $mid, "TEXT/HTML", $mob);
                foreach( $mob->parts as $nm=>$part )
                {
                    if( $part->type==5  ||$part->type==3 )
                    {
                        $ext='';
                        if( array_key_exists($part->subtype,$this->imageTypesAllowed) )
                        {
                            $ext=$this->imageTypesAllowed[$part->subtype];
                        }
                        if( $ext=='' && $part->subtype=='OCTET-STREAM' && isset($part->dparameters) )
                        {
                            $attFilename='';
                            foreach( $part->dparameters as $dpara )
                            {
                                if( $dpara->attribute=='FILENAME' ) 
                                {
                                    $attFilename=$dpara->value;
                                }
                            }
                            foreach( $this->imageTypesAllowed as $allowedExt )
                            {
                                if( substr($attFilename,-strlen($allowedExt) )== $allowedExt )
                                {
                                    $ext=$allowedExt;
                                }
                            }
                        }
                        if( $ext!=''  )
                        {
                            $filename='';
                            while(  $filename=='' )
                            {
                                $filename=randomString( 20,'abcdefghijklmnopqrstuvwxyz0123456789').$ext;
                                if( file_exists($attachmentLocationServer.$tmpFolder.'/'.$filename ) )
                                {
                                    $filename='';
                                }
                            }
                            if( $file=fopen($attachmentLocationServer.$tmpFolder.'/'.$filename,'w') )
                            {
                                fwrite($file,imap_base64(imap_fetchbody($this->mbox,$mid,$nm+1)));
                                fclose($file);
                                $images[str_replace(array('>','<'),array('','cid:'),$part->id)]=$attachmentLocationWeb.$tmpFolder.'/'.$filename;
                            }

                        }

                    }
                }

            }
            $emails[]=Array('from'=>$fromAddress,'subject'=>$mail_header->Subject,'body'=>$message,'header'=>$mail_header,'images'=>$images,'tmp_folder'=>$tmpFolder);
            if( $deleteMessages )
            {
                imap_delete($this->mbox,$mid);      
            }
        }
        imap_expunge($this->mbox);
        return $emails;
    }

    function close()
    {
        imap_close($this->mbox);
    }
}
?>



<?
   function get_mime_type(&$structure) {
   $primary_mime_type = array("TEXT", "MULTIPART","MESSAGE", "APPLICATION", "AUDIO","IMAGE", "VIDEO", "OTHER");
   if($structure->subtype) {
    return $primary_mime_type[(int) $structure->type] . '/' .$structure->subtype;
   }
    return "TEXT/PLAIN";
   }
   function get_part($stream, $msg_number, $mime_type, $structure = false,$part_number    = false) {

    if(!$structure) {
        $structure = imap_fetchstructure($stream, $msg_number);
    }
    if($structure) {
        if($mime_type == get_mime_type($structure)) {
            if(!$part_number) {
                $part_number = "1";
            }
            $text = imap_fetchbody($stream, $msg_number, $part_number);
            if($structure->encoding == 3) {
                return imap_base64($text);
            } else if($structure->encoding == 4) {
                return imap_qprint($text);
            } else {
            return $text;
        }
    }

        if($structure->type == 1) /* multipart */
        {
            $prefix ='';
        while(list($index, $sub_structure) = each($structure->parts)) {
            if($part_number) {
                $prefix = $part_number . '.';
            }
            $data = get_part($stream, $msg_number, $mime_type, $sub_structure,$prefix .    ($index + 1));
            if($data) {
                return $data;
            }
        } // END OF WHILE
        } // END OF MULTIPART
    } // END OF STRUTURE
    return false;
   } // END OF FUNCTION

?> 

EDIT #2: One More Part to complete the entire process

<?php

    define('RECEIVING_EMAIL_SERVER','mail.server.com');
    define('RECEIVING_EMAIL_ACCOUNT','incoming@server.com');
    define('RECEIVING_EMAIL_PASSWORD','myPasswordIsHere');
    define('DELETE_MAIL_MESSAGES_FROM_SERVER',false);
    define('USE_MYSQL_ESCAPE',false);
    define('STORED_FOLDER_BASE',dirname(__FILE__).'/');
    define('STORED_IMAGES_LOCATION','storedimages');
    define('STORED_IMAGES_LOCATION_ABSOLUTE',STORED_FOLDER_BASE.STORED_IMAGES_LOCATION);
    include_once('class.emaildownloader.php');
    include_once('class.htmlparser.php');
    $getEmail=new EmailDownloader(RECEIVING_EMAIL_ACCOUNT,RECEIVING_EMAIL_PASSWORD,RECEIVING_EMAIL_SERVER);
    $emails=$getEmail->getEmails( DELETE_MAIL_MESSAGES_FROM_SERVER,STORED_IMAGES_LOCATION_ABSOLUTE,STORED_IMAGES_LOCATION );
    $getEmail->close();

    if( $emails )
    {   
echo "EMAILS FOUND: ".count($emails)." <br />";
    }

    foreach( $emails as $email )
    {
echo "PARSING EMAIL<br />";
        $parsedDoc=new HTMLParser($email['body']);
        $parsedDoc->removeSections( "script" );
        $parsedDoc->removeSections( "style" );
        $parsedDoc->removeSections( "head" );
        $parsedDoc->removeSections( "applet");
        $parsedDoc->removeSections( "embed");
        $parsedDoc->removeSections( "object");
        $parsedDoc->removeSections( "iframe" );
        $parsedDoc->removeSections( "select" );
        $parsedDoc->removeSections( "option" );
        $parsedDoc->removeTags("noscript");
        $parsedDoc->removeTags("html");
        $parsedDoc->removeTags("body");
        $parsedDoc->removeTags("~comment");
        $parsedDoc->removeTags( "input" );
        $parsedDoc->removeTags( "link" );
        $parsedDoc->removeTags( "form");
        $parsedDoc->removeAttributes("background");
        $parsedDoc->removeAttributes("bgcolor");
        $parsedDoc->removeAttributesStartingWith("on");
        $parsedDoc->removeStyleStartingWith('background');
        $parsedDoc->compact( );
        if( count($email['images'])>0 ) 
        {
            $parsedDoc->replaceSources( $email['images'] );
        }
        $subject=explode(' ',$email['subject']);
        if( USE_MYSQL_ESCAPE)
        {
            $project_id=mysql_real_escape_string($subject[0]);
            $page_id=mysql_real_escape_string($subject[1]);
        }
        else
        {
            $project_id=addSlashes($subject[0]);
            $page_id=addSlashes($subject[1]);
        }

echo "PARSING COMPLETE<br />";
    }   

?>


Zend_Mail:

http://framework.zend.com/manual/en/zend.mail.html

Specifically: Zend_Mail_Storage

http://framework.zend.com/manual/en/zend.mail.read.html

Note: You don't need to use the whole framework. You can just use the classes you need. It's designed that way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜