Location: PHPKode > scripts > htmlMimeMailEx > htmlmimemailex/htmlMimeMailEx.php
<?php 
/**
* This file contains the googledancer class that checks if Google is "dancing".
*
* @package htmlMimeMailEx
*/

require_once (dirname (__FILE__) . '/htmlMimeMail.php');

/**
* htmlMimeMailEx class htmlMimeMail class provides some small enhanchement in the API that I find usefull.
*
* This extende version of the 
*
* @package htmlMimeMailEx
* @author Setec Astronomy
* @version 1.0
* @abstract An extended version of the htmlMimeMail class.
* @link http://phpguru.org/static/mime.mail.html Original htmlMimeMail class
* @link http://setecastronomy.stufftoread.com
* @copyright 2004 - Setec Astronomy
*/
class htmlMimeMailEx extends htmlMimeMail {

	/**
	* @access private
	*/
	var $_ex_priorities = array ();
	/**
	* @access private
	*/
	var $_ex_to = array ();
	/**
	* @access private
	*/
	var $_ex_cc = array ();
	/**
	* @access private
	*/
	var $_ex_bcc = array ();

	/**
	* Default constructor
	*/
	function htmlMimeMailEx () {
		parent::htmlMimeMail ();
		$this->_ex_priorities = array (1 => '1 (Highest)', 
									2 => '2 (High)', 
									3 => '3 (Normal)', 
									4 => '4 (Low)', 
									5 => '5 (Lowest)');
		$this->_ex_to = array ();
		$this->_ex_cc = array ();
		$this->_ex_bcc = array ();
	}

	/**
	* @access private
	*/
	function _addReceipList ($address, &$list)	{
		if (is_array ($address)) { // B, C, D
			foreach ($address as $key => $value) {
				if (is_numeric ($key)) { // B, D
					if (is_string ($value)) { // B
						$list[] = $value; 
					} elseif (isset ($value['name']) && isset ($value['email'])) { // D
						$value['name'] = str_replace ('"', "'", $value['name']);
						$list[] = '"' . $value['name'] . '" <' . $value['email'] . '>'; 
					}
				} elseif (is_string ($key) && is_string ($value))	{  // C
					$value = str_replace ('"', "'", $value);
					$list[] = "\"$value\" <$key>"; 
				}
			}
			return true;
		} elseif (is_string ($address)) { // A
			$list[] = $address; 
			return true;
		}
		return false;
	}

	/**
	* setTo method
	*
	* Set the recipients addresses, as the other methods ({@link htmlMimeMailEx::setCc() htmlMimeMailEx::setCc()}, 
	* {@link htmlMimeMailEx::setBcc() htmlMimeMailEx::setBcc()}, {@link htmlMimeMailEx::addTo() htmlMimeMailEx::addTo()},
	* {@link htmlMimeMailEx::addCc() htmlMimeMailEx::addCc()}, {@link htmlMimeMailEx::addBcc() htmlMimeMailEx::addBcc()}) 
	* it can accept a mixed $address param, acceptable structures are: <br />
	* A. string $address = 'hide@address.com'; <br />
	* B. array $address = array ('hide@address.com', 'hide@address.com'); <br />
	* C. array $address = array ('hide@address.com' => 'Joe Smith', 'hide@address.com' => 'Mike Black'); <br />
	* D. array $address = array (array ('name' => 'Joe Smith', 'email' => 'hide@address.com'), <br />
	*				             array ('name' => 'Mike Black', 'email' => 'hide@address.com')); <br />
	* @param mixed $address see below.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setTo ($address) {
		$this->_ex_to = array ();
		return $this->addTo ($address);
	}
	
	/**
	* setCc method
	*
	* @param mixed $address see {@link htmlMimeMailEx::setTo() htmlMimeMailEx::setTo()}.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	* @see htmlMimeMailEx::setTo()
	*/	
	function setCc ($address) {
		$this->_ex_cc = array ();
		return $this->addCc ($address);
	}

	/**
	* setBcc method
	*
	* @param mixed $address see {@link htmlMimeMailEx::setTo() htmlMimeMailEx::setTo()}.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	* @see htmlMimeMailEx::setTo()
	*/	
	function setBcc ($address) {
		$this->_ex_bcc = array ();
		return $this->addBcc ($address);
	}	

	/**
	* addTo method
	*
	* @param mixed $address see {@link htmlMimeMailEx::setTo() htmlMimeMailEx::setTo()}.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	* @see htmlMimeMailEx::setTo()
	*/	
	function addTo ($address) {
		return $this->_addReceipList ($address, $this->_ex_to);
	}

	/**
	* addCc method
	*
	* @param mixed $address see {@link htmlMimeMailEx::setTo() htmlMimeMailEx::setTo()}.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	* @see htmlMimeMailEx::setTo()
	*/	
	function addCc ($address) {
		return $this->_addReceipList ($address, $this->_ex_cc);
	}

	/**
	* addBcc method
	*
	* @param mixed $address see {@link htmlMimeMailEx::setTo() htmlMimeMailEx::setTo()}.
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	* @see htmlMimeMailEx::setTo()
	*/	
	function addBcc ($address) {
		return $this->_addReceipList ($address, $this->_ex_bcc);
	}
	
	/**
	* @access private
	*/
	function _setEmailHeaderField ($field_name, $email, $name = '') {
		if (!is_string ($email) || empty ($email)) { 
			return false; 
		} elseif (empty ($name)) { 
			$this->headers[$field_name] = $email; 
		} else { 
			$name = str_replace ('"', "'", $name);
			$this->headers[$field_name] = "\"$name\" <$email>"; 
		}
		return true;
	}

