#native_company# #native_desc#
#native_cta#

HotMAIL

By Steven Chalker
on February 10, 2002

Version: 1.5

Type: Sample Code (HOWTO)

Category: HTML

License: GNU General Public License

Description: This is kind of like HotMail, I just call it Hotmail because It looks like it.

<?php
// You need PHP4 (because it's using sessions) to work this.

// ------------ Configuration

$optionvalue="IMAP"; //set it to POP3 if using pop connection
$serverdo="mail.something.com"; //Server
$myname="Airlines Webmail"; //Who's webmail?

// ----------------------------
session_start();
echo "<title> $myname </title>n";
$javascript_for_select = "<script language='javascript'><!--n";

$javascript_for_select .= "function selectState(state) {n";

$javascript_for_select .= "for(i=0; i < document.forms[0].elements.length;i++){n";

$javascript_for_select .= "document.forms[0].elements[i].checked=state;n";

$javascript_for_select .= "}n}n";

$javascript_for_select .= "//--></script>n";



$select_all = "&nbsp;&nbsp;<input type='button' value='Select All' onclick='";

$select_all .= "selectState(true)'>&nbsp;&nbsp;";



$unselect_all = "<input type='button' value='Unselect All' onclick='";

$unselect_all .= "selectState(false)'>&nbsp;&nbsp;";

if($CMD == "topframe") {

if(!$folder){ $folder = "INBOX"; }

if($type == "POP3") {

$mbox = imap_open("{".$session_server.":110/pop3}INBOX.".urldecode($foldery), $sesson_username, $session_password) or die("<h2>Invalid IMAP/POP3 Server</h2> The IMAP/POP3 Server ".$server." did not connect successfully.");

} elseif($type == "IMAP") {

$mbox = imap_open("{".$session_server."}".$foldery, $session_username, $session_password) or die("<h2>Invalid IMAP/POP3 Server</h2> The IMAP/POP3 Server ".$server." did not connect successfully.");

}

echo "<meta HTTP-EQUIV='refresh' CONTENT='1500'>n";

echo "<body BGCOLOR=BLUE><font COLOR=WHITE>n";

$mcheck = imap_check($mbox);

$mnum = $mcheck->Nmsgs;

if($mnum == "0") {

echo "<br>You have no messages in your INBOX.";

} else {

$overview = imap_fetch_overview($mbox, "1:$mnum", 0);

echo "<form ACTION='webmail.php?CMD=delete' METHOD=post name=delete>n";

echo "<input TYPE=submit VALUE=Delete>n";

echo $select_all;

echo $unselect_all;

echo "<table BORDER=0>n";
$s=sizeof($overview);
echo "<tr><td>FROM</td><td>SUBJECT</td><td>DATE</td><td>Delete</td></tr>";

for($i=$s-1; $i >= 0; $i--) {

$val=$overview[$i];

$nb=$val->msgno;

$rawsubject = imap_mime_header_decode($val->subject);

$subject = $rawsubject[0]->text;

if($subject == ''){ $subject = "[no subject]"; }

$rawfrom = imap_mime_header_decode($val->from);

$from = $rawfrom[0]->text;
$header = imap_header($mbox,$nb);
if($header->Unseen == "U") {
$font = "bgcolor="black"";
} else {
$font = "ignore="true"";
}
$date=$val->date;
$isfont = ($font == "bgcolor="black"") ? "<!-- yes -->" : "<!-- no -->"; 
echo "<tr ".$font."><td>".$isfont."<a HREF='webmail.php?CMD=view&num=$nb&foldery=$folder' STYLE='cursor:hand' STYLE='color:white'>";

echo "$from</a></td></font><td>".$isfont."<a HREF='webmail.php?CMD=view&num=$nb&foldery=$folder' STYLE='color:white'>$subject</a></td></font><td>".$isfont."<a HREF='webmail0.7.htm?CMD=view&num=$nb&foldery=$folder' STYLE='color:white'>$date</a></td><td><input TYPE='checkbox' NAME='num[]' VALUE=$nb></td></tr></font>";

}

echo "</table>n</body>";

echo $javascript_for_select;

}

imap_close($mbox);

} elseif($CMD == "source") {

if($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}".urldecode($foldery),$session_username,$session_password);

} elseif($type == 'IMAP') {

$mbox=imap_open("{".$session_server."}".urldecode($foldery),$session_username,$session_password);

}

