home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / php_nuke / html / modules / webmail / libmail.php < prev    next >
Encoding:
PHP Script  |  2002-09-16  |  12.7 KB  |  387 lines

  1. <?php
  2.  
  3. /*************************************************************************/
  4.  #  Mailbox 0.9.2a   by Sivaprasad R.L (http://netlogger.net)             #
  5.  #  eMailBox 0.9.3   by Don Grabowski  (http://ecomjunk.com)              #
  6.  #          --  A pop3 client addon for phpnuked websites --              #
  7.  #                                                                        #
  8.  # This program is distributed in the hope that it will be useful,        #
  9.  # but WITHOUT ANY WARRANTY; without even the implied warranty of         #
  10.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          #
  11.  # GNU General Public License for more details.                           #
  12.  #                                                                        #
  13.  # You should have received a copy of the GNU General Public License      #
  14.  # along with this program; if not, write to the Free Software            #
  15.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              #
  16.  #                                                                        #
  17.  #             Copyright (C) by Sivaprasad R.L                            #
  18.  #            Script completed by Ecomjunk.com 2001                       #
  19. /*************************************************************************/
  20.  
  21. /*
  22.         this class encapsulates the PHP mail() function.
  23.         implements CC, Bcc, Priority headers
  24.  
  25. @version        1.3
  26.  
  27. - added ReplyTo( $address ) method
  28. - added Receipt() method - to add a mail receipt
  29. - added optionnal charset parameter to Body() method. this should fix charset problem on some mail clients
  30.  
  31. @example
  32.         include ("modules/WebMail/libmail.php");
  33.         $m= new Mail; // create the mail
  34.         $m->From( "leo@isp.com" );
  35.         $m->To( "destination@somewhere.fr" );
  36.         $m->Subject( "the subject of the mail" );
  37.         $message= "Hello world!\nthis is a test of the Mail class\nplease ignore\nThanks.";
  38.     $m->Body( $message);        // set the body
  39.         $m->Cc( "someone@somewhere.fr");
  40.         $m->Bcc( "someoneelse@somewhere.fr");
  41.         $m->Priority(4) ;        // set the priority to Low
  42.         $m->Attach( "/home/leo/toto.gif", "image/gif" ) ;        // attach a file of type image/gif
  43.         $m->Send();        // send the mail
  44.         echo "the mail below has been sent:<br><pre>", $m->Get(), "</pre>";
  45.  
  46. LASTMOD
  47.         Fri Oct  6 15:46:12 UTC 2000
  48.  
  49. @author        Leo West - lwest@free.fr
  50.  
  51. */
  52.  
  53. class Mail {
  54.         /*
  55.         list of To addresses
  56.         @var        array
  57.         */
  58.         var $sendto = array();
  59.         /*
  60.         @var        array
  61.         */
  62.         var $acc = array();
  63.         /*
  64.         @var        array
  65.         */
  66.         var $abcc = array();
  67.         /*
  68.         paths of attached files
  69.         @var array
  70.         */
  71.         var $aattach = array();
  72.         /*
  73.         list of message headers
  74.         @var array
  75.         */
  76.         var $xheaders = array();
  77.         /*
  78.         message priorities referential
  79.         @var array
  80.         */
  81.         var $priorities = array( '1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)' );
  82.         /*
  83.         character set of message
  84.         @var string
  85.         */
  86.         var $charset = "us-ascii";
  87.         var $ctencoding = "7bit";
  88.         var $receipt = 0;
  89. /*
  90.         Mail contructor
  91. */
  92.  
  93. function Mail() {
  94.         $this->autoCheck( true );
  95.         $this->boundary= "--" . md5( uniqid("myboundary") );
  96. }
  97.  
  98. /*
  99. activate or desactivate the email addresses validator
  100. ex: autoCheck( true ) turn the validator on
  101. by default autoCheck feature is on
  102. @param boolean        $bool set to true to turn on the auto validation
  103. @access public
  104. */
  105.  
  106. function autoCheck( $bool ) {
  107.         if( $bool )
  108.                 $this->checkAddress = true;
  109.         else
  110.                 $this->checkAddress = false;
  111. }
  112.  
  113. /*
  114. Define the subject line of the email
  115. @param string $subject any monoline string
  116. */
  117.  
  118. function Subject( $subject ) {
  119.         $this->xheaders['Subject'] = strtr( $subject, "\r\n" , "  " );
  120. }
  121.  
  122. /*
  123. set the sender of the mail
  124. @param string $from should be an email address
  125. */
  126.  
  127. function From( $from ) {
  128.         if( ! is_string($from) ) {
  129.                 echo "Class Mail: error, From is not a string";
  130.                 exit;
  131.         }
  132.         $this->xheaders['From'] = $from;
  133. }
  134.  
  135. /*
  136. set the Reply-to header
  137. @param string $email should be an email address
  138. */
  139.  
  140. function ReplyTo( $address ) {
  141.         if( ! is_string($address) )
  142.                 return false;
  143.         $this->xheaders["Reply-To"] = $address;
  144. }
  145.  
  146. /*
  147. add a receipt to the mail ie.  a confirmation is returned to the "From" address (or "ReplyTo" if defined)
  148. when the receiver opens the message.
  149. @warning this functionality is *not* a standard, thus only some mail clients are compliants.
  150. */
  151.  
  152. function Receipt() {
  153.         $this->receipt = 1;
  154. }
  155.  
  156. /*
  157. set the mail recipient
  158. @param string $to email address, accept both a single address or an array of addresses
  159. */
  160.  
  161. function To( $to ) {
  162.         // TODO : test validitΘ sur to
  163.         if( is_array( $to ) )
  164.                 $this->sendto= $to;
  165.         else
  166.                 $this->sendto[] = $to;
  167.         if( $this->checkAddress == true )
  168.         $this->CheckAdresses( $this->sendto );
  169. }
  170.  
  171. /*                Cc()
  172.  *                set the CC headers ( carbon copy )
  173.  *                $cc : email address(es), accept both array and string
  174.  */
  175.  
  176. function Cc( $cc ) {
  177.         if( is_array($cc) )
  178.                 $this->acc= $cc;
  179.         else
  180.             $this->acc[]= $cc;
  181.         if( $this->checkAddress == true )
  182.             $this->CheckAdresses( $this->acc );
  183. }
  184.  
  185. /*                Bcc()
  186.  *                set the Bcc headers ( blank carbon copy ).
  187.  *                $bcc : email address(es), accept both array and string
  188.  */
  189.  
  190. function Bcc( $bcc ) {
  191.         if( is_array($bcc) ) {
  192.                 $this->abcc = $bcc;
  193.         } else {
  194.                 $this->abcc[]= $bcc;
  195.         }
  196.         if( $this->checkAddress == true )
  197.                 $this->CheckAdresses( $this->abcc );
  198. }
  199.  
  200. /*                Body( text [, charset] )
  201.  *                set the body (message) of the mail
  202.  *                define the charset if the message contains extended characters (accents)
  203.  *                default to us-ascii
  204.  *                $mail->Body( "mΘl en franτais avec des accents", "iso-8859-1" );
  205.  */
  206.  
  207. function Body( $body, $charset="" ) {
  208.         $this->body = $body;
  209.         if( $charset != "" ) {
  210.                 $this->charset = strtolower($charset);
  211.                 if( $this->charset != "us-ascii" )
  212.                         $this->ctencoding = "8bit";
  213.         }
  214. }
  215.  
  216. /*                Organization( $org )
  217.  *                set the Organization header
  218.  */
  219.  
  220. function Organization( $org ) {
  221.         if( trim( $org != "" )  )
  222.                 $this->xheaders['Organization'] = $org;
  223. }
  224.  
  225. /*                Priority( $priority )
  226.  *                set the mail priority
  227.  *                $priority : integer taken between 1 (highest) and 5 ( lowest )
  228.  *                ex: $mail->Priority(1) ; => Highest
  229.  */
  230.  
  231. function Priority( $priority ) {
  232.         if( ! intval( $priority ) )
  233.                 return false;
  234.         if( ! isset( $this->priorities[$priority-1]) )
  235.                 return false;
  236.         $this->xheaders["X-Priority"] = $this->priorities[$priority-1];
  237.     return true;
  238. }
  239.  
  240. /*
  241.  Attach a file to the mail
  242.  @param string $filename : path of the file to attach
  243.  @param string $filetype : MIME-type of the file. default to 'application/x-unknown-content-type'
  244.  @param string $disposition : instruct the Mailclient to display the file if possible ("inline") or always as a link ("attachment") possible values are "inline", "attachment"
  245.  */
  246.  
  247. function Attach( $filename, $filetype = "", $disposition = "inline" ) {
  248.         // TODO : si filetype="", alors chercher dans un tablo de MT connus / extension du fichier
  249.         if( $filetype == "" )
  250.             $filetype = "application/x-unknown-content-type";
  251.         $this->aattach[] = $filename;
  252.         $this->actype[] = $filetype;
  253.         $this->adispo[] = $disposition;
  254. }
  255.  
  256. /*
  257. Build the email message
  258. @access protected
  259. */
  260.  
  261. function BuildMail() {
  262.         // build the headers
  263.     $this->headers = "";
  264. //        $this->xheaders['To'] = implode( ", ", $this->sendto );
  265.         if( count($this->acc) > 0 )
  266.                 $this->xheaders['CC'] = implode( ", ", $this->acc );
  267.         if( count($this->abcc) > 0 )
  268.                 $this->xheaders['BCC'] = implode( ", ", $this->abcc );
  269.         if( $this->receipt ) {
  270.                 if( isset($this->xheaders["Reply-To"] ) )
  271.                         $this->xheaders["Disposition-Notification-To"] = $this->xheaders["Reply-To"];
  272.                 else
  273.                         $this->xheaders["Disposition-Notification-To"] = $this->xheaders['From'];
  274.         }
  275.         if( $this->charset != "" ) {
  276.                 global $contenttype;
  277.                 $this->xheaders["Mime-Version"] = "1.0";
  278.                 $this->xheaders["Content-Type"] = "$contenttype; charset=$this->charset";
  279.                 $this->xheaders["Content-Transfer-Encoding"] = $this->ctencoding;
  280.         }
  281.         $this->xheaders["X-Mailer"] = "RLSP Mailer";
  282.         // include attached files
  283.         if( count( $this->aattach ) > 0 ) {
  284.                 $this->_build_attachement();
  285.         } else {
  286.                 $this->fullBody = $this->body;
  287.         }
  288.         reset($this->xheaders);
  289.         while( list( $hdr,$value ) = each( $this->xheaders )  ) {
  290.                 if( $hdr != "Subject" )
  291.                         $this->headers .= "$hdr: $value\n";
  292.         }
  293. }
  294.  
  295. /*
  296. fornat and send the mail
  297. @access public
  298. */
  299.  
  300. function Send() {
  301.         $this->BuildMail();
  302.         $this->strTo = implode( ", ", $this->sendto );
  303.         // envoie du mail
  304.         $res = @mail( $this->strTo, $this->xheaders['Subject'], $this->fullBody, $this->headers );
  305. }
  306.  
  307. /*
  308.  *                return the whole e-mail , headers + message
  309.  *                can be used for displaying the message in plain text or logging it
  310.  */
  311.  
  312. function Get() {
  313.         $this->BuildMail();
  314.         $mail = "To: " . $this->strTo . "\n";
  315.         $mail .= $this->headers . "\n";
  316.         $mail .= $this->fullBody;
  317.         return $mail;
  318. }
  319.  
  320. /*
  321. check an email address validity
  322. @access public
  323. @param string $address : email address to check
  324. @return true if email adress is ok
  325. */
  326.  
  327. function ValidEmail($address) {
  328.         if( ereg( ".*<(.+)>", $address, $regs ) ) {
  329.                 $address = $regs[1];
  330.         }
  331.      //if(eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,6}$",$address))
  332.          if(ereg( "^[^@  ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address) )
  333.                  return true;
  334.          else
  335.                  return false;
  336. }
  337.  
  338. /*
  339. check validity of email addresses
  340. @param        array $aad -
  341. @return if unvalid, output an error message and exit, this may -should- be customized
  342. */
  343.  
  344. function CheckAdresses( $aad ) {
  345.         for($i=0;$i< count( $aad); $i++ ) {
  346.                 if( ! $this->ValidEmail( $aad[$i]) ) {
  347.                         echo "Class Mail, method Mail : invalid address $aad[$i]";
  348.                         exit;
  349.                 }
  350.         }
  351. }
  352.  
  353. /*
  354. check and encode attach file(s) . internal use only
  355. @access private
  356. */
  357.  
  358. function _build_attachement() {
  359.         $this->xheaders["Content-Type"] = "multipart/mixed;\n boundary=\"$this->boundary\"";
  360.         $this->fullBody = "This is a multi-part message in MIME format.\n--$this->boundary\n";
  361.         $this->fullBody .= "Content-Type: text/plain; charset=$this->charset\nContent-Transfer-Encoding: $this->ctencoding\n\n" . $this->body ."\n";
  362.         $sep= chr(13) . chr(10);
  363.         $ata= array();
  364.         $k=0;
  365.         // for each attached file, do...
  366.         for( $i=0; $i < count( $this->aattach); $i++ ) {
  367.                 $filename = $this->aattach[$i];
  368.                 $basename = basename($filename);
  369.                 $ctype = $this->actype[$i];        // content-type
  370.                 $disposition = $this->adispo[$i];
  371.                 if( ! file_exists( $filename) ) {
  372.                         echo "Class Mail, method attach : file $filename can't be found"; exit;
  373.                 }
  374.                 $subhdr= "--$this->boundary\nContent-type: $ctype;\n name=\"$basename\"\nContent-Transfer-Encoding: base64\nContent-Disposition: $disposition;\n  filename=\"$basename\"\n";
  375.                 $ata[$k++] = $subhdr;
  376.                 // non encoded line length
  377.                 $linesz= filesize( $filename)+1;
  378.                 $fp= fopen( $filename, 'r' );
  379.                 $ata[$k++] = chunk_split(base64_encode(fread( $fp, $linesz)));
  380.                 fclose($fp);
  381.         }
  382.         $this->fullBody .= implode($sep, $ata);
  383. }
  384.  
  385. } // class Mail
  386.  
  387. ?>