<?php /* sending and receiving email with imap version 0.4 - work in progress... Copyright 2005-2006 V. Yanson <info@positiveblue.com> Creation Date: 06/02/05 Last Update: 08/24/06 (Thanks Thomas James Hunkin) !!! REQUIRES IMAP MODULE INSTALLED !!! http://www.php.net/manual/en/ref.imap.php 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 public function __toString() { return "rsIMAP v0.4"; } // attach file to message composition public function attachFile($filename) { $x = count($this->body) + 1; // next part $this->body[$x] = array(); $this->body[$x]["type"] = TYPEAPPLICATION; $this->body[$x]["encoding"] = ENCBASE64; $this->body[$x]["subtype"] = "octet-stream"; $this->body[$x]["description"] = basename($filename); $this->body[$x]["disposition.type"] = "attachment"; $this->body[$x]["disposition"] = array("filename" => basename($filename)); $this->body[$x]["dparameters.filename"] = basename($filename); $this->body[$x]["parameters.name"] = basename($filename); $this->body[$x]["contents.data"] = base64_encode(file_get_contents($filename)); } public function addEmbeddedImage($filename, $cid) { $x = count($this->body) + 1; // next part $this->body[$x] = array(); $this->body[$x]["type"] = TYPEIMAGE; $this->body[$x]["encoding"] = ENCBASE64; $this->body[$x]["subtype"] = "octet-stream"; $this->body[$x]["description"] = basename($filename); $this->body[$x]["id"] = "<" . $cid . ">"; $this->body[$x]["disposition.type"] = "inline"; $this->body[$x]["disposition"] = array("filename" => basename($filename)); $this->body[$x]["content.type"] = "octet-stream"; $this->body[$x]["content"] = array("name" => $cid); $this->body[$x]["dparameters.filename"] = basename($filename); $this->body[$x]["parameters.name"] = basename($filename); $this->body[$x]["cid"] = $cid; $this->body[$x]["contents.data"] = base64_encode(file_get_contents($filename)); } // create imap mbox object public 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 public function sendMessage($to, $subject, $message, $type = 'text', $subtype = 'html') { $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"] = $type; // text $this->body[$x]["subtype"] = $subtype; // 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 (just if you want to put some ads in outgoing emails or something) $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 public 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 .= nl2br($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 } } ?>
|