$structure=imap_fetchstructure($mbox,$num);

echo "<pre>";

$rb=imap_fetchbody($mbox,$num,0);
$rb=str_replace("<","&lt;",$rb);
echo $rb;

echo "nn";
$mb=imap_body($mbox,$num);
$mb=str_replace("<","&lt;",$mb);
echo $mb;

echo "</pre>";

} elseif($CMD == "view") {

if($type == "POP3") {

$mbox = imap_open("{".$session_server.":110/pop3}".urldecode($foldery), $session_username, $session_password);

} elseif($type == "IMAP") {

$mbox = imap_open("{".$session_server."}".urldecode($foldery), $session_username, $session_password);

}
$h=imap_headers($mbox);
imap_setflag_full($mbox,imap_uid($mbox,substr($h[$num],5,4)),'SEEN',SE_UID);

$info=imap_header($mbox, $num);

$from=$info->from[0];

echo "<style> A { color:yellow } </STYLE>";

echo "<body BGCOLOR=BLUE>n";

echo "<table BORDER=0 BGCOLOR=BLUE><font COLOR=WHITE>";

echo "<tr><td><a HREF='webmail.php?CMD=topframe&folder=$foldery'>Back to Messages</a></td><td><a HREF='javascript:void(window.open("webmail.php?CMD=reply&num=$num&foldery=$foldery"))'>Reply to Message</a></td><td><a HREF='webmail.php?CMD=delete&num=$num&foldery=$foldery'>Delete This Message</a></td><td><a HREF='javascript:void(window.open("webmail.php?CMD=compose"))'>Compose Message</a></td></tr>";
$rawdfrom = imap_mime_header_decode($info->fromaddress);
$from = $rawdfrom[0]->text;
echo "<tr><td>From</td><td>$from</td></tr>";

echo "<tr><td>To</td><td>$info->toaddress</td></tr>";

if($info->ccaddress){ echo "<tr><td>CC</td><td>$info->ccaddress</td></tr>"; }

if($info->bccaddress){ echo "<tr><td>BCC</td><td>$info->bccaddress</td></tr>"; }

echo "<tr><td>Date</td><td>$info->Date</td></tr>";
$rawdsubject = imap_mime_header_decode($info->Subject);
$subject = $rawdsubject[0]->text;
echo "<tr><td>Subject</td><td>$subject</td></tr>";

$structure = imap_fetchstructure($mbox, $num);

$structure=imap_fetchstructure($mbox,$num);

if(isset($structure->parts)) {

echo "<tr><td>Attachments</td><td>";

while(list($key,$val) = each($structure->parts)) {

if($key > 0) {

if($val->ifparameters) {

$attach=$val->parameters;

echo "<a href='webmail.php?CMD=Attachview&num=$num&part=$key&foldery=$foldery' TARGET=blanke>";

$attachmenttext=$attach[0]->value;

$attachmenttext=str_replace("US-ASCII", "HTML/HTM Document", $attachmenttext);

$attachmenttext=str_replace("us-ascii", "HTML/HTM Document", $attachmenttext);

echo $attachmenttext;

echo "</a>,&nbsp;";

} else {

echo "<a href='webmail.php?CMD=Attachview&num=$num&part=$key&foldery=$foldery' TARGET=blanke>";

echo "(unnamed)</a>,&nbsp;";

}

}

}

}

echo "</tr>n</td>";

echo "<tr><td>n";

echo "<a HREF='javascript:void(window.open("webmail.php?CMD=source&num=$num&foldery=$foldery"));'>Source</a>";

echo "</td></tr>n";

echo "</table><br><pre>";

if(isset($structure->parts)) {

$body=imap_fetchbody($mbox,$num,1);

} else {

$body=imap_body($mbox,$num);

}
$partone = $structure;
$parttwo = $partone->parts[0];
if($parttwo->encoding == 3) {

$ambody = imap_base64($body);

} elseif($parttwo->encoding == 4) {

$ambody = imap_qprint($body);

} else {

$ambody = $body;

}


echo $ambody;

echo "</pre><br>";

echo "<table BORDER=0>";

echo "<tr><td><a HREF='webmail.php?CMD=topframe&folder=$foldery'>Back to Messages</a></td><td><a HREF='javascript:void(window.open("webmail.php?CMD=reply&num=$num&foldery=$foldery"))'>Reply to Message</a></td><td><a HREF='webmail.php?CMD=delete&num=$num&foldery=$foldery'>Delete This Message</a></td><td><a HREF='javascript:void(window.open("webmail.php?CMD=compose"))'>Compose Message</a></td></tr>";

echo "</table>";

imap_close($mbox);

} elseif($CMD == "reply") {

if($type == "POP3") {

$mbox = imap_open("{".$session_server.":110/pop3}".urldecode($foldery), $session_username, $session_password);

} elseif($type == "IMAP") {

$mbox = imap_open("{".$session_server."}".urldecode($foldery), $session_username, $session_password);

}

$replytext=imap_header($mbox, $num);

$to=$replytext->from[0];

echo "<body BGCOLOR=BLUE><font COLOR=WHITE>n";

echo "<form ACTION='webmail.php?CMD=send' enctype='multipart/form-data' METHOD='post'>n";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='248000012'>n";
echo "<h1>Reply to this Message</h1>";

echo "To: <input type='text' name='to' value='$to->mailbox@$to->host'><br>n";

echo "From: <input type='text' name='fr' value='$replytext->toaddress'><br>n";
echo "CC: <input type='text' name=cc value=''><br>n";
echo "BCC: <input type='text' name=bcc value='><br>n";
$subj = $replytext->Subject;

$subj = str_replace("Re:", "", $subj);

echo "Subject: <input type='text' name='su' value='Re: $subj'><br>n";
echo "<textarea ROWS='20' cols='45' name='ms'>";

$structure=imap_fetchstructure($mbox,$num);

if(isset($structure->parts)) {

$body = imap_fetchbody($mbox,$num,1);

} else {

$body = imap_body($mbox,$num);

}

$body = str_replace("n", "n&gt; ", $body);

$body = str_replace("<", "&lt;", $body);

$body = str_replace(">", "&gt;", $body);

echo "nnnOn $replytext->Date, $replytext->fromaddress wrote:n";

echo "> ".$body;

echo "<textarea><br>n";

echo "<input TYPE=submit value='Send Message'>";

imap_close($mbox);

} elseif($CMD == "compose") {
$generated="";
$generated=$session_username."@".$session_server;
$generated=str_replace("mail.","",$generated);
$generated=str_replace("pop.","",$generated);
$generated=str_replace("pop3.","",$generated);
$generated=str_replace("imap.","",$generated);
echo "<form ACTION='webmail.php?CMD=send' enctype='multipart/form-data' METHOD=post>n";
echo "<input type='hidden' name='MAX_FILE_SIZE' VALUE='2480000021'>n";
echo "<body BGCOLOR='blue'><font COLOR='white'>n";

echo "<h1>Compose a Message</h1>n";

echo "To: <input type='text' name='to' value=''><br>n";

echo "From: <input type='text' name=fr value='$generated'><br>n";
echo "CC: <input type='text' name=cc value=''><br>n";
echo "BCC: <input type='text' name=bcc value=''><BR>n";
echo "Subject: <input type=text name=su value=''><br>n";
echo "<textarea rows='20' cols=55 name=ms><textarea><br>n";

echo "<input type='submit' value='Send'></form>n";

} elseif($CMD == "delete") {

if($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}".urldecode($foldery), $session_username, $session_password);

} elseif($type == "IMAP") {

$mbox=imap_open("{".$session_server."}".urldecode($foldery), $session_username, $session_password);

}

