Version: 0.3

Type: Class

Category: Networking

License: Other

Description: This class can use the PHP IMAP extension to retrieve e-mail messages from a mailbox stored in an IMAP server. It can also compose and send new messages using other functions of the PHP IMAP extension. Currently it allows attaching a file to a message to be sent with a given text body.



<?php

/*

sending and receiving email with imap
version 0.3 - work in progress...

Copyright 2005 V. Yanson <slava@gxsquare.com>
Date: 06/02/05
You may not distribute this script without author's written permission!

!!! REQUIRES IMAP MODULE INSTALLED !!!
http://www.php.net/manual/en/ref.imap.php

only tested in php 5.0.4 but should work in php 4 as well

to send message:
$imap = new imap();
$imap->from = "my name <my@email>";
$imap->attachFile("/path/to/your/file");
$imap->sendMessage("send.to@address.com", "my subject", "my <b>html</b> message");

to receive message:
$mbox = imap::getMbox("my@user.name", "password");
$message = imap::getMessage($mbox, 10);
print_r($message);

*/

class imap
{
	var $from = "";
	var $to = "";
	var $cc = "";
	var $msgFooter = "";
	// protected from public
	protected $body = array(
		// should start with 1... just in case ;)
		1 => array(
			"type" => TYPEMULTIPART, // we only send multipart
			"subtype" => "mixed" // mixed formats
		)
	);
	
	// someone calls class without any function
	function __toString()
	{
		return "rsIMAP v0.3";
	}
	
	// attach file to message composition
	function attachFile($filename)
	{
		$contents = file_get_contents($filename);
		
		$x = count($this->body) + 1; // next part
		
		$this->body[$x] = array();
		$this->body[$x]["contents.data"] = $contents;
		$this->body[$x]["disposition.type"] = "attachment"; // type is attachment
		$this->body[$x]["disposition"] = array ("filename" => basename($filename)); // filename
	}
	
	// create imap mbox object
	function getMbox($user, $pass, $host = "localhost", $port = 110, $box = "INBOX", $type = "pop3")
	{
		$mbox = imap_open("{".$host.":".$port."/".$type."}".$box, $user, $pass);
		return $mbox;
	}
	
	// send message
	function sendMessage($to, $subject, $message)
	{
		$this->to = $to;
			
		// assemble envelop
		$envelop = array (
			"from" => $this->from,
			"to" => $this->to,
			"cc" => $this->cc
		);
		
		$x = count($this->body) + 1; // next part
		
		$this->body[$x] = array();
		
		$this->body[$x]["type"] = "text"; // text
		$this->body[$x]["subtype"] = "html"; // sending in html
		$this->body[$x]["description"] = "Message body"; // just a description for this part can skip it
		// footer of message is in $this->msgFooter;
		$this->body[$x]["contents.data"] =  $message . $this->msgFooter;
		
		// assemble message from parts
		$message = imap_mail_compose($envelop, $this->body);
		list($t_header, $t_body) = split("\r\n\r\n", $message, 2); // split headers and body
		$t_header = str_replace("\r",'', $t_header);

		// send it now
		return imap_mail($to, $subject, $t_body, $t_header); 
	}
	
	// retrieve message by it's id
	function getMessage($mbox, $messageid)
	{
		$message = array(); // it's an array
		
		// get headers for message
		$header = imap_header($mbox, $messageid);
		$structure = imap_fetchstructure($mbox, $messageid); // structure
		// assign variables
		$message['subject'] = $header->subject;
		$message['fromname'] = $header->from[0]->personal;
		$message['fromaddress'] = $header->from[0]->mailbox . "@" . $header->from[0]->host;
		$message['toaddress'] = $header->toaddress;
		$message['ccaddress'] = $header->ccaddress;
		$message['date'] = $header->date;
		
		// do we have any attachments?
		$parts = $structure->parts;
		$numparts = count($parts);
	
		if ($numparts > 1) // yes we do
		{
			$endwhile = false;
			$stack = array();
			$content = "";
			$attachment = array();
			$i = 0;
			
			// for every attachment
			while (!$endwhile)
			{
				if (!$parts[$i])
				{
					if (count($stack) > 0) // have stacks?
					{ // sure we do!
						$parts = $stack[count($stack)-1]["p"];
						$i = $stack[count($stack)-1]["i"] + 1;
						array_pop($stack);
					} else { // or no...
						$endwhile = true;
					}
				}
				
				if (!$endwhile) // last loop?
				{
					$partstring = "";
					foreach ($stack as $s)
						$partstring .= ($s["i"]+1) . ".";
					$partstring .= ($i+1);
					
					if (strtoupper($parts[$i]->disposition) == "ATTACHMENT") // attachment!!
					{
						$attachment[] = array(
							"filename" => $parts[$i]->parameters[0]->value, // filename
							"encoding" => $parts[$i]->encoding, // encodign
							"filedata" => imap_fetchbody($mbox, $messageid, $partstring) // data, duhh!
						);
					} elseif (strtoupper($parts[$i]->subtype) == "PLAIN") { // message
						$np = imap_fetchbody($mbox, $messageid, $partstring); // parse
						$content .= str_replace("\n", "<br>", $np); // add this part
					}
				}
				
				if ($parts[$i]->parts)
				{
					$stack[] = array("p" => $parts, "i" => $i); // next stack
					$parts = $parts[$i]->parts; // parts
					$i = 0;
				} else {
					$i++;
				}
			}
		} else { // no attachments
			$attachment = array();
			$content = imap_body($mbox, $messageid);
		}
			
		$message['body'] = $content;
		$message['attachment'] = $attachment;
		
		return $message; // return message
	}
}

?>