<?php

/*
* Email class by Thomas Smit 4/10/06 - 9/10/06
* Provides methods to send emails, set headers, validate addresses and offers a choice between plain-text and HTML (with a templating system)
*/

class email
{

    public 
$recipient;    // Address to send to
    
public $subject;      // Email subject
    
public $body;         // Body of the email
    
private $headers;     // Email headers

    
const HTML true;    // Whether to use HTML or not

    /*
    * __construct() - class constructor
    * @param mixed recipient - address(es) to send to (string = person, array = persons)
    * @param string subject - email subject
    * @param string body - content of the email
    * @param string from - who the email is from
    * @return void
    */

    
public function __construct($recipient$subject$body$from)
    {
        
$this->recipient $recipient;
        
$this->subject $subject;
        
$this->body $body;

        if(
self::HTML === true)
        {
            
$headers = array('From' => $from'Reply-to' => $from'Content-type' => 'text/html');
            
$this->template('template.html');
        }
        else
            
$headers = array('From' => $from'Reply-to' => $from'Content-type' => 'text/plain');

        
$this->setHeaders($headers);
    }

    
/*
    * template() - templating system
    * @param string templatefile - template file to use
    * @return void
    */

    
private function template($templatefile)
    {
        if(!
file_exists($templatefile))
            
trigger_error('Template file does not exist 'E_USER_ERROR);
        else
        {
            
$file file_get_contents($templatefile);
            
$search = array('{[address]}''{[subject]}''{[body]}');
            
$replace = array($this->recipient$this->subject$this->body);
            
$this->body str_replace($search$replace$file);
        }
    }

    
/*
    * setHeaders() - set email headers
    * @param array headers - headers to send, passed as an array
    * @return void
    */

    
public function setHeaders($headers)
    {
        if(!
is_array($headers))
            
trigger_error('Method argument must be passed as an array 'E_USER_WARNING);
        else
            foreach(
$headers as $header => $val)
                
$this->headers .= $header ': ' $val "\r\n";
    }

    
/*
    * validate() - validate email address (argument taken in case $recipient is an array)
    * @return boolean
    */

    
public function validate($address)
    {
        return (
preg_match('/^(.+){1,64}\@(.+){2,255}\.(.+){2,10}$/'$address)) ? true false;
    }

    
/*
    * send() - send the email
    * @return boolean
    */

    
public function send()
    {
        if(
is_array($this->recipient))
        {
            foreach(
$this->recipient as $address)
                
$recipients[] = $address;
            
$recipient implode(', '$recipients);
        }
        else
            
$recipient $this->recipient;

        return (
mail($recipient$this->subject$this->body$this->headers)) ? true false;
    }

}

?>