for($i=0; $i < sizeof($num); $i++) {

imap_delete($mbox,$num[$i]);

}

imap_expunge($mbox);

echo "<script> window.location='webmail.php?CMD=topframe'; </SCRIPT>";

imap_close($mbox);

} elseif($CMD == "Attachview") {

if($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}".urldecode($foldery),$session_username,$session_password);

} elseif($type == "IMAP") {

$mbox=imap_open("{".$session_server."}".urldecode($foldery),$session_username,$session_password);

}

$mimetype=array("text","multipart","message","application","audio","image","video","other");



$structure=imap_fetchstructure($mbox,$num);

$attach_body = imap_fetchbody($mbox,$num,$part+1);

$atype=$structure->parts[$part]->type;

if($atype=="") $atype=0;

$asubtype=$structure->parts[$part]->subtype;

if($asubtype == "") $asubtype='PLAIN';

$attach_type=$mimetype[$atype]."/".$asubtype;

$attach_name=$structure->parts[$part]->parameters[0]->value;

if($attach_name = "") $attach_name='(unnamed)';



$anencode=$structure->parts[$part]->encoding;

if($anencode == 3){

$return_content=imap_base64($attach_body);

} elseif($anencode == 4) {

$return_content=imap_qprint($attach_body);

} else {

$return_content=$attach_body;

}

