Enhancing your Webmail program
imapfuncs.phl
\n"; print "\n"; print "
\n"; print ""; print "\n"; print "\n"; } function m_create_mailbox ($new = '') { global $MBOX, $M_REALM; global $PHP_AUTH_USER, $PHP_AUTH_PW; if (! $MBOX) { m_login("INBOX"); } if ($new == "") { return false; } $name = m_mailbox_name($new); $result = imap_createmailbox($MBOX, $name); return $result; } 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); print "\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)); print ""; print " \n"; $i = 1; $bgcolor = $M_COLOR_EVEN; print "Folder name "; print "\n"; print " \n"; if ($mailboxes) { while (list($dummy, $box) = each($mailboxes)) { $bgcolor = ($i%2 == 0)?$M_COLOR_EVEN:$M_COLOR_ODD; print "Default inbox \n"; print "\n"; print " \n"; $i++; } } print "$box \n"; print "\n"; print " \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 = ""; } print "\n"; print "
\n"; print "\n"; print " Message #$msgno: $from / $subj \n"; print " \n"; print "From: $from\n"; print "Subject: $subj\n"; print "Date: $date\n"; print "
\n"; print "$body\n"; print "\n"; print " "; print " \n"; print "\n"; print " \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_MAILSERVER\n"; $headers .= "Reply-to: $PHP_AUTH_USER@$M_MAILSERVER\n"; $headers .= "Content-Type: text/plain; charset=iso-8859-1\n"; $headers .= "Content-Transfer-Encoding: 8bit\n"; $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, $M_PREFIX; /* 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 . $M_PREFIX . $mbx; } function m_strip_mbox_name ($mbx) { global $M_PREFIX; ereg("$M_PREFIX(.*)$", $mbx, $regs); return $regs[1]; } function m_reject($dom) { Header("HTTP/1.0 401 Unauthorized"); Header("WWW-authenticate: basic realm=\"$dom\""); print "Access denied\n"; exit; } /* make sure there is NO trailing space here!!! */ ?>
Page 6 of 7
This article was originally published on May 25, 1999