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

  1. MIME mail 
  2.  
  3. This code allow to attach many attachement with any content/type. 
  4.  
  5.  
  6.  
  7. <?php 
  8. /*
  9. * Notes from rozhik@ziet.zhitomir.ua 25 Mar 2000:
  10. * This library based  idea of Dan Potter
  11. * Improvements: Multi attachmends in one e-mail, ability to post html & plain trext, up to 3x speed improved.
  12. * USSAGE - mimetype example for attacment 
  13. * $m = new CMIMEMail($to,$from,$subject); 
  14. * $m->mailbody("This is simply text","<html><body><h1>This is HTML text</h1>");
  15. * $m->attach("example.html","text/html",$filebody);
  16. * $m->attachFile("resume.gif","image/gif");
  17. * $m->send(); 
  18. * NOTE: if your system have chunk_split function use it.
  19. *******
  20. * To Do:
  21. * 1. Make quoted-printable encoder and use them in makebody;
  22. * 2. Generate right boundaries
  23. * 3. Fix bugs in  SMTP send direct futction SMTPsend()
  24. */ 
  25.  
  26. function my_chunk_split($str) 
  27.         $stmp = base64_encode($str); 
  28.         $len = strlen($stmp); 
  29.         $out =  ""; 
  30.     $done=0;
  31.     while( $done<$len ) {
  32.       $out.=( $len-$done>76)?substr($strp,$done, 76). "\r\n":substr($strp,$done, $len-$done). "\r\n";
  33.       $done+=76;
  34.       
  35.     }
  36.         return $out; 
  37.  
  38.  
  39. class CMIMEMail { 
  40.  var $to; 
  41.  var $boundary =  "----=_NextPart_000_0009_01BF95E9.CDFD2060"; 
  42.  var $smtp_headers; 
  43.  var $filename_real; 
  44.  var $body_plain; 
  45.  var $body_html; 
  46.  var  $atcmnt;
  47.  var $atcmnt_type;
  48.  function CMIMEMail($to,$from,$subject,$priority=3) { 
  49.    $this->to=$to; $this->from=$from; $this->subject=$subject; $this->priority=$priority; 
  50.  }
  51.  function  mailbody( $plain, $html= "" ) { 
  52.    $this->body_plain=$plain; 
  53.    $this->body_html=$html; 
  54.  } 
  55.  function  attach( $name, $content_type, $data ) { 
  56.    $this->atcmnt[$name]=$data; 
  57.    $this->atcmnt_type[$name]=$content_type; 
  58.  } 
  59.  function  attachfile( $fname, $content_type ) { 
  60.    $name=ereg_replace( "(.+/)", "",$fname);
  61.    $f=fopen($name, "r");
  62.    attach($name,$content_type,$fread(filesize($f)));
  63.    flose($f);
  64.  } 
  65.  function  clear() { 
  66.    unset( $atcmnt ); 
  67.    unset( $atcmnt_type ); 
  68.  } 
  69.  function  makeheader() {
  70.    $out = "Reply-To: ".$this->from. "\n";
  71.    $out.= "MIME-Version: 1.0\nContent-Type: multipart/mixed;\n boundary=\"".$this->boundary. "\"\nX-Priority: ".$this->priority. "\n";
  72.    return $out;
  73.  }
  74.  function  makebody() {
  75.    $boundary2=  "----=_NextPart_001_0009_01BF95E9.CDFD2060"; 
  76.    $out= "";
  77.    $out= "\n\n".$this->body_plain. "\n\n";
  78.    if( $this->body_html!= "" ) {
  79.      $out.= "--".$this->boundary. "\nContent-Type: multipart/alternative;\n boundary=$boundary2\n\n";
  80.      $out.= "$body_plan\n--$boundary2\nContent-Type: text/plain\nContent-Transfer-Encoding: quoted-printable\n\n".$this->body_plain. "\n\n--$boundary2\nContent-Type: text/html\n".
  81.             "Conent-Transfer-Encoding: quoted-printable\n\n$this->body_html\n\n--$boundary2--\n";
  82.    } else {
  83.      $out.= "--".$this->boundary. "\nContent-Type: text/plain\nContent-Transfer-Encoding: quoted-printable\n\n".$this->body_plain. "\n\n--".$this->boundary. "\n";
  84.    }
  85.    reset( $this->atcmnt_type);
  86.    while( list($name, $content_type) = each($this->atcmnt_type) ) {
  87.      $out.= "\n--".$this->boundary. "\nContent-Type: $content_type\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment; filename=\"$name\"\n\n".
  88.        chunk_split(base64_encode($this->atcmnt[$name])). "\n";
  89.      
  90.    }
  91.    $out.= "--".$this->boundary. "--\n";
  92.    return $out;
  93.  } 
  94.  function  send(){
  95.    mail( $this->to, $this->subject, $this->makebody(),$this->makeheader() );
  96.  }
  97.  function  sendto($email){
  98.    mail( $email, $this->subject, $this->makebody(),$this->makeheader() );
  99.  } 
  100.  function  SMTPsend($host){
  101.    $errno=0;$errstr= "";
  102. //   $f=fsockopen("127.0.0.1",25,&$erno, &$errstr); 
  103.    if(!$f) {
  104.      $this->send();
  105.    } else {
  106.       //SNMP commands Not finished yet 
  107.       echo fgets($f,512);     
  108.      fputs($f, "HELO host.com\n");
  109.       echo fgets($f,512); 
  110.       fputs($f, "MAIL FROM: ".$this->from. "\n");
  111.       echo fgets($f,512); 
  112.       fputs($f, "RCPT TO: ".$this->to). "\n";
  113.       echo fgets($f,512); 
  114.      fputs($f, "data\n");
  115.       echo fgets($f,512); 
  116.      fputs($f, "From: ".$this->from. "\nTo: ".$this->to. "\n".$this->makeheader().$this->makebody(). "\n\n.\n");
  117.      fputs($f, "quit\nexit");
  118.      
  119.      fclose($f);
  120.   }
  121.  }
  122. ?> 
  123.