header("Content-type: $attach_type");

if(($atype != 0) && ($atype != 5)){

header("Content-Disposition: attachment; filename=$attach_name");

}

echo $return_content;

} elseif($CMD == "folders") {

if($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}", $session_username, $session_password);

$folderr=imap_listmailbox($mbox,"{".$session_server.":110/pop3}","*");

$am="{".$session_server.":110/pop3}";

} elseif($type == "IMAP") {

$mbox=imap_open("{".$session_server."}",$session_username,$session_password);

$am="{".$session_server."}";

$folderr=imap_listmailbox($mbox,"{".$session_server."}","*");

}

echo "<body BGCOLOR=blue><font COLOR=white>n";

echo "<form ACTION='webmail.php?CMD=folders_do' METHOD=post>n";

echo "Folders<br><br>";

echo "Create New Mailbox: <input type='text' name='newbox'> <input type=submit value=Make>";

echo "<br><br>";

echo "Rename Mailbox: <select name='renameFolder'>n";

while(list($useless_key,$helped_val) = each($folderr)) {

$helped_val = str_replace($am,"",$helped_val);

echo "<option VALUE='INBOX.".urlencode($helped_val)."'>$helped_val</option>n";

}

echo "</select>&nbsp;";

echo "<input TYPE='text' name='newfolder'>&nbsp;";

echo "<input TYPE='submit' VALUE='Rename'><br><br>";

echo "Delete Mailbox <select NAME='delefolder'>n";

for($i=0; $i < count($folderr); $i++) {

$folderr[$i] = str_replace($am,"",$folderr[$i]);

echo "<option VALUE='".$folder[$i]."'>".$folderr[$i]."</option>n";

}

echo "</select>&nbsp;";

echo "<input TYPE=submit VALUE='Delete Folder'>n";

echo "</form>";

} elseif($CMD == "folders_do") {

if($type == "IMAP") { //go opposite

$mbox=imap_open("{".$session_server."}", $session_username,$session_password);

$ish="{".$session_server."}";

} elseif($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}",$session_username, $session_password);

$ish="{".$session_server.":110/pop3}";

}

if($newbox) {

imap_createmailbox($mbox,imap_utf7_encode($ish."INBOX.$newbox"));

}

if($newfolder) {

imap_renamemailbox($mbox,$ish.$renameFolder,$ish."INBOX.".$newfolder);

}

if($delefolder) {

imap_deletemailbox($mbox,$ish."INBOX.".$delefolder);

}

