home *** CD-ROM | disk | FTP | other *** search
- MIME message e-mail
-
- EMail class that supports MIME quoted printable encoding of message bodies and Q encoding of message headers
-
-
-
- <?
- /*
- * email.php3
- *
- * @(#) $Header: /cvsroot/enanet/email.php3,v 1.6 1998/05/18 15:57:17 mlemos Exp $
- *
- * E na Net service
- *
- * This information is CONFIDENTIAL and PROPRIETARY
- * (C) Copyright Manuel Lemos. All Rights Reserved.
- *
- * $Log: email.php3,v $
- * Revision 1.6 1998/05/18 15:57:17 mlemos
- * Added an instance variable to hold the number of added message parts.
- * Added the method AddBodyPreEncodedText.
- * Made the first body part be directly assigned to the body variable in the
- * Send method.
- *
- * Revision 1.5 1998/05/17 23:10:49 mlemos
- * Fixed bug in EmailEncodeQuotedPrintable function of skipping contiguous
- * whitespaces.
- * Made EmailEncodeQuotedPrintable function not break lines when Q-encoding
- * header text.
- * Made all methods return an empty string as success code.
- * Added the method ResetBody to email_message class to clear the body data
- * array.
- *
- * Revision 1.4 1998/05/15 18:36:33 mlemos
- * Made the spaces in the headers be encoded as underscore for better
- * readability.
- * Turned the statement that actually sends the message into the class method
- * SendMail to be easily overridable by any subclass.
- *
- * Revision 1.3 1998/05/14 22:05:49 mlemos
- * Fixed the error messages returned from the send method regarding mandatory
- * missing headers.
- *
- * Revision 1.2 1998/05/11 23:02:25 mlemos
- * Added the function to validate an e-mail address.
- *
- * Revision 1.1 1998/05/11 17:47:50 mlemos
- * Initial revision.
- *
- *
- *
- */
-
- Function EmailValidateAddress($address)
- {
- return(ereg("^[^@ ]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2}|net|com|gov|mil|org|edu|int)\$",$address));
- }
-
- $default_email_charset="iso-8859-1";
-
- Function EmailEncodeQuotedPrintable($text,$header_charset)
- {
- $length=strlen($text);
- for($whitespace="",$line=0,$encode="",$index=0;$index<$length;$index++)
- {
- $character=substr($text,$index,1);
- $order=Ord($character);
- $encode=0;
- switch($order)
- {
- case 9:
- case 32:
- if($header_charset=="")
- {
- $previous_whitespace=$whitespace;
- $whitespace=$character;
- $character="";
- }
- else
- {
- if($order==32)
- $character="_";
- else
- $encode=1;
- }
- break;
- case 10:
- case 13:
- if($whitespace!="")
- {
- if($header_charset==""
- && $line+3>75)
- {
- $encoded.="=\n";
- $line=0;
- }
- $encoded.=sprintf("=%02X",Ord($whitespace));
- $line+=3;
- $whitespace="";
- }
- $encoded.=$character;
- $line=0;
- continue 2;
- default:
- if($order>127
- || $order<32
- || $character=="="
- || ($header_charset!=""
- && ($character=="?"
- || $character=="_"
- || $character=="("
- || $character==")")))
- $encode=1;
- break;
- }
- if($whitespace!="")
- {
- if($header_charset==""
- && $line+1>75)
- {
- $encoded.="=\n";
- $line=0;
- }
- $encoded.=$whitespace;
- $line++;
- $whitespace="";
- }
- if($character!="")
- {
- if($encode)
- {
- $character=sprintf("=%02X",$order);
- $encoded_length=3;
- }
- else
- $encoded_length=1;
- if($header_charset==""
- && $line+$encoded_length>75)
- {
- $encoded.="=\n";
- $line=0;
- }
- $encoded.=$character;
- $line+=$encoded_length;
- }
- }
- if($whitespace!="")
- {
- if($header_charset==""
- && $line+3>75)
- $encoded.="=\n";
- $encoded.=sprintf("=%02X",Ord($whitespace));
- }
- if($header_charset!=""
- && $text!=$encoded)
- return("=?$header_charset?q?$encoded?=");
- else
- return($encoded);
- }
-
- Function EmailEncodeAddress($address,$name,$header_charset)
- {
- global $default_email_charset;
-
- if($header_charset=="")
- $header_charset=$default_email_charset;
- return("$address (".EmailEncodeQuotedPrintable($name,$header_charset).")");
- }
-
- class email_message
- {
- var $mailer="";
- var $default_charset=$default_email_charset;
-
- var $headers=array("To"=>"","Subject"=>"");
- var $body=array();
- var $parts=0;
-
- Function SetHeader($header,$value)
- {
- $this->headers["$header"]="$value";
- return("");
- }
-
- Function AddBodyText($text)
- {
- $this->body[$this->parts]["CONTENTS"]=$text;
- $this->body[$this->parts]["TYPE"]="TEXT";
- $this->parts++;
- return("");
- }
-
- Function AddBodyEncodedText($text)
- {
- $this->body[$this->parts]["CONTENTS"]=EmailEncodeQuotedPrintable($text,"");
- $this->body[$this->parts]["TYPE"]="ENCODED_TEXT";
- $this->body[$this->parts]["CHARSET"]=$this->default_charset;
- $this->parts++;
- return("");
- }
-
- Function AddBodyPreEncodedText($text)
- {
- $this->body[$this->parts]["CONTENTS"]=$text;
- $this->body[$this->parts]["TYPE"]="ENCODED_TEXT";
- $this->body[$this->parts]["CHARSET"]=$this->default_charset;
- $this->parts++;
- return("");
- }
-
- Function Send()
- {
- if(!IsSet($this->headers["To"])
- || $this->headers["To"]=="")
- return("the header To: was not defined");
- if(!IsSet($this->headers["Subject"])
- || $this->headers["Subject"]=="")
- return("the header Subject: was not defined");
- for($charset=$body="",$part=0;$part<$this->parts;$part++)
- {
- if($part>0)
- return("multipart mails are not yet supported");
- switch($this->body[$part]["TYPE"])
- {
- case "TEXT":
- if($body=="")
- $body=$this->body[$part]["CONTENTS"];
- else
- $body.=$this->body[$part]["CONTENTS"];
- break;
- case "ENCODED_TEXT":
- if($body=="")
- $body=$this->body[$part]["CONTENTS"];
- else
- $body.=$this->body[$part]["CONTENTS"];
- $charset=$this->body[$part]["CHARSET"];
- break;
- }
- }
- for($header=0,$headers="",Reset($this->headers);$header<count($this->headers);Next($this->headers),$header++)
- {
- switch(Key($this->headers))
- {
- case "To":
- case "Subject":
- break;
- default:
- $headers.=Key($this->headers).": ".$this->headers[Key($this->headers)]."\n";
- break;
- }
- }
- if($this->mailer!="")
- $headers.="X-Mailer: $this->mailer";
- if($charset!="")
- $headers.="MIME-Version: 1.0\nContent-Type: text/plain; charset=$charset\nContent-Transfer-Encoding: quoted-printable\n";
- return($this->SendMail($this->headers["To"],EmailEncodeQuotedPrintable($this->headers["Subject"],$this->default_charset),&$body,$headers));
- }
-
- Function SendMail($to,$subject,$body,$headers)
- {
- mail($to,$subject,&$body,$headers);
- return("");
- }
-
- Function ResetBody()
- {
- $this->body=array();
- return("");
- }
- };
-
- ?>
-
-