一個簡單的Webmail系統 (轉)

worldblog發表於2007-12-02
一個簡單的Webmail系統 (轉)[@more@]

一個簡單的
  來源:sadly
 
  這是一段Web Mail的示範程式碼,功能不是很強,但是結構還比較完整,主要的功能如檢視夾,檢視信件,回覆,寫信都有。但是中沒有考慮附件解讀和傳送的問題。 (原文在 這裡)。僅推薦給希望瞭解Webmail的網友做個參考。更加完善的Webmail請大家到 這裡查詢。
請注意:程式中使用了http功能,此功能需配合使用。


--------------------------------------------------------------------------------
將以下程式碼分別存為 index.3, imapfuncs.phl
imapfuncs.phl檔案中的$M_HOST = "localhost"; $M_MAILSERVER = "transit.fast.no";
然後瀏覽 index.php3 即可.
--------------------------------------------------------------------------------
index.php3

/* $Id: index.php3,v 1.3 1999/04/14 12:12:32 borud E$ */

/* load the IMAP library functions we've written */
include("imapfuncs.phl");

m_login($m);

?>


<?PHP echo="" "$M_SYSNAME";="" ?=""/>





if ($cmd == "delete") {
m_delete($marked, $m);
m_list($m);
}

elseif ($cmd == "display") {
m_display($n, $m);
}

elseif ($cmd == "compose" || $cmd == "reply") {
m_compose($n, $m);
}

elseif ($cmd == "send") {
m_send($to, $subject, $body);
m_list($m);
}

else {
m_list($m);
}
?>




 

 


--------------------------------------------------------------------------------
imapfuncs.phl

-*-C++-*- */

<p>

/* $Id: imapfuncs.phl,v 1.4 1999/04/14 12:12:32 borud Exp $ */

/* configurable parameters */


$M_HOST = "localhost";
$M_MAILSERVER = "transit.fast.no";

$M_COLOR_ODD = "#CCCCCC";
$M_COLOR_EVEN = "#EEEEEE";
$M_COLOR_HEAD = "#AAAAFF";
$M_COLOR_BG = "#FFFFFF";


/* globals */
$M_PORT = 143;
$M_SERVICE = "imap";
$M_SYSNAME = "Simple PHP3 IMAP Interface 1.0";
$M_MBOX = "{$M_HOST:$M_PORT/$M_SERVICE}";
$M_REALM = "IMAP Interface";
$MBOX = false;

/* functions */

function m_login ($mailbox = '')
{
global $MBOX, $M_REALM;
global $PHP_AUTH_USER, $PHP_AUTH_PW;

if ($MBOX) {
return true;
}

if (! $PHP_AUTH_USER) {
m_reject($M_REALM);
}

$MBOX = @imap_open(m_mailbox_name($mailbox), $PHP_AUTH_USER, $PHP_AUTH_PW);

if (! $MBOX) {
m_reject($M_REALM);
}

return true;
}


function m_list($mailbox = '')
{
global $MBOX, $PHP_SELF;
global $M_COLOR_ODD, $M_COLOR_EVEN, $M_COLOR_HEAD, $M_COLOR_BG;

/* if not logged into server, do so */
if (! $MBOX) {
if (! m_login($mailbox)) {
return false;
}
}

$num = imap_num_msg($MBOX);

echo "n";
echo "

n";

echo "

";
echo "";
echo "n";

for ($i=1; $i < ($num+1); $i++) {
$head = imap_header($MBOX, $i, 50, 50, 0);

$from = $head->fetchfrom;
$subj = $head->fetchsubject;
$date = m_date_format($head->date);

$bgcolor = ($i%2 == 0)?$M_COLOR_ODD:$M_COLOR_EVEN;

echo "

n";
echo " n";
echo " ";
echo "";
echo "n";
echo "n";
}

if ($num <= 0) {
echo "

n";
}

echo "

n";

echo "

FromSubjectDate
$from$subj$date

";
echo "No messages in mailbox";
echo "

";
echo "";
echo "";
echo "";
echo "
n";
echo "n";

return true;
}


