国内最大的酷站演示中心!
这是一段web mail的示范代码,功能不是很强,但是结构还比较完整,主要的功能如查看文件夹,查看信件,回复,写信都有。但是程序中没有考虑附件解读和发送的问题。 (原文在 这里)。仅推荐给希望了解webmail编程的网友做个参考。更加完善的webmail请大家到 这里查找。
请注意:程序中使用了http认证功能,此功能需配合apache服务器使用。
--------------------------------------------------------------------------------
将以下代码分别存为 index.php3, imapfuncs.phl
配置imapfuncs.phl文件中的$m_host = "localhost"; $m_mailserver = "transit.fast.no";
然后浏览 index.php3 即可.
--------------------------------------------------------------------------------
index.php3
<?php
/* $id: index.php3,v 1.3 1999/04/14 12:12:32 borud exp $ */
/* load the imap library functions we/'ve written */
include("imapfuncs.phl");
m_login($m);
?>
<html>
<head>
<title><?php echo "$m_sysname"; ?></title>
</head>
<body bgcolor=#aaaaaa text=#000000 link=#440000 vlink=#440000 alink=#ff00ff>
<h1 align=center><?php echo "$m_sysname"; ?></h1>
<hr size=1 noshade>
<p>
<?php
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);
}
?>
<p>
<hr size=1 noshade>
<?php echo "user: $php_auth_user"; ?>
</body>
</html>
--------------------------------------------------------------------------------
imapfuncs.phl
<?php /* -*-c++-*- */
/* $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 "<form action=$php_self type=post>/n";
echo "<center><table border=0 cellspacing=0 cellpadding=1 width=90%>/n";
echo "<tr align=left bgcolor=$m_color_head>";
echo "<th> </th><th>from</th><th>subject</th><th>date</th>";
echo "</tr>/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 "<tr bgcolor=/"$bgcolor/">/n";
echo " <td><input type=checkbox name=marked[] value=$i></td>/n";
echo " <td>$from</td>";
echo "<td><a href=/"$php_self?m=$mailbox&n=$i&cmd=display/">$subj</a></td>";
echo "<td>$date</td>/n";
echo "</tr>/n";
}
if ($num <= 0) {
echo "<tr><td align=center colspan=4 bgcolor=$m_color_bg><br>";
echo "<font size=+1 color=#ff0000>no messages in mailbox</font>";
echo "<br><br></td></tr>/n";
}
echo "<tr bgcolor=$m_color_head><td colspan=4>";
echo "<input type=submit name=cmd value=delete>";
echo "<input type=submit name=cmd value=compose>";
echo "<input type=submit name=cmd value=refresh>";
echo "</td></tr>/n";
echo "</table></center>/n";
echo "</form>/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 "<center>/n";
echo "<table border=0 cellspacing=0 cellpadding=10 width=100%>/n";
echo "<tr bgcolor=$m_color_head><th>message #$msgno: $from / $subj</th></tr>/n";
echo "<tr bgcolor=$m_color_bg><td><pre>/n";
echo "from: $from/n";
echo "subject: $subj/n";
echo "date: $date/n";
echo "<hr size=2 noshade>/n";
echo "$body/n";
echo "</pre></td></tr>/n";
echo "<tr bgcolor=$m_color_head><td>";
echo "<form action=$php_self method=post>";
echo "<input type=hidden name=m value=$mailbox>/n";
echo "<input type=hidden name=n value=$msgno>/n";
echo "<input type=hidden name=marked[] value=$msgno>/n";
echo "<input type=submit name=cmd value=back>";
echo "<input type=submit name=cmd value=reply>";
echo " <input type=submit name=cmd value=delete>";
echo "</td></tr>/n";
echo "</table>/n";
echo "</center>/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 "<center>/n";
echo "<form method=post action=/"$php_self/">/n";
echo "<table border=0 cellpadding=5 cellspacing=0>/n";
echo "<tr bgcolor=$m_color_head><td align=right>to:</td>";
echo "<td><input type=text name=to value=/"$to/" size=50></td></tr>/n";
echo "<tr bgcolor=$m_color_head><td align=right>subject:</td>";
echo "<td><input type=text name=subject value=/"$subject/" size=50></td></tr>/n";
echo "<tr bgcolor=$m_color_bg><td colspan=2>";
echo "<textarea cols=76 rows=10 name=body wrap=hard>$body</textarea>";