	/**
	* setFrom method
	*
	* Sets the From message header.
	* @param string $email the sender email
	* @param string $name the sender name
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setFrom ($email, $name = '') {
		return _setEmailHeaderField ('From', $email, $name);
	}

	/**
	* setReturnReceipt method
	*
	* Sets the Return-Receipt-To message header.
	* @param string $email the email to notificate to
	* @param string $name the relative name
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setReturnReceipt ($email, $name = '') {
		return _setEmailHeaderField ('Return-Receipt-To', $email, $name);
	}

	/**
	* setDispositionNotification method
	*
	* Sets the Disposition-Notification-To message header.
	* @param string $email the email to notificate to
	* @param string $name the relative name
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setDispositionNotification ($email, $name = '') {
		return _setEmailHeaderField ('Disposition-Notification-To', $email, $name);
	}

	/**
	* setReplyTo method
	*
	* Sets the Reply-To message header.
	* @param string $email the email to Reply-To
	* @param string $name the name to Reply-To
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setReplyTo ($email, $name = '') {
		return _setEmailHeaderField ('Reply-To', $email, $name);
	}

	/**
	* setOrganization method
	*
	* Sets the Organization message header.
	* @param string $organization the organization name
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setOrganization ($organization = "")
	{
		if (!empty ($organization)) { 
			$this->headers['Organization'] = $organization; 
			return true;
		}
		return false;
	}

	/**
	* setAntiSpaming method
	*
	* This method is very usefull when using this class in form mail, it sets some additional
	* headers (X-HTTP-Posting-Host, X-HTTP-Proxy-Server and X-HTTP-Posting-UserAgent) that can be used for
	* anti spamming and tracking purpose. All the parameters are optional, if you omit them the class will analize 
	* the current user HTTP request to get the needed information.
	* @param string $client_ip the client IP address (optional)
	* @param string $proxy_server the user proxy server IP address (optional)
	* @param string $user_agent the user user-agent (optional)
	* @return boolean always returns true.
	*/
	function setAntiSpaming ($client_ip = '', $proxy_server = '', $user_agent = '') {
		if (empty ($client_ip)) { 
			if (isset ($_SERVER['HTTP_X_FORWARDED_FOR']) && (trim ($_SERVER['HTTP_X_FORWARDED_FOR']) != '')) { 
				$client_ip = $_SERVER['HTTP_X_FORWARDED_FOR']; 
			} elseif (isset ($_SERVER['HTTP_CLIENT_IP']) && (trim ($_SERVER['HTTP_CLIENT_IP']) != '')) { 
				$client_ip = $_SERVER['HTTP_CLIENT_IP']; 
			} elseif (isset ($_SERVER['HTTP_FROM ']) && (trim ($_SERVER['HTTP_FROM']) != '')) { 
				$client_ip = $_SERVER['HTTP_FROM']; 
			} elseif (isset ($_SERVER['REMOTE_ADDR']) && (trim ($_SERVER['REMOTE_ADDR']) != '')) {
				$client_ip = $_SERVER['REMOTE_ADDR']; 
			}
			$this->headers['X-HTTP-Posting-Host'] = $client_ip; 
		} else { 
			$this->headers['X-HTTP-Posting-Host'] = $client_ip; 
		}

		if (empty ($proxy_server)) { 
			if (isset ($_SERVER['REMOTE_ADDR']) && ($client_ip != $_SERVER['REMOTE_ADDR'])) { 
				$this->headers['X-HTTP-Proxy-Server'] = $_SERVER['REMOTE_ADDR']; 
			} 
		} else { 
			$this->headers['X-HTTP-Proxy-Server'] = $proxy_server; 
		}

		if (empty ($user_agent)) { 
			if (isset ($_SERVER['HTTP_USER_AGENT']) && (trim ($_SERVER['HTTP_USER_AGENT']) != '')) { 
				$this->headers['X-HTTP-Posting-UserAgent'] = $_SERVER['HTTP_USER_AGENT']; 
			} else { 
				$this->headers['X-HTTP-Posting-UserAgent'] = "Unknown"; 
			}
		} else { 
			$this->headers['X-HTTP-Posting-UserAgent'] = $user_agent;
		}
		return true;
	}
	
	/**
	* setPriority method
	*
	* Sets the message priority (X-Priority header).
	* @param integer $priority acceptable values from 1 to 5
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setPriority ($priority = 3) {
		if (isset ($this->_ex_priorities[$priority])) { 
			$this->headers["X-Priority"] = $this->_ex_priorities[$priority]; 
			return true; 
		}
		return false;
	}

	/**
	* setReturnPath method
	*
	* Sets the Return-Path message header.
	* @param string $email the email address
	* @param string $name the relative name
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function setReturnPath ($email, $name = '') {
		if (!is_string ($email) || empty ($email)) { 
			return false; 
		} elseif (empty ($name)) { 
			parent::setReturnPath ($email); 
		} else { 
			$name = str_replace ('"', "'", $name);
			parent::setReturnPath ("\"$name\" <$email>"); 
		}
		return true;
	}
	
	/**
	* send method
	*
	* Sends the current message.
	* @param string $type acceptable values are "mail" and "smpt" (without double quotes).
	* @return boolean returns false if somethig goes wrong otherwise it returns true.
	*/
	function send ($type = 'mail') {
		parent::setCc (implode(', ', $this->_ex_cc));	
		parent::setBcc (implode(', ', $this->_ex_bcc));	
		return parent::send ($this->_ex_to, $type);
	}
}
?>
Return current item: htmlMimeMailEx