Here is a PHP mail class that handles multiple attachments and several other functions:

* Notes from [EMAIL PROTECTED] 25 Mar 2000:
* This library based  idea of Dan Potter
* Improvements: Multi attachmends in one e-mail, ability to post html & plain trext, 
up to
3x speed improved.
* USAGE - mimetype example for attacment
* $m = new CMIMEMail($to,$from,$subject);
* $m->mailbody("This is simply text","<html><body><h1>This is HTML text</h1>");
* $m->attach("example.html","text/html",$filebody);
* $m->attachFile("resume.gif","image/gif");
* $m->send();
* NOTE: if your system have chunk_split function use it.
* To Do:
* 1. Make quoted-printable encoder and use them in makebody;
* 2. Generate right boundaries
* 3. Fix bugs in  SMTP send direct futction SMTPsend()

function my_chunk_split($str)
        $stmp = base64_encode($str);
        $len = strlen($stmp);
        $out =  "";
    while( $done<$len ) {
      $out.=( $len-$done>76)?substr($strp,$done, 76). "\r\n":substr($strp,$done,
$len-$done). "\r\n";

        return $out;

class CMIMEMail {
 var $to;
 var $from;
 var $cc;
 var $bcc;
 var $subject;
 var $priority;
 var $boundary =  "----=_NextPart_000_0009_01BF95E9.CDFD2060";
 var $smtp_headers;
 var $filename_real;
 var $body_plain;
 var $body_html;
 var $atcmnt;
 var $atcmnt_type;
 function CMIMEMail($to,$from,$subject,$cc="",$bcc="",$priority=3) {
   $this->to=$to; $this->from=$from; $this->subject=$subject;
   $this->priority=$priority; $this->cc=$cc; $this->bcc=$bcc;
 function  mailbody( $plain, $html= "" ) {
 function  attach( $name, $content_type, $data ) {
 function  attachfile( $fname, $content_type ) {
   $name=ereg_replace( "(.+/)", "",$fname);
   $f=fopen($name, "r");
   $fsz = filesize($name);
   $ftext = fread($f,$fsz);
 function  clear() {
   unset( $this->atcmnt );
   unset( $this->atcmnt_type );
 function  makeheader() {
   $out = "From: ".$this->from."\nReply-To: ".$this->from. "\n";
   if( $this->cc !="")  $out .="Cc: ".$this->cc."\n";
   if( $this->bcc !="") $out .="Bcc: ".$this->bcc."\n";
   $out.= "MIME-Version: 1.0\nContent-Type: multipart/mixed;\n
boundary=\"".$this->boundary. "\"\nX-Priority: ".$this->priority. "\n";
   return $out;
 function  makebody() {
   $boundary2=  "----=_NextPart_001_0009_01BF95E9.CDFD2060";
   $out= "";
   $out= "\n\n".$this->body_plain. "\n\n";
   if( $this->body_html!= "" ) {
     $out.= "--".$this->boundary. "\nContent-Type: multipart/alternative;\n
     $out.= "$body_plain\n--$boundary2\nContent-Type:
text/plain\nContent-Transfer-Encoding: quoted-printable\n\n".$this->body_plain.
"\n\n--$boundary2\nContent-Type: text/html\n".
   } else {
#     $out.= "--".$this->boundary. "\nContent-Type: 
quoted-printable\n\n".$this->body_plain. "\n\n--".$this->boundary. "\n";
     $out.= "--".$this->boundary. "\nContent-Type: 
quoted-printable\n\n".$this->body_plain. "\n\n";
   if( ISSET($this->atcmnt_type) )
   reset( $this->atcmnt_type);
   while( list($name, $content_type) = each($this->atcmnt_type) ) {
     $out.= "\n--".$this->boundary. "\nContent-Type:
$content_type\nContent-Transfer-Encoding: base64\nContent-Disposition: attachment;
       chunk_split(base64_encode($this->atcmnt[$name])). "\n";

   } }
   $out.= "--".$this->boundary. "--\n";
   return $out;
 function  send(){
   mail( $this->to, $this->subject, $this->makebody(),$this->makeheader() );
 function  sendto($email,$subj="#"){
   if( $subj=="#") $subj= $this->subject;
   mail( $email, $subj, $this->makebody(),$this->makeheader() );
 function  SMTPsend($host){
   $errno=0;$errstr= "";
//   $f=fsockopen("",25,&$erno, &$errstr);
   if(!$f) {
   } else {
      //SNMP commands Not finished yet
      echo fgets($f,512);
     fputs($f, "HELO\n");
      echo fgets($f,512);
      fputs($f, "MAIL FROM: ".$this->from. "\n");
      echo fgets($f,512);
      fputs($f, "RCPT TO: ".$this->to). "\n";
      echo fgets($f,512);
     fputs($f, "data\n");
      echo fgets($f,512);
     fputs($f, "From: ".$this->from. "\nTo: ".$this->to.
"\n".$this->makeheader().$this->makebody(). "\n\n.\n");
     fputs($f, "quit\nexit");


----- Original Message -----
From: "Oliver Witt" <[EMAIL PROTECTED]>
Sent: Sunday, November 10, 2002 7:31 AM
Subject: [PHP] attachement

I wrote a php script that's supposed to enable me to attach files to an
email. However, the attached files ends up being a mess of letters.
"Hallo" becomes "SGFsbG8=". That must have to do with encoding. Is that
a common problem or do you need the entire script?

PHP General Mailing List (
To unsubscribe, visit:

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to