echo "<script> window.location='webmail.php?CMD=folders'; </SCRIPT>";

} elseif($CMD == "leftframe") {

if($type == "POP3") {

$mbox=imap_open("{".$session_server.":110/pop3}", $session_username, $session_password) or exit;

} elseif($type == "IMAP") {

$mbox=imap_open("{".$session_server."}",$session_username,$session_password) or exit;

}

echo "<meta HTTP-EQUIV='refresh' CONTENT='300'>n";

echo "<style>n";

echo "A { color:yellow; }n";

echo "</STYLE>n";

echo "<form NAME=thising>n";

echo "<body BGCOLOR=BLUE><font COLOR=WHITE>n";

echo "<select NAME='selectfolder' ONCHANGE='window.parent.msg.location=document.thising.selectfolder.options[document.thising.selectfolder.selectedIndex].value'>n";

if($type == "POP3") {

$folder = imap_listmailbox($mbox,"{".$server.":110/pop3}","*");

} elseif($type == "IMAP") {

$folder = imap_listmailbox($mbox,"{".$server."}","*");

}

while(list($key,$val) = each($folder)) {

if($type == "POP3") {

$status = imap_status($mbox,"{".$server.":110/pop3}INBOX",SA_ALL);

$val = str_replace("{".$server.":110/pop3}", "", $val);

} else {

$status = imap_status($mbox,"{".$server."}INBOX",SA_ALL);

$val = str_replace("{".$server."}", "", $val);

}



echo "<option VALUE='webmail.php?CMD=topframe&folder=".urlencode($val)."'>$val</option>n";

}

echo "</select>";

echo "<br><br>NewMail 0.7<br><br>";

echo "<a HREF='webmail.php?CMD=topframe' TARGET='msg'>INBOX</a><br><br><br><br>";

echo "<a HREF='webmail.php?CMD=compose' TARGET='compwin'>Compose</a><br><br><br><br>";

echo "<a HREF='webmail.php?CMD=logout' TARGET='_top'>Log Out</a><br><br>";

echo "<a HREF='webmail.php?CMD=folders' TARGET='msg'>Folders</a><br><br>";

echo "</body>";

if($status->unseen) {

echo "<!-- $status->unseen messages(unseen) -->";

}

} elseif($CMD == "logout") {

session_destroy();

echo "<center><h1>Log Out</h1>n";

echo "You have been logged out. <a href='webmail.php'>Re-Login</a>";

if($ReturnToPageLink) {

echo "<br><br><br><a HREF='$ReturnToPageLink?returnedfrom=NewMail'>Return to $ReturnToPageLink</a>";

}

echo "</center>";

} elseif($CMD == "send") {
$h="From: $frn";
if($cc) $h.="CC: $ccn";
if($bcc) $h.="BCC: $bccn";
mail($to,$su,$ms,$h);
echo "Mail is Sent.<br>";
echo "<a HREF='javascript:with(window) close()'>Close</a>n";
} elseif($CMD == "framed") {

echo "<html>n";

echo "<frameset frameborder='NO' border='0' framespacing='0' COLS='109,*'>n";

echo "<frame SRC='webmail.php?CMD=leftframe' NAME='left' SCROLLING=no>n";

echo "<frame SRC='webmail.php?CMD=topframe' NAME='msg'>n";

echo "</frameset>";

echo "</html>n";

} elseif($CMD == "cookie") {

session_register("session_server"); $session_server=$host;
session_register("session_username"); $session_username=$user;
session_register("session_password"); $session_password=$pass;
session_register("type"); $type = $opt;

echo "<script> window.location='webmail.php?CMD=framed' </SCRIPT>";

} elseif($CMD == "GetCode") {

echo "<pre>";

echo "&lt;html&gt;n";

echo "&lt;form action='http://www.101h.com/~stevenc/webmail.php?CMD=cookie' method='post'>n";

echo "&lt;center>&lt;h1>Mail Check - Your Name Here &lt;/h1>&lt;/center>n";

echo "Username: &lt;input type='text' name='user'>&lt;BR>n";

echo "Password: &lt;input type='password' name=pass>&lt;BR>n";

echo "Server: Stored&lt;BR>n";

echo "&lt;input type='hidden' name='host' value='your.server.here'>n";

echo "&lt;input type='hidden' name='opt' value='IMAP'>&lt;!-- for POP3, just enter POP3 -->n";

echo "&lt;input type=submit value='Login'>n&lt;/form>n";

echo "</pre><br><br>";

echo "<a HREF='webmail.php'>Return</a>";

} else {
if($session_username || $session_password || $session_server || $type) {
echo "<SCRIPT> window.location='webmail.php?CMD=framed'; </SCRIPT>";
exit;
}

echo "<form ACTION='webmail.php?CMD=cookie' METHOD='post'>n";

echo "<center><h1>Mail Checking</h1>n";

echo "Username: <input TYPE='text' NAME='user'><br>n";

echo "Password: <input TYPE='password' NAME='pass'><br>n";

echo "<input type='hidden' name='host' value='$serverdo'>n";
echo "<input type='hidden' name='opt' value='$optionvalue'>n";

echo "<input type='submit' value='Log in'>n</form><br>n";
echo "<br><br><br>n";

echo "This is now Web mail! <a HREF='webmail.php?CMD=GetCode'>Get the Code</a> Here!";

}

?>