Build a simple Webmail System

Source: Internet
Author: User
Tags http authentication imap

This is a demonstration of web mail. Code The function is not very powerful, but the structure is still relatively complete. The main functions include viewing folders, viewing emails, replying, and writing letters. However Program The attachment interpretation and sending issues were not considered. (The original article is here ). It is only recommended to users who want to know webmail programming for reference. For more comprehensive webmail, please visit here.
Note: HTTP authentication is used in the program. This function must be used with the Apache server.

--------------------------------------------------------------------------------
Save the following code as index. php3, imapfuncs. PHL
Configure $ m_host = "localhost" in the imapfuncs. PHL file; $ m_mailserver = "transit. Fast. No ";
Then browse index. php3.
--------------------------------------------------------------------------------
Index. php3

<? PHP
/* $ ID: Index. php3, V 1.3 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 12:12:32 borud exp $ */

/* Retriable 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> </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> ";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.