home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / mimemessageemail.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  6.5 KB  |  274 lines

  1. MIME message e-mail 
  2.  
  3. EMail class that supports MIME quoted printable encoding of message bodies and Q encoding of message headers
  4.  
  5.  
  6.  
  7. <?
  8. /*
  9.  * email.php3
  10.  *
  11.  * @(#) $Header: /cvsroot/enanet/email.php3,v 1.6 1998/05/18 15:57:17 mlemos Exp $
  12.  *
  13.  * E na Net service
  14.  *
  15.  * This information is CONFIDENTIAL and PROPRIETARY
  16.  * (C) Copyright Manuel Lemos. All Rights Reserved.
  17.  *
  18.  * $Log: email.php3,v $
  19.  * Revision 1.6  1998/05/18 15:57:17  mlemos
  20.  * Added an instance variable to hold the number of added message parts.
  21.  * Added the method AddBodyPreEncodedText.
  22.  * Made the first body part be directly assigned to the body variable in the
  23.  * Send method.
  24.  *
  25.  * Revision 1.5  1998/05/17 23:10:49  mlemos
  26.  * Fixed bug in EmailEncodeQuotedPrintable function of skipping contiguous
  27.  * whitespaces.
  28.  * Made EmailEncodeQuotedPrintable function not break lines when Q-encoding
  29.  * header text.
  30.  * Made all methods return an empty string as success code.
  31.  * Added the method ResetBody to email_message class to clear the body data
  32.  * array.
  33.  *
  34.  * Revision 1.4  1998/05/15 18:36:33  mlemos
  35.  * Made the spaces in the headers be encoded as underscore for better
  36.  * readability.
  37.  * Turned the statement that actually sends the message into the class method
  38.  * SendMail to be easily overridable by any subclass.
  39.  *
  40.  * Revision 1.3  1998/05/14 22:05:49  mlemos
  41.  * Fixed the error messages returned from the send method regarding mandatory
  42.  * missing headers.
  43.  *
  44.  * Revision 1.2  1998/05/11 23:02:25  mlemos
  45.  * Added the function to validate an e-mail address.
  46.  *
  47.  * Revision 1.1  1998/05/11 17:47:50  mlemos
  48.  * Initial revision.
  49.  *
  50.  *
  51.  *
  52.  */
  53.  
  54. Function EmailValidateAddress($address)
  55. {
  56.  return(ereg("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address));
  57. }
  58.  
  59. $default_email_charset="iso-8859-1";
  60.  
  61. Function EmailEncodeQuotedPrintable($text,$header_charset)
  62. {
  63.  $length=strlen($text);
  64.  for($whitespace="",$line=0,$encode="",$index=0;$index<$length;$index++)
  65.  {
  66.   $character=substr($text,$index,1);
  67.   $order=Ord($character);
  68.   $encode=0;
  69.   switch($order)
  70.   {
  71.    case 9:
  72.    case 32:
  73.     if($header_charset=="")
  74.     {
  75.      $previous_whitespace=$whitespace;
  76.      $whitespace=$character;
  77.      $character="";
  78.     }
  79.     else
  80.     {
  81.      if($order==32)
  82.       $character="_";
  83.      else
  84.       $encode=1;
  85.     }
  86.     break;
  87.    case 10:
  88.    case 13:
  89.     if($whitespace!="")
  90.     {
  91.      if($header_charset==""
  92.      && $line+3>75)
  93.      {
  94.       $encoded.="=\n";
  95.       $line=0;
  96.      }
  97.      $encoded.=sprintf("=%02X",Ord($whitespace));
  98.      $line+=3;
  99.      $whitespace="";
  100.     }
  101.     $encoded.=$character;
  102.     $line=0;
  103.     continue 2;
  104.    default:
  105.     if($order>127
  106.     || $order<32
  107.     || $character=="="
  108.     || ($header_charset!=""
  109.     && ($character=="?"
  110.     || $character=="_"
  111.     || $character=="("
  112.     || $character==")")))
  113.      $encode=1;
  114.     break;
  115.   }
  116.   if($whitespace!="")
  117.   {
  118.    if($header_charset==""
  119.    && $line+1>75)
  120.    {
  121.     $encoded.="=\n";
  122.     $line=0;
  123.    }
  124.    $encoded.=$whitespace;
  125.    $line++;
  126.    $whitespace="";
  127.   }
  128.   if($character!="")
  129.   {
  130.    if($encode)
  131.    {
  132.     $character=sprintf("=%02X",$order);
  133.     $encoded_length=3;
  134.    }
  135.    else
  136.     $encoded_length=1;
  137.    if($header_charset==""
  138.    && $line+$encoded_length>75)
  139.    {
  140.     $encoded.="=\n";
  141.     $line=0;
  142.    }
  143.    $encoded.=$character;
  144.    $line+=$encoded_length;
  145.   }
  146.  }
  147.  if($whitespace!="")
  148.  {
  149.   if($header_charset==""
  150.   && $line+3>75)
  151.    $encoded.="=\n";
  152.   $encoded.=sprintf("=%02X",Ord($whitespace));
  153.  }
  154.  if($header_charset!=""
  155.  && $text!=$encoded)
  156.   return("=?$header_charset?q?$encoded?=");
  157.  else
  158.   return($encoded);
  159. }
  160.  
  161. Function EmailEncodeAddress($address,$name,$header_charset)
  162. {
  163.  global $default_email_charset;
  164.  
  165.  if($header_charset=="")
  166.   $header_charset=$default_email_charset;
  167.  return("$address (".EmailEncodeQuotedPrintable($name,$header_charset).")");
  168. }
  169.  
  170. class email_message
  171. {
  172.  var $mailer="";
  173.  var $default_charset=$default_email_charset;
  174.  
  175.  var $headers=array("To"=>"","Subject"=>"");
  176.  var $body=array();
  177.  var $parts=0;
  178.  
  179.  Function SetHeader($header,$value)
  180.  {
  181.   $this->headers["$header"]="$value";
  182.   return("");
  183.  }
  184.  
  185.  Function AddBodyText($text)
  186.  {
  187.   $this->body[$this->parts]["CONTENTS"]=$text;
  188.   $this->body[$this->parts]["TYPE"]="TEXT";
  189.   $this->parts++;
  190.   return("");
  191.  }
  192.  
  193.  Function AddBodyEncodedText($text)
  194.  {
  195.   $this->body[$this->parts]["CONTENTS"]=EmailEncodeQuotedPrintable($text,"");
  196.   $this->body[$this->parts]["TYPE"]="ENCODED_TEXT";
  197.   $this->body[$this->parts]["CHARSET"]=$this->default_charset;
  198.   $this->parts++;
  199.   return("");
  200.  }
  201.  
  202.  Function AddBodyPreEncodedText($text)
  203.  {
  204.   $this->body[$this->parts]["CONTENTS"]=$text;
  205.   $this->body[$this->parts]["TYPE"]="ENCODED_TEXT";
  206.   $this->body[$this->parts]["CHARSET"]=$this->default_charset;
  207.   $this->parts++;
  208.   return("");
  209.  }
  210.  
  211.  Function Send()
  212.  {
  213.   if(!IsSet($this->headers["To"])
  214.   || $this->headers["To"]=="")
  215.    return("the header To: was not defined");
  216.   if(!IsSet($this->headers["Subject"])
  217.   || $this->headers["Subject"]=="")
  218.    return("the header Subject: was not defined");
  219.   for($charset=$body="",$part=0;$part<$this->parts;$part++)
  220.   {
  221.    if($part>0)
  222.     return("multipart mails are not yet supported");
  223.    switch($this->body[$part]["TYPE"])
  224.    {
  225.     case "TEXT":
  226.      if($body=="")
  227.       $body=$this->body[$part]["CONTENTS"];
  228.      else
  229.       $body.=$this->body[$part]["CONTENTS"];
  230.      break;
  231.     case "ENCODED_TEXT":
  232.      if($body=="")
  233.       $body=$this->body[$part]["CONTENTS"];
  234.      else
  235.       $body.=$this->body[$part]["CONTENTS"];
  236.      $charset=$this->body[$part]["CHARSET"];
  237.      break;
  238.    }
  239.   }
  240.   for($header=0,$headers="",Reset($this->headers);$header<count($this->headers);Next($this->headers),$header++)
  241.   {
  242.    switch(Key($this->headers))
  243.    {
  244.     case "To":
  245.     case "Subject":
  246.      break;
  247.     default:
  248.      $headers.=Key($this->headers).": ".$this->headers[Key($this->headers)]."\n";
  249.      break;
  250.    }
  251.   }
  252.   if($this->mailer!="")
  253.    $headers.="X-Mailer: $this->mailer";
  254.   if($charset!="")
  255.    $headers.="MIME-Version: 1.0\nContent-Type: text/plain; charset=$charset\nContent-Transfer-Encoding: quoted-printable\n";
  256.   return($this->SendMail($this->headers["To"],EmailEncodeQuotedPrintable($this->headers["Subject"],$this->default_charset),&$body,$headers));
  257.  }
  258.  
  259.  Function SendMail($to,$subject,$body,$headers)
  260.  {
  261.   mail($to,$subject,&$body,$headers);
  262.   return("");
  263.  }
  264.  
  265.  Function ResetBody()
  266.  {
  267.   $this->body=array();
  268.   return("");
  269.  }
  270. };
  271.  
  272. ?>
  273.  
  274.