#native_company# #native_desc#
#native_cta#

Email class

By Ewout de Boer
on August 17, 2003

Version: 2.1

Type: Class

Category: Other

License: GNU General Public License

Description: This class allow sending of text, html, or html/text messages with or without attachments.

<?php

/*

Amended 17/08/2003 to attach data from php as a file

	Use the 1st argument of '$email->attach()' for the data
	and the 2nd argument for the filename

	code is backwards compatible with older versions.

Amended 20/02/2002 to include embeded images in html.

email_html_wa & email_multi_wa are now capable of using embeded images.

Include file holds 6 classes each extensions of email_txt

email_txt		- Text email no attachments.
email_wa		- Text email with attachments.
email_html		- Html email no attachments.
email_html_wa		- Html email with attachments.
email_multi		- Multi part email html and text.
email_multi_wa		- Multi part email html and text with attachments.

All have the same prototype
(
    from,		- Sender of email
    subject,		- Subject of email
    return address,	- Address for failed emails to return to.
    reply address,	- Address for any replies to go to.
)

All classes have these functions

sendmail(recipient)
  			- Sends the email to the supplied email address
			- the email address is checked for domain existance.
setheaders()
			- Creates the headers ready for transmission.
clean()			
			- Removes any message and headers already set.
			- Useful for sending different emails.
addmessage(message)
			- Add the message to the email.

classes email_multi and email_multi_wa use this version of addmessage instead
of the above.
addmessage(text, html)
			- text is the text part of the email and html holds
			- the html alternative.

Classes email_wa, email_html_wa and email_multi_wa also have

attach(filename)	
			- This will add an attachment to the email.
			- filename is the full pathname to the file.

embed_image(filename, imageid)		
			- Embeds an image in the email usefull for 
 			  sending html emails with images that can be
			  viewed offline.

examples of use:

text email

$email=new email_txt("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("Hello");
$email->sendmail("[email protected]");

text email with attachment

$email=new email_wa("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("Hello");
$email->attach("/path/to/file/filename");
$email->sendmail("[email protected]");

HTML email

$email=new email_html("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("<B>Hello<B>");
$email->sendmail("[email protected]");

HTML email with attachment

$email=new email_html("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("<B>Hello<B>");
$email->attach("/path/to/file/filename");
$email->sendmail("[email protected]");

Multipart email

$email=new email_multi("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("hello", "<B>Hello<B>");
$email->sendmail("[email protected]");

Multipart email with attachment

$email=new email_multi_wa("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("hello", "<B>Hello<B>");
$email->attach("/path/to/file/filename");
$email->sendmail("[email protected]");

HTML email with embeded image

$email=new email_html_wa("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("<IMG src="cid:pic1.1a"");
$email->embed_image("/path/to/file/filename", "pic1.1a");
$email->sendmail("[email protected]");

Multipart email with embeded image

$email=new email_multi_wa("[email protected]", "Test Email");
$email->clean();
$email->setheaders();
$email->addmessage("<IMG src="cid:pic1.1a"");
$email->embed_image("/path/to/file/filename", "pic1.1a");
$email->sendmail("[email protected]");

*/

class email_txt
{
    var $subject;			// email subject
    var $bound;				// email boundary
    var $from;				// from address
    var $headers;			// message headers
    var $message;			// email message
    var $retadd;			// return address
    var $replyto;			// reply address

