Email Php

bodies and Q encoding of message headers.
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);$headerheaders);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( "");
}
};
?>