function m_display($msgno, $mailbox = '')
{
global $MBOX, $M_COLOR_HEAD, $M_COLOR_BG;
global $PHP_SELF;

if (! $MBOX) {
if (! m_login($mailbox)) {
return false;
}
}

$struc = imap_fetchstructure($MBOX, $msgno);
if (! $struc) {
return false;
}

$head = imap_header($MBOX, $msgno, 50, 50, 0);

$from = $head->fromaddress;
$subj = $head->subject;
$date = $head->date;
$body = htmlentities(imap_body($MBOX, $msgno));

echo "

n";
echo "n";
echo "n";
echo "n";
echo "n";
echo "
Message #$msgno: $from / $subj
n";
echo "From: $fromn";
echo "Subject: $subjn";
echo "Date: $daten";
echo "
n";
echo "$bodyn";
echo "
";

echo "

";
echo "n";
echo "n";
echo "n";
echo "";
echo "";
echo "  ";

echo "

n";
echo "
n";

return true;
}

function m_delete ($msgno, $mailbox='')
{
global $MBOX;

if (is_array($msgno)) {
while (list($dummy, $num) = each($msgno)) {
imap_delete($MBOX, $num);
}
imap_expunge($MBOX);
} else {
return false;
}

return true;
}

function m_compose ($msgno='', $mailbox='')
{
global $MBOX, $M_COLOR_HEAD, $M_COLOR_BG;
global $PHP_SELF, $PHP_AUTH_USER, $M_MAILSERVER;

if ($msgno != '') {
$head = imap_header($MBOX, $msgno, 150, 150, 0);
$to = $head->fromaddress;
$subject = "Re: " . $head->subject;
$body = "$to wrote:n";
$body .= ereg_replace("n","n>", "n" . imap_body($MBOX, $msgno));
} else {
$to = "";
$subject = "";
$body = "";
}

echo "

n";
echo "
n";
echo "n";
echo "";
echo "n";

echo "

";
echo "n";

echo "

n";

echo "

";

echo "

To:
Subject:
";
echo "n";
echo "
";
echo "";
echo "";
echo "";
echo "
n";
echo "
n";
}


function m_send ($to, $subject, $body)
{
global $PHP_AUTH_USER, $M_MAILSERVER, $M_SYSNAME;

if ($PHP_AUTH_USER && $M_MAILSERVER && $to && $body) {

$headers = "From: $PHP_AUTH_USER@$M_MAILSERVERn";
$headers .= "Reply-to: $PHP_AUTH_USER@$M_MAILSERVERn";
$headers .= "Content-Type: text/plain; charset=iso-8859-1n";
$headers .= "Content-Traner-Encoding: 8bitn";
$headers .= "X-Mailer: $M_SYSNAME/" . phpversion() . "n";

return mail($to, $subject, $body, $headers);
}

return false;
}

 

function m_date_format($datestr)
{

if (ereg("([[:digit:]]{1,2})[[:space:]]+([[:alpha:]]{3})[[:space:]]+([[:digit:]]{4})",
$datestr, $regs)) {
return $regs[0];
}

return $datestr;
}

function m_mailbox_name ($mbx)
{
global $M_MBOX;

/* no mailbox specified means we map it to the inbox */
if ($mbx == "") {
return $M_MBOX . "INBOX";
}

/* replace some special chars */
$mbx = ereg_replace("[^[:alnum:]]", "_", $mbx);

return $M_MBOX . $mbx;
}


function m_reject($)
{
Header("HTTP/1.0 401 Unauthorized");
Header("WWW-authenticate: basic realm="$dom"");
echo "Access deniedn";
exit;
}

 

/* make sure there is NO trailing space here!!! */
?>

 
 

--------------------------------------------------------------------------------

  編輯整理:中國PHP自由聯盟
  [關閉視窗] - 2000-03-28 -
 
 

Copyright ? 2000 Chinese PHP Power All rights reserved. 


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-987482/,如需轉載,請註明出處,否則將追究法律責任。

相關文章