    function email_txt($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    { 
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->message="";
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function sendmail($rcpt)
    {
	list($name, $domain)=explode("@", $rcpt);
	$retval=checkdnsrr($domain, ANY);
	if ( (!$retval) || (empty($name)) || (empty($domain)) )
	{
	    echo "Invalid email address ".$rcpt.".<BR>";
	    return(0);
	}
	$retval=mail($rcpt, $this->subject, "", $this->headers.$this->message);
	return($retval);
    }

    function setheaders()
    {
	unset($headers);
	$headers="Errors: <".$this->retadd.">n";
	$headers.="From: <".$this->from.">n";
	$headers.="MIME-Version: 1.0n";
	$headers.="Return-Path: <".$this->retadd.">n";
	$headers.="Reply-To: <".$this->replyto.">n";
	$headers.="Content-Transfer-Encoding: 7bitn";
	$headers.="Content-Type:text/plain; charset=us-asciinn";
	
        unset($this->headers);
	$this->headers=$headers;
    }

    function clean()
    {
        $this->message="";
        $this->headers="";
    }

    function addmessage($messin)
    {
	if ( !empty($messin) )
	{
	    $this->message.=$messin."n";
	}
    }
    
}

class email_html extends email_txt
{
    function email_html($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    { 
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->message="";
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function setheaders()
    {
	unset($headers);
	$headers.="Errors: <".$this->retadd.">n";
	$headers.="From: <".$this->from.">n";
	$headers.="X-Mailer: PHPn";
	$headers.="X-Sender: <".$this->from.">n";
	$headers.="X-Priority: 1n";
	$headers.="Return-Path: <".$this->retadd.">n";
	$headers.="Reply-To: <".$this->replyto.">n";
	$headers.="Content-Type:text/html; charset=iso-8859-1nn";
	
	$this->message=$headers;
    }

}

class email_wa extends email_txt
{
    function email_wa($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    { 
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->message="";
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function sendmail($rcpt)
    {
	list($name, $domain)=explode("@", $rcpt);
	$retval=checkdnsrr($domain, ANY);
	if ( (!$retval) || (empty($name)) || (empty($domain)) )
	{
	    echo "Invalid email address ".$rcpt.".<BR>";
            return(0);
	}
	$this->message.="nn--".$this->bound."--n";
	$retval=mail($rcpt, $this->subject, "", $this->message);
        return($retval);
    }

    function setheaders()
    {
	unset($headers);
	$headers.="Errors: <".$this->retadd.">n";
	$headers.="From: <".$this->from.">n";
	$headers.="MIME-Version: 1.0n";
	$headers.="Return-Path: <".$this->retadd.">n";
	$headers.="Reply-To: <".$this->replyto.">n";
	$headers.="Content-Type: multipart/mixed;boundary="".$this->bound.""n";
	$headers.="Content-Transfer-Encoding: 7bitn";
	$headers.="nn--".$this->bound."n";
	$headers.="Content-Type:text/plain; charset=us-asciinn";
	
	$this->message=$headers;
    }

    function attach($attch, $filename)
    {

	if ($filename)
	{
		$thisfile = $filename;
	}
	else
	{
		$bits=explode("/", $attch);
		$thisfile=$bits[(count($bits)-1)];
	}
	
	unset($line);
	$line.="nn--".$this->bound."n";
	$line.="Content-Type: application/octet-stream; name="$thisfile"n";
	$line.="Content-Transfer-Encoding: base64nnn";

	if ($filename)
	{
		$line.= chunk_split(base64_encode($attch));
	}
	else
	{
		$filename=$attch;
		$linein=`uuencode -m $filename fred`;
	
		$lines=explode("n", $linein);
		for ( $loop=1; $loop<count($lines); $loop++ )
		{
	    	$line.=$lines[$loop]."nr";
		}

	}
	
	$line.="nn";

	$this->message.=$line;
    }

}

class email_html_wa extends email_wa
{
    function email_html_wa($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    { 
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->message="";
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function setheaders()
    {
	unset($headers);
	$headers.="Errors: <".$this->retadd.">n";
	$headers.="From: <".$this->from.">n";
	$headers.="MIME-Version: 1.0n";
	$headers.="Return-Path: <".$this->retadd.">n";
	$headers.="Reply-To: <".$this->replyto.">n";
	$headers.="Content-Type: multipart/mixed;boundary="".$this->bound.""n";
	$headers.="Content-Transfer-Encoding: 7bitn";
	$headers.="nn--".$this->bound."n";
	$headers.="Content-Type:text/html; charset=iso-8859-1nn";
	
	$this->message=$headers;
    }

    function addmessage($messin)
    {
	if ( !empty($messin) )
	{
	    $this->message.=$messin."n";
	}
    }
    
    function embed_image($img_path, $img_name)
    {
	unset($line);
	unset($bits);
	$bits=explode("/", $img_path);
	$filename=$bits[(count($bits)-1)];
	unset($bits);
	$bits=explode(".", $img_path);
	$line.="nn--".$this->bound."n";
	$line.="Content-Type: image/".$bits[1]."n";
	$line.="Content-ID: ".$img_name."n";
	$line.="Content-Disposition: inline; filename="".$filename.""n";
	$line.="Content-Transfer-Encoding: base64nnn";

	$linein=`uuencode -m $img_path fred`;

	$lines=explode("n", $linein);
	for ( $loop=1; $loop<count($lines); $loop++ )
	{
	    $line.=$lines[$loop]."nr";
	}

	$line.="nn";

	$this->message.=$line;
    }

}

class email_multi extends email_txt
{

    function email_multi($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    {
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function addmessage($text, $html)
    {
	unset($message);
	// text 
	$message .= "--$this->boundn"; 
	$message .= "Content-Type: text/plain; ntcharset="iso-8859-1"rn"; 
	$message .= "Content-Transfer-Encoding: 7bitrnrn"; 
	$message .= $text . "n"; 

	// html 
	$message .= "--$this->boundrn"; 
	$message .= "Content-Type: text/html; ntcharset="iso-8859-1"rn"; 
	$message .= "Content-Transfer-Encoding: 7bitrnrn"; 
	$message .= $html; 
	
	$message .= "rn--$this->bound--"; 

	$this->message=$message;
    }

    function setheaders()
    {
	unset($headers);
	$headers="From: $from <$this->from>rn"; 
	$headers.="X-Sender: <$this->from>rn"; 
	$headers.="X-Mailer: TLGExtranetrn";
	$headers.="Return-Path: <$this->from>rn";
	$headers.="Mime-Version: 1.0n"; 
	$headers.="Content-Type: multipart/alternative; boundary="$this->bound"rn"; 
	$headers.="X-Priority: 1rn"; 
	$this->headers=$headers;
    }
}

class email_multi_wa extends email_txt
{

    function email_multi($from="[email protected]", $subj="Email for you", $retadd="[email protected]", $replyto="[email protected]")
    {
	$bound="----=_NextPart_000_" . uniqid(rand()); 
	$this->bound=$bound;
	$this->subject=$subj;
	$this->retadd=$retadd;
	$this->replyto=$replyto;
	$this->from=$from;
    }

    function addmessage($text, $html)
    {
	unset($message);
	// text 
	$message .= "--$this->boundn"; 
	$message .= "Content-Type: text/plain; ntcharset="iso-8859-1"rn"; 
	$message .= "Content-Transfer-Encoding: 7bitrnrn"; 
	$message .= $text . "n"; 

	// html 
	$message .= "--$this->boundrn"; 
	$message .= "Content-Type: text/html; ntcharset="iso-8859-1"rn"; 
	$message .= "Content-Transfer-Encoding: 7bitrnrn"; 
	$message .= $html; 
	
	$this->message=$message;
    }

    function setheaders()
    {
	unset($headers);
	$headers="From: $from <$this->from>rn"; 
	$headers.="X-Sender: <$this->from>rn"; 
	$headers.="X-Mailer: TLGExtranetrn";
	$headers.="Return-Path: <$this->from>rn";
	$headers.="Mime-Version: 1.0n"; 
	$headers.="Content-Type: multipart/alternative; boundary="$this->bound"rn"; 
	$headers.="X-Priority: 1rn"; 
	$this->headers=$headers;
    }

    function sendmail($rcpt)
    {
	list($name, $domain)=explode("@", $rcpt);
	$retval=checkdnsrr($domain, ANY);
	if ( (!$retval) || (empty($name)) || (empty($domain)) )
	{
	    echo "Invalid email address ".$rcpt.".<BR>";
	    return(0);
	}
	$this->message.="rn--$this->bound--"; 

	$retval=mail($rcpt, $this->subject, "", $this->headers.$this->message);
	return($retval);
    }

    function attach($attch)
    {
	$bits=explode("/", $attch);
	$thisfile=$bits[(count($bits)-1)];
	unset($line);
	$line.="nn--".$this->bound."rn";
	$line.="Content-Type: application/octet-stream; name="$thisfile"rn";
	$line.="Content-Transfer-Encoding: base64rnrn";

	$filename=$attch;

	$linein=`uuencode -m $filename fred`;

	$lines=explode("n", $linein);
	for ( $loop=1; $loop<count($lines); $loop++ )
	{
	    $line.=$lines[$loop]."nr";
	}

	$line.="nn";

	$this->message.=$line;
    }
    
    function embed_image($img_path, $img_name)
    {
	unset($line);
	unset($bits);
	$bits=explode("/", $img_path);
	$filename=$bits[(count($bits)-1)];
	unset($bits);
	$bits=explode(".", $img_path);
	$line.="nn--".$this->bound."n";
	$line.="Content-Type: image/".$bits[1]."n";
	$line.="Content-ID: ".$img_name."n";
	$line.="Content-Disposition: inline; filename="".$filename.""n";
	$line.="Content-Transfer-Encoding: base64nnn";

	$linein=`uuencode -m $img_path fred`;

	$lines=explode("n", $linein);
	for ( $loop=1; $loop<count($lines); $loop++ )
	{
	    $line.=$lines[$loop]."nr";
	}

	$line.="nn";

	$this->message.=$line;
    }

    
}

?>