Location: PHPKode > scripts > POP/IMAP to RSS > pop-imap-to-rss/mail2rss.class.php
<?php
/**
* Filename: mail2rss.class.php 
* Purpose: Create RSS feeds from POP/IMAP mailboxes
* Author: Vedanta Barooah <hide@address.com>
* License: PHP License
* Date: Sunday, March 19, 2006
*/
///////////////////////////////////////////////////////////////////////////////////////
// changes:
// 19-Mar-06 : Initial Release
// 22-Mar-06 : Trash off Multipart messages, (bug reported by: hide@address.com)

class mail2rss{
	
	var $mbox			=	NULL;	/* mailbox resource */
	var $xml			=	NULL;
	/** 
	* Constructor, takes in the mail server name, the type and the mail port, mail username and his password
	*/
	function mail2rss($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);
		//if(!$mbox) die("*** error, connect to specified mail server failed\n");
	}
	/**
	* Read mail and create feed
	*/
	function makefeed($rss_title="MAIL",$description="MAIL",$web_url=NULL,$item_link=NULL,$language='en-us'){
		$self_name='http://'.$_SERVER['REMOTE_ADDR'].$_SERVER['PHP_SELF'];
		$this->xml="<?xml version='1.0' ?>\n<rss version='2.0'>\n<channel>\n";
		$this->xml.="<title>$rss_title</title>\n";
		$this->xml.="<link>$web_url</link>\n";
		$this->xml.="<description>$description</description>\n";
		$this->xml.="<language>$language</language>\n";
		$this->xml.="<docs>$self_name</docs>\n";
		$headers=imap_headers($this->mbox);
		for($idx=0,$mid=1;$idx<count($headers);$idx++,$mid++){	
			$mail_header=imap_header($this->mbox,$mid);
			$from=htmlentities($mail_header->fromaddress);
			// $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
				$message="Multipart Mail or Mail with Attachment!";
			}
			if($item_link!=NULL)$postfix=$item_link.$mid;
			$this->xml.="<item>\n";
			$this->xml.="<title>".$mail_header->Subject.' - '.$from."</title>\n";
			$this->xml.="<description>".$message."</description>";
			$this->xml.="<link>". $postfix ."</link>";
			$this->xml.="</item>\n";
		}
		$this->xml.="</channel>\n</rss>\n";
	}
	
	function show(){
	/* spits out the created rss */
	header("Content-type: text/xml");
	echo $this->xml;
	}
	
	function createfile($filename){
	/* create a rss file */
		$fp=fopen($filename,'w');
		fwrite($fp,$this->xml);
		fclose($fp);
	}
	
	function close(){
		imap_close($this->mbox);
	}
}
?>
Return current item: POP/IMAP to RSS