Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 8f7a27d6e172f418bc6549343b5485ef9f8c74bb 1 parent b9a1ea3
Chris authored
View
34 README.md
@@ -1,4 +1,34 @@
-php-imap
+PHP-IMAP
========
-Imap Class for PHP
+PHP-IMAP is used to retrieve messages from an IMAP server.It uses the PHP IMAP extension to access to a given IMAP server and can execute several operations to retrieve different parts of the available messages. Currently it can retrieve information about the mail box, message headers, message data, and attachments.
+
+PHP-IMAP came about a few years ago due to a complete lack of quality libraries for working with PHPs IMAP functions, particularly dealing with attachments. I've just now gotten around to putting the project on github.
+
+Features
+------
+* Connects to IMAP server
+* Returns mailbox information
+* Access e-mail headers
+* List mailboxes
+* Read e-mails including attachments
+* Save attachments from remote imap server locally
+
+Dependancies
+------
+* PHP 5.x
+* php5-imap
+
+Installation
+------
+
+You can either clone the project, download the project, or just copy & paste imap.class.php from github.
+
+Documentation
+------
+
+When you download or clone, move the code into a directory you can run apache from. Load up the index.php page (example http://localhost/php-imap/index.php). This is a demo application. Each operation it executes is documented with the method it calls and the parameters it accepts. You can view the demo source to see how to use the class. I'll add more documentation later.
+
+Licensing
+------
+Code is licensed under the MIT License.
View
343 classes/imap.class.php
@@ -0,0 +1,343 @@
+<?php
+/**
+ * LICENSE: The MIT License
+ * Copyright (c) 2010 Chris Nizzardini (http://www.cnizz.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+ * largely a wrapper class for php imap functions but since the classes on phpclasses.org are so shitty here we go....
+ * @see http://www.php.net/manual/en/book.imap.php
+ * @uses imap_mailboxmsginfo
+ * @uses imap_headers
+ * @uses imap_list
+ * @uses imap_headerinfo
+ */
+
+class Imap
+{
+ private $stream;
+ private $mbox;
+ private $is_connected = 0;
+ private $host;
+ private $username;
+ private $password;
+ private $port;
+ private $tls;
+
+ function __construct($host,$username,$password,$port=143,$tls='notls'){
+ $this->username = $username;
+ $this->password = $password;
+ $this->host = $host;
+ $this->port = $port;
+ $this->tls = $tls;
+ $this->openMailBox();
+ }
+
+ /**
+ * returnImapMailBoxmMsgInfoObj - returns general mailbox information
+ * @see http://www.php.net/manual/en/function.imap-mailboxmsginfo.php
+ * @param void
+ * @return object
+ */
+ public function returnImapMailBoxmMsgInfoObj(){
+ return imap_mailboxmsginfo($this->stream);
+ }
+
+ /**
+ * returnMailBoxHeaderArr - this is all you need to see email header information for all emails within a mailbox
+ * @param void
+ * @return array
+ */
+ public function returnMailBoxHeaderArr($mailbox)
+ {
+ $array = array();
+ $this->openMailBox($mailbox);
+ $arr = $this->returnImapHeadersArr();
+
+ if(is_array($arr)){
+ foreach($arr as $i){
+ $i = trim($i);
+ // check for unread emails
+ if(substr($i,0,1) == 'U'){
+ $i = substr($i,1,strlen($i));
+ }
+ $i = trim($i);
+ // display if not pending deletion
+ if(substr($i,0,1) != 'D'){
+ $position = strpos($i,')');
+ $msgno = preg_replace('/\D/','',substr($i,0,$position));
+ $array[] = $this->returnEmailHeaderArr($msgno);
+ }
+ }
+ }
+ return $array;
+ }
+
+ /**
+ * returnMailboxListArr - returns lists of mailboxes
+ * @see http://www.php.net/manual/en/function.imap-list.php
+ * @param void
+ * @return array
+ */
+ public function returnMailboxListArr(){
+ return imap_list($this->stream,$this->mbox,'*');
+ }
+
+ /**
+ * returnEmailHeaderArr
+ * This returns detailed header information for the given message number
+ * @param messageNumber
+ * @return array
+ */
+ public function returnEmailHeaderArr($messageNumber)
+ {
+ $head = $this->returnHeaderInfoObj($messageNumber);
+
+ $array['date'] = $head->date;
+ $array['subject'] = $head->subject;
+ $array['to'] = $head->toaddress;
+ $array['message_id'] = $head->message_id;
+ $array['from'] = $head->from[0]->mailbox.'@'.$head->from[0]->host;
+ $array['sender'] = $head->sender[0]->mailbox.'@'.$head->sender[0]->host;
+ $array['reply_toaddress'] = $head->reply_toaddress;
+ $array['size'] = $head->Size;
+ $array['msgno'] = $head->Msgno;
+
+ if($head->Unseen == 'U'){
+ $array['status'] = 'Unread';
+ }
+ else{
+ $array['status'] = 'Read';
+ }
+ return $array;
+ }
+
+ /**
+ * returnEmailMessageArr
+ * This returns the entire email for the given message number
+ * @param unknown_type $messageNumber
+ * @return array
+ * @example array('header'=>object,'plain'=>'','html'=>'','attachment'=>array());
+ */
+ public function returnEmailMessageArr($messageNumber,$withEncodedAttachment=0)
+ {
+ $array = array();
+
+ $o = $this->returnMessageStructureObj($messageNumber);
+ //print_r($o);
+ //$this->returnRawBodyStr($messageNumber);
+ if(is_object($o))
+ {
+ $array['header'] = $this->returnEmailHeaderArr($messageNumber);
+ if(isset($o->parts) && is_array($o->parts))
+ {
+ $attachments = 0;
+ // first build plain and/or html part of array
+ foreach($o->parts as $x => $i)
+ {
+ if(isset($i->parts) && is_array($i->parts))
+ {
+ foreach($i->parts as $j => $k)
+ {
+ if($k->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1.1');
+ }
+ else if($k->subtype == 'HTML'){
+ $array['html'] = $this->returnBodyStr($messageNumber,'1.2');
+ }
+ else if($k->disposition == 'attachment'){
+ $attachments++;
+ }
+ }
+ }
+ else{
+ if($i->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1');
+ }
+ else if($i->subtype == 'HTML'){
+ $array['html'] = $this->returnBodyStr($messageNumber,'2');
+ }
+ else if($i->disposition == 'attachment'){
+ $attachments++;
+ $array['attachments'][] = array('type'=>$i->subtype,'bytes'=>$i->bytes,'name'=>$i->parameters[0]->value,'part'=>"2");
+ }
+ }
+ }
+
+ if(isset($attachments) && $attachments > 1){
+ $array['attachments'] = array();
+ foreach($o->parts as $x => $i)
+ {
+ if($i->disposition == 'attachment')
+ {
+ $part = $x+1;
+ $array['attachments'][] = array('type'=>$i->subtype,'bytes'=>$i->bytes,'name'=>$i->parameters[0]->value,'part'=>$part,'msgno'=>$messageNumber);
+ }
+ }
+ }
+ }
+ // simple plain text email
+ else if($o->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1');
+ }
+ else{
+ $array['error'][] = 'Error encountered parsing email';
+ }
+ }
+ else{ // report error
+
+ }
+ return $array;
+ }
+
+ /**
+ * saveAttachmentAndReturnFullFilePathStr
+ * @param messageNumber(int),part(str),saveToFile(str)
+ * @example saveToFile = '/var/www/attachment/myAttachement' (do not include an extension i do this for you at return!)
+ * @return string
+ */
+ public function saveAttachment($messageNumber,$part,$saveToFile)
+ {
+ $arr = $this->returnEmailMessageArr($messageNumber,1);
+
+ if(is_array($arr['attachments']))
+ {
+ foreach($arr['attachments'] as $i){
+ if($i['part'] == $part){
+ $extensionArr = explode('.',$i['name']);
+ $extension = $extensionArr[(count($extensionArr) - 1)];
+ $file = $saveToFile.'.'.$extension;
+ $f = fopen($file,'w+');
+ fwrite($f,base64_decode($this->returnBodyStr($messageNumber,$part)));
+ fclose($f);
+ $f = fopen($file,'r');
+ fread($f,filesize($file));
+ return $file;
+ }
+ }
+ }
+ return '';
+ }
+
+ public function get_is_connected(){
+ return $this->is_connected;
+ }
+ public function get_mbox(){
+ return $this->mbox;
+ }
+ public function get_host(){
+ return $this->host;
+ }
+ public function get_username(){
+ return $this->username;
+ }
+ public function get_password(){
+ return $this->password;
+ }
+ public function get_folder(){
+ return $this->folder;
+ }
+ public function get_port(){
+ return $this->port;
+ }
+ public function get_tls(){
+ return $this->tls;
+ }
+ public function get_stream(){
+ return $this->stream;
+ }
+
+ // METHODS BELOW ARE PRIVATE - YOU CAN CHANGE THESE TO PUBLIC IF NEED BE - BUT THE ABOVE METHODS SHOULD GIVE YOU EVERYTHING YOU NEED
+
+ private function openMailBox($mailbox=''){
+
+ $this->mbox = (empty($mailbox)) ? '{'.$this->host.':'.$this->port.'/'.$this->tls.'}' : $mailbox;
+
+ try{
+ $this->stream = imap_open($this->mbox,$this->username,$this->password);
+ }
+ catch(Exception $e){
+ var_dump($e);
+ }
+
+ if($this->stream != false){
+ $this->is_connected = 1;
+ }
+ }
+
+ /**
+ * returnHeaderInfoObj
+ * @see http://www.php.net/manual/en/function.imap-headerinfo.php
+ * @param void
+ * @return object
+ */
+ private function returnHeaderInfoObj($messageNumber){
+ return @imap_headerinfo($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnMessageStructureObj
+ * @see http://www.php.net/manual/en/function.imap-fetchstructure.php
+ * @param unknown_type $messageNumber
+ * @return object
+ */
+ private function returnMessageStructureObj($messageNumber){
+ return imap_fetchstructure($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnRawMessageBodyStr
+ * @see http://www.php.net/manual/en/function.imap-body.php
+ * @param unknown_type $messageNumber
+ * @return string
+ */
+ private function returnRawBodyStr($messageNumber){
+ return imap_body($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnImapHeadersArr - returns general info on emails in this box
+ * @see http://www.php.net/manual/en/function.imap-headers.php
+ * @param void
+ * @return array
+ */
+ private function returnImapHeadersArr(){
+ return imap_headers($this->stream);
+ }
+
+ /**
+ * returnMessageBodyStructureObj
+ * @see http://www.php.net/manual/en/function.imap-bodystruct.php
+ * @param $messageNumber(int),part(int)
+ * @return object
+ */
+ private function returnBodyStructureObj($messageNumber,$part){
+ return imap_bodystruct($this->stream,$messageNumber,$part);
+ }
+
+ /**
+ * returnBodyStr
+ * @see http://www.php.net/manual/en/function.imap-fetchbody.php
+ * @param $messageNumber(int),part(int)
+ * @return string
+ */
+ private function returnBodyStr($messageNumber,$section){
+ return imap_fetchbody($this->stream,$messageNumber,$section);
+ }
+}
View
330 classes/imap.class.php.save
@@ -0,0 +1,330 @@
+<?php
+/**
+ * LICENSE: The MIT License
+ * Copyright (c) 2010 Chris Nizzardini (http://www.cnizz.com)
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
+ * largely a wrapper class for php imap functions but since the classes on phpclasses.org are so shitty here we go....
+ * @see http://www.php.net/manual/en/book.imap.php
+ * @uses imap_mailboxmsginfo
+ * @uses imap_headers
+ * @uses imap_list
+ * @uses imap_headerinfo
+ */
+
+class Imap
+{
+ private $stream;
+ private $mbox;
+ private $is_connected = 0;
+ private $host;
+ private $username;
+ private $password;
+ private $folder;
+ private $port;
+ private $tls;
+
+
+ function __construct($host,$username,$password,$folder='INBOX',$port=143,$tls='notls')
+ {
+ $this->mbox = '{'.$host.':'.$port.'/'.$tls.'}';
+ try{
+ $this->stream = imap_open($this->mbox,$username,$password);
+ }
+ catch(Exception $e){
+ var_dump($e);
+ }
+ if($this->stream != false){
+ $this->is_connected = 1;
+ }
+ }
+
+ /**
+ * returnImapMailBoxmMsgInfoObj - returns general mailbox information
+ * @see http://www.php.net/manual/en/function.imap-mailboxmsginfo.php
+ * @param void
+ * @return object
+ */
+ public function returnImapMailBoxmMsgInfoObj(){
+ return imap_mailboxmsginfo($this->stream);
+ }
+
+ /**
+ * returnMailBoxHeaderArr - this is all you need to see email header information for all emails within a mailbox
+ * @param void
+ * @return array
+ */
+ public function returnMailBoxHeaderArr()
+ {
+ $array = array();
+ $arr = $this->returnImapHeadersArr();
+ if(is_array($arr)){
+ foreach($arr as $i){
+ $i = trim($i);
+ // check for unread emails
+ if(substr($i,0,1) == 'U'){
+ $i = substr($i,1,strlen($i));
+ }
+ $i = trim($i);
+ // display if not pending deletion
+ if(substr($i,0,1) != 'D'){
+ $position = strpos($i,')');
+ $msgno = substr($i,0,$position);
+ $array[] = $this->returnEmailHeaderArr($msgno);
+ }
+ }
+ }
+ return $array;
+ }
+
+ /**
+ * returnMailboxListArr - returns lists of mailboxes
+ * @see http://www.php.net/manual/en/function.imap-list.php
+ * @param void
+ * @return array
+ */
+ public function returnMailboxListArr(){
+ return imap_list($this->stream,$this->mbox,'*');
+ }
+
+ /**
+ * returnEmailHeaderArr
+ * This returns detailed header information for the given message number
+ * @param messageNumber
+ * @return array
+ */
+ public function returnEmailHeaderArr($messageNumber)
+ {
+ $head = $this->returnHeaderInfoObj($messageNumber);
+ $array['date'] = $head->date;
+ $array['subject'] = $head->subject;
+ $array['to'] = $head->toaddress;
+ $array['message_id'] = $head->message_id;
+ $array['from'] = $head->from[0]->mailbox.'@'.$head->from[0]->host;
+ $array['sender'] = $head->sender[0]->mailbox.'@'.$head->sender[0]->host;
+ $array['reply_toaddress'] = $head->reply_toaddress;
+ $array['size'] = $head->Size;
+ $array['msgno'] = $head->Msgno;
+
+ if($head->Unseen == 'U'){
+ $array['status'] = 'Unread';
+ }
+ else{
+ $array['status'] = 'Read';
+ }
+ return $array;
+ }
+
+ /**
+ * returnEmailMessageArr
+ * This returns the entire email for the given message number
+ * @param unknown_type $messageNumber
+ * @return array
+ * @example array('header'=>object,'plain'=>'','html'=>'','attachment'=>array());
+ */
+ public function returnEmailMessageArr($messageNumber,$withEncodedAttachment=0)
+ {
+ $array = array();
+
+ $o = $this->returnMessageStructureObj($messageNumber);
+ //print_r($o);
+ //$this->returnRawBodyStr($messageNumber);
+ if(is_object($o))
+ {
+ $array['header'] = $this->returnEmailHeaderArr($messageNumber);
+ if(is_array($o->parts))
+ {
+ // first build plain and/or html part of array
+ foreach($o->parts as $x => $i)
+ {
+ if(is_array($i->parts))
+ {
+ foreach($i->parts as $j => $k)
+ {
+ if($k->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1.1');
+ }
+ else if($k->subtype == 'HTML'){
+ $array['html'] = $this->returnBodyStr($messageNumber,'1.2');
+ }
+ else if($k->disposition == 'attachment'){
+ $attachments++;
+ }
+ }
+ }
+ else{
+ if($i->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1');
+ }
+ else if($i->subtype == 'HTML'){
+ $array['html'] = $this->returnBodyStr($messageNumber,'2');
+ }
+ else if($i->disposition == 'attachment'){
+ $attachments++;
+ $array['attachments'][] = array('type'=>$i->subtype,'bytes'=>$i->bytes,'name'=>$i->parameters[0]->value,'part'=>"2");
+ }
+ }
+ }
+
+ if($attachments > 1){
+ $array['attachments'] = array();
+ foreach($o->parts as $x => $i)
+ {
+ if($i->disposition == 'attachment')
+ {
+ $part = $x+1;
+ $array['attachments'][] = array('type'=>$i->subtype,'bytes'=>$i->bytes,'name'=>$i->parameters[0]->value,'part'=>$part,'msgno'=>$messageNumber);
+ }
+ }
+ }
+ }
+ // simple plain text email
+ else if($o->subtype == 'PLAIN'){
+ $array['plain'] = $this->returnBodyStr($messageNumber,'1');
+ }
+ else{
+ $array['error'][] = 'Error encountered parsing email';
+ }
+ }
+ else{ // report error
+
+ }
+ return $array;
+ }
+
+ /**
+ * saveAttachmentAndReturnFullFilePathStr
+ * @param messageNumber(int),part(str),saveToFile(str)
+ * @example saveToFile = '/var/www/attachment/myAttachement' (do not include an extension i do this for you at return!)
+ * @return string
+ */
+ public function saveAttachment($messageNumber,$part,$saveToFile)
+ {
+ $arr = $this->returnEmailMessageArr($messageNumber,1);
+
+ if(is_array($arr['attachments']))
+ {
+ foreach($arr['attachments'] as $i){
+ if($i['part'] == $part){
+ $extensionArr = explode('.',$i['name']);
+ $extension = $extensionArr[(count($extensionArr) - 1)];
+ $file = $saveToFile.'.'.$extension;
+ $f = fopen($file,'w+');
+ fwrite($f,base64_decode($this->returnBodyStr($messageNumber,$part)));
+ fclose($f);
+ $f = fopen($file,'r');
+ fread($f,filesize($file));
+ return $file;
+ }
+ }
+ }
+ return '';
+ }
+
+ public function get_is_connected(){
+ return $this->is_connected;
+ }
+ public function get_mbox(){
+ return $this->mbox;
+ }
+ public function get_host(){
+ return $this->host;
+ }
+ public function get_username(){
+ return $this->username;
+ }
+ public function get_password(){
+ return $this->password;
+ }
+ public function get_folder(){
+ return $this->folder;
+ }
+ public function get_port(){
+ return $this->port;
+ }
+ public function get_tls(){
+ return $this->tls;
+ }
+ public function get_stream(){
+ return $this->stream;
+ }
+
+ // METHODS BELOW ARE PRIVATE - YOU CAN CHANGE THESE TO PUBLIC IF NEED BE - BUT THE ABOVE METHODS SHOULD GIVE YOU EVERYTHING YOU NEED
+
+ /**
+ * returnHeaderInfoObj
+ * @see http://www.php.net/manual/en/function.imap-headerinfo.php
+ * @param void
+ * @return object
+ */
+ private function returnHeaderInfoObj($messageNumber){
+ return @imap_headerinfo($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnMessageStructureObj
+ * @see http://www.php.net/manual/en/function.imap-fetchstructure.php
+ * @param unknown_type $messageNumber
+ * @return object
+ */
+ private function returnMessageStructureObj($messageNumber){
+ return imap_fetchstructure($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnRawMessageBodyStr
+ * @see http://www.php.net/manual/en/function.imap-body.php
+ * @param unknown_type $messageNumber
+ * @return string
+ */
+ private function returnRawBodyStr($messageNumber){
+ return imap_body($this->stream,$messageNumber);
+ }
+
+ /**
+ * returnImapHeadersArr - returns general info on emails in this box
+ * @see http://www.php.net/manual/en/function.imap-headers.php
+ * @param void
+ * @return array
+ */
+ private function returnImapHeadersArr(){
+ return imap_headers($this->stream);
+ }
+
+ /**
+ * returnMessageBodyStructureObj
+ * @see http://www.php.net/manual/en/function.imap-bodystruct.php
+ * @param $messageNumber(int),part(int)
+ * @return object
+ */
+ private function returnBodyStructureObj($messageNumber,$part){
+ return imap_bodystruct($this->stream,$messageNumber,$part);
+ }
+
+ /**
+ * returnBodyStr
+ * @see http://www.php.net/manual/en/function.imap-fetchbody.php
+ * @param $messageNumber(int),part(int)
+ * @return string
+ */
+ private function returnBodyStr($messageNumber,$section){
+ return imap_fetchbody($this->stream,$messageNumber,$section);
+ }
+}
View
157 index.php
@@ -0,0 +1,157 @@
+<?
+ include_once 'classes/imap.class.php';
+
+ session_start();
+ ini_set('error_reporting',E_ALL);
+ ini_set('display_errors',1);
+ if(isset($_POST['Login'])){
+ $_SESSION['ImapLogin'] = $_POST['Login'];
+ }
+?>
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<title>PHP-IMAP</title>
+<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
+<style type="text/css">
+body{
+font-family: "Ubuntu", "Ubuntu Beta", "Bitstream Vera Sans", DejaVu Sans, Tahoma, sans-serif
+color: #333;
+font-size: 16px;
+line-height:1.5;
+width:1024px;
+margin:auto;
+}
+.block{
+background-color: #F1F1ED;
+border:1px solid #333;
+padding:10px;
+margin:auto;
+}
+.round{
+-moz-border-radius: 8px;
+-webkit-border-radius: 8px;
+-khtml-border-radius: 8px;
+border-radius: 8px;
+}
+
+.code{
+font-weight:normal;
+font-size:16px;
+color:green;
+}
+.bold{
+ font-weight:bold;
+}
+</style>
+</head>
+<body>
+<div class="block round">
+<?
+if(isset($_GET['part'])){
+ echo '<h3>MailBoxes <span class="code">imap::saveAttachment( (INT) $msgno, (INT) $part, (STRING) $file )</span></h3>';
+ $imap = new Imap($_SESSION['ImapLogin']['server'],$_SESSION['ImapLogin']['username'],$_SESSION['ImapLogin']['password']);
+ $imap->saveAttachment($_GET['msgno'],$_GET['part'],$_GET['name']);
+ echo '<p>DOWNLOAD HAS BEEN SAVED TO CURRENT WORKING DIRECTORY. IF YOU RECEIVED AN ERROR CHECK PERMISSIONS ON THE FOLDER</p>';
+ DIE();
+}
+?>
+<h2>Imap Connection <span class="code">imap::__construct()</span></h2>
+<p>This is a basic demo application using the imap.class.php. It has only been built to give you
+a live working example of the class. I feel like this is better than documentation, but I'll eventually add better documentation.
+This is not meant for production use and the demo code is littered with examples of what not to do when programming.</p>
+<form method="post">
+<table>
+<tr>
+<td>Server</td>
+<td><input type="text" name="Login[server]" value="<?=@$_SESSION['ImapLogin']['server']?>" /></td>
+</tr>
+<tr>
+<td>Username</td>
+<td><input type="text" name="Login[username]" value="<?=@$_SESSION['ImapLogin']['username']?>" /></td>
+</tr>
+<tr>
+<td>Password</td>
+<td><input type="text" name="Login[password]" value="<?=@$_SESSION['ImapLogin']['password']?>" /></td>
+</tr>
+<tr>
+<td></td>
+<td><input type="submit" value="Connect" /></td>
+</tr>
+</table>
+</form>
+<?
+if(isset($_SESSION['ImapLogin'])){
+
+ include_once 'classes/imap.class.php';
+ $imap = new Imap($_SESSION['ImapLogin']['server'],$_SESSION['ImapLogin']['username'],$_SESSION['ImapLogin']['password']);
+ $imapObj = $imap->returnImapMailBoxmMsgInfoObj();
+ echo '<h3>Mailbox Stats <span class="code">imap::returnImapMailBoxmMsgInfoObj()</span></h3><p>Unread: ('.$imapObj->Unread.') Deleted: ('.$imapObj->Deleted.') Emails: ('.$imapObj->Nmsgs.') Size: ('.round($imapObj->Size/1024/1024,1).' MB)</p>';
+ echo '<h3>MailBoxes <span class="code">imap::returnMailboxListArr()</span></h3>';
+ $mailBoxArr = $imap->returnMailboxListArr();
+
+ if(is_array($mailBoxArr)){
+ echo '<ul>';
+ foreach($mailBoxArr as $i){
+ echo '<li><a href="?mailbox='.urlencode($i).'">'.$i.'</a></li>';
+ }
+ echo '</ul>';
+ }
+
+ if(isset($_GET['msgno'])){
+ echo '<h3>Read Email <span class="code">imap::returnEmailMessageArr( (INT) $msgno )</span></h3>';
+ $email = $imap->returnEmailMessageArr(urldecode($_GET['msgno']));
+ $attachments = '';
+ if(isset($email['attachments'])){
+
+ $attachments = '<h4>Attachments</h4>
+ <em>NOTE: In the demo attachments are saved to the current working directory. Make sure this script has write permissions to the folder.</em>
+ <ul>';
+
+ foreach($email['attachments'] as $i){
+ $attachments.= '<li><a href="?mailbox='.urlencode($_GET['mailbox']).'&msgno='.trim($_GET['msgno']).'&part=2&name='.$i['name'].'" target="_blank">'.$i['name'].' ('.round(1024/$i['bytes'],1).' KB)</a></li>';
+ }
+
+ $attachments.= '</ul>';
+ }
+
+ echo '<div class="block round">
+ <h3>'.$email['header']['subject'].'</h3>
+ Date: '.$email['header']['date'].'<br/>
+ From: '.$email['header']['from'].'<br/>
+ Size: '.round(1024/$email['header']['size'],1).' KB<br/>
+ <hr/>
+ <h4>Body</h4>
+ '.base64_decode($email['html']).$attachments.'
+ </div>';
+ }
+
+ if(isset($_GET['mailbox'])){
+ echo '<h3>List Emails <span class="code">imap::returnMailBoxHeaderArr( (STRING) $mailbox )</span></h3>';
+ $emailArr = $imap->returnMailBoxHeaderArr(urldecode($_GET['mailbox']));
+ rsort($emailArr);
+
+ if(is_array($emailArr)){
+ echo '<ul>';
+ foreach($emailArr as $i){
+ echo '<li>
+ <a href="?mailbox='.urlencode($_GET['mailbox']).'&msgno='.trim($i['msgno']).'" class="'.((strtoupper($i['status']) != 'READ') ?'bold':'').'">'.$i['subject'].'</a> ('.$i['status'].')<br/>
+ Date: '.$i['date'].'<br/>
+ From: '.$i['from'].'<br/>
+ Size: '.round(1024/$i['size'],1).' KB<br/>
+ </li>';
+ }
+ echo '</ul>';
+ }
+ }
+}
+?>
+</div>
+</body>
+<?php
+echo "<pre>";
+//print_r($imap->returnMailBoxHeaderArr());
+//print_r($imap->returnEmailMessageArr(1));
+echo "</pre>";
+
+//echo $imap->saveAttachment(2,2,'/path/to/where/you/want/the/attachment/saved'.md5('14'.date('Y-m-d H:i:s')));
Please sign in to comment.
Something went wrong with that request. Please try again.