send.php3
上传用户:xuanqunsh
上传日期:2007-01-04
资源大小:58k
文件大小:6k
源码类别:

WEB邮件程序

开发平台:

PHP

  1. <?php
  2. # ---------------------------------------------------------------
  3. # phpop
  4. # A WWW based POP3 basic mail user agent (MUA)
  5. # Copyright (C) 1999  Padraic Renaghan
  6. # Licensed under terms of GNU General Public License
  7. # (see http://www.renaghan.com/phpop/source/LICENSE)
  8. # ---------------------------------------------------------------
  9. # $Id: send.php3,v 1.9 2000/04/12 23:23:11 prenagha Exp $
  10. # ---------------------------------------------------------------
  11. include("poprepend.inc");
  12. page_open(array( "sess" => "pop_sess",
  13.                  "auth" => "pop_auth"));
  14. if ($phpop->allow_send_attachment) {
  15.   $body_tpl = "send.mimebody.tpl";
  16. } else {
  17.   $body_tpl = "send.body.tpl";
  18. }
  19. $tpl->define(array(
  20.  standard    => "common.standard.tpl",
  21.  body        => $body_tpl,
  22.  msg         => "common.message.tpl",
  23.  error_msg   => "common.error_message.tpl"
  24. ));
  25. set_standard("send", &$tpl);
  26. # do we need to get the message
  27. $get_msg = TRUE;
  28. ## Check if there was a submission
  29. while ( is_array($HTTP_POST_VARS) 
  30.      && list($key, $val) = each($HTTP_POST_VARS)) {
  31.   switch ($key) {
  32.   ## send the message
  33.   case "phpop_send":
  34.     $get_msg = FALSE;
  35.     $email = new email_class;
  36. if (! $email->setFrom($from)) {
  37.       $error_msg .= "<br>Missing or invalid from address.";
  38.     }
  39. if (! $email->setTo($to)) {
  40.       $error_msg .= "<br>Missing or invalid To address(es). Only the email address part is permitted (e.g. &quot;pop@linux.com&quot;, not &quot;Bob Smith &lt;pop@linux.com&gt;&quot)";
  41.     }
  42. if (!empty($cc)) {
  43.       if (! $email->setCC($cc)) {
  44.         $error_msg .= "<br>Invalid CC address(es).";
  45.       }
  46.     }
  47. if (!empty($bcc)) {
  48.       if (! $email->setCC($bcc)) {
  49.         $error_msg .= "<br>Invalid BCC address(es).";
  50.       }
  51.     }
  52. if (! $email->setSubject($subject)) {
  53.       $error_msg .= "<br>Missing Subject.";
  54.     }
  55. if (! $email->setText($body)) {
  56.       $error_msg .= "<br>Missing body.";
  57.     }
  58. #
  59. # setAttachmentMaxSize
  60. # in email.inc then check each one.
  61. #
  62. if (!empty($phpop->site_headers)) {
  63.       if (! $email->setAddlHdrs($phpop->site_headers)) {
  64.         $error_msg .= "<br>Invalid additional headers.";
  65.       }
  66.     }
  67. # stuping MSIE puts a "none" in the filename
  68. if ($attachment != "none"
  69.     &&  !empty($attachment) 
  70.     &&  $phpop->allow_send_attachment) {
  71.       if (! $email->setAttachments($attachment, $attachment_name)) {
  72.         $error_msg .= "<br>Invalid attachment.";
  73.       }
  74.     }
  75.     if (isset ($error_msg)) {
  76.   break;
  77. }
  78.     if ($email->send()) {
  79.       $msg .= "<br>Message sent to $to.";
  80.     } else {
  81.       $error_msg .= "<BR>ERROR sending message to $to.";
  82.     }
  83.   break;
  84.   ## reset the form
  85.   case "phpop_reset":
  86.   break;
  87.   
  88.   default:
  89.   break;
  90.  }
  91. }
  92. if ( $get_msg ) {
  93.   $from = sprintf("%s@%s", $auth->auth["uname"], $auth->auth["server"]);
  94.   unset ($body);
  95.   unset ($msg_from);
  96.   unset ($reply_to);
  97.   unset ($sender);
  98.   unset ($to);
  99.   unset ($subject);
  100.   unset ($cc);
  101.   
  102.   if ( $id > 0 ) {
  103.     # connect to the pop server
  104.     if ( ! $pop3->connect($auth->auth["server"], $auth->auth["port"]) ) {
  105.       $error_msg .= sprintf("Failed to connect to POP server %s on port %s.<br><small>%s</small>", $auth->auth["server"], $auth->auth["port"], $pop3->ERROR);
  106.     }
  107.     # login to the POP server
  108.     $count = $pop3->apop($auth->auth["uname"], $auth->auth["passwd"]);
  109.     #if ( (!$count) or ($count == -1) ) {
  110.     if ( $count < 0 ) {
  111.       $error_msg .= "Failed to login to POP server<br><small>$pop3->ERROR</small>";
  112.     }
  113. # get the entire message
  114.     $msg_array = $pop3->get($id);
  115. # loop through the headers and look for the stuff
  116. # we are interested in...
  117.     $in_body = false;
  118.     while ( list ($linenbr, $line) = each ($msg_array) ) {
  119.       if ( (!$msg_from) and (ereg ("@", $line)) and (eregi ("^From:(.*)", $line, $match)) ) {
  120.     $msg_from = htmlentities(trim ( $match[1] ));
  121.   } elseif ( (!$reply_to) and (ereg ("@", $line)) and (eregi ("^Reply-To:(.*)", $line, $match)) ) {
  122.     $reply_to = htmlentities(trim ( $match[1] ));
  123.   } elseif ( (!$sender) and (ereg ("@", $line)) and (eregi ("^Sender:(.*)", $line, $match)) ) {
  124.     $sender = htmlentities(trim ( $match[1] ));
  125.   } elseif ( (!$subject) and (eregi ("^Subject:(.*)", $line, $match)) ) {
  126.     # if replying, then put RE: at the begining of the subject
  127.   if ( $action == "R" ) {
  128.       $subject = "RE: " . htmlentities(trim ( $match[1] ));
  129.   } else {
  130.       $subject = "FW: " . htmlentities(trim ( $match[1] ));
  131.   }
  132.   } elseif ( eregi ("^r", $line) ) {
  133.     # if forward then add two blank lines, then a fwd line
  134.   if ( ($action == "F") and (!$in_body) ) {
  135.       $body .= "nn----- Forwarded Message ------n";
  136.   $body .= "From: $msg_fromn";
  137.         }
  138.     $in_body = true;
  139.  
  140.   } elseif ( $in_body ) {
  141.     # if replying, then put a ">" before each line
  142.   if ( $action == "R" ) {
  143.       $body .= "&gt;" . htmlentities($line);
  144.   } else {
  145.       $body .= htmlentities($line);
  146.   }
  147.   }
  148.     } # for each line in the message header
  149.   # to is first found of reply-to, from, sender headers
  150.     if ( $action == "F" ) {
  151.     $to = "";
  152.     } elseif ( $reply_to ) {
  153.     $to = $reply_to;
  154.   } elseif ( $msg_from ) {
  155.     $to = $msg_from;
  156.   } elseif ( $sender ) {
  157.     $to = $sender;
  158.   } else {
  159.     $to = "";
  160.   $msg .= "<br>No return address found in message headers!";
  161.   }
  162.     if (!empty($to)) {
  163.       if (eregi("([._a-z0-9-]+)@([._a-z0-9-]+)", $to, $match)) {
  164.         $to = $match[1] . "@" . $match[2];
  165.       } else {
  166.         $msg .= "<br>Unable to parse return address";
  167.       }
  168.     }
  169.   # close the POP session
  170.   $pop3->quit();
  171.   } else {
  172. # if we get here we are in NEW mode
  173. # nothing to do since all fields except from are blank
  174. # except set the TO if passed in URL as ...?newto=pad@jkfdsjk.com
  175.     $to = $newto;
  176.   } # id > 0
  177. } # if get msg
  178. $tpl->assign(array(
  179.   FORM_ACTION => $sess->self_url(),
  180.   FROM        => $from,
  181.   CC          => $cc,
  182.   TO          => $to,
  183.   SUBJECT     => $subject,
  184.   MSGBODY     => $body
  185. ));
  186. # assign messages if any found
  187. if (isset ($error_msg)) {
  188.   $tpl->assign(ERROR_MSG_TEXT, $error_msg);
  189.   $tpl->parse(ERROR_MSG, "error_msg");
  190. }
  191. if (isset ($msg)) {
  192.   $tpl->assign(MSG_TEXT, $msg);
  193.   $tpl->parse(MSG, "msg");
  194. }
  195. $tpl->parse(BODY, "body");
  196. $tpl->parse(MAIN, "standard");
  197.   
  198. $tpl->FastPrint();
  199. page_close();
  200. ?>