Location: PHPKode > projects > Thought Push PHP Framework > system/utilities/system/Email.php
<?
///class used to send email
/**
Uses:
@verbatim
Use with just HTML message
	email::send(to,subject,html,from);
Use with attachments:
	$message = '<h2>Hello</h2><p>Bob</p>';
	$email = new email($message);
	$email->attach('/var/www/humval/imgs/crystal.png');
	email::send('hide@address.com','testing',$email,'hide@address.com');
@endverbatim
*/
class Email{
	function __construct($html=''){
		$this->html = $html;
	}
	///send an email
	/**
	@param	to	to address "bob <hide@address.com>, hide@address.com"
	@param	subject	subject of email
	@param	email	email html -> only sends html at this point
	@param	from	from email address
	@options	array of options including bcc, cc.
	*/
	static function send($to,$subject,$email,$from=null,$options=null){
		if(is_string($email)){
			$email = new email($email);
		}
		if($email->attached){
			$boundary = Config::$x['emailUniqueId'].'-'.microtime();
			$email->header['Content-Type'] = 'multipart/mixed; boundary="'.$boundary.'"';
			$boundary = "\n\n--".$boundary."\n";
			$message = $boundary.
				'Content-Type: text/html; charset="iso-8859-1"'."\n".
				'Content-Transfer-Encoding: 7bit'."\n\n".
				$email->html;
			foreach($email->attached as $v){
				if(is_file($v)){
					$data = chunk_split(base64_encode(file_get_contents($v)));
					$mime = exec('file -ib '.$v);
					$name = basename($v);
					$message .= $boundary.
						'Content-Type: '.$mime.'; name="'.$name.'"'."\n".
						'Content-Transfer-Encoding: base64'."\n".
						'Content-Disposition: attachment'."\n\n".
						$data;
				}
			}
			
		}else{
			$email->header['Content-Type'] = 'text/html; charset=iso-8859-1';
			$message = $email->html;
		}
		if($from){
			$email->header['From'] = $email->header['Reply-To'] = $from;
		}
		if($options){
			if($options['cc']){
				$email->header['Cc'] = $options['cc'];
			}
			if($options['bcc']){
				$email->header['Bcc'] = $options['bcc'];
			}
		}
		exec('hostname',$out);
		$email->header['Message-Id'] = '<'.Config::$x['emailUniqueId'].'-'.sha1(uniqid(microtime()).rand(1,100)).'@'.$out[0].'>';
		foreach($email->header as $k=>$v){
			$headers .= $k.': '.$v."\r\n";
		}
		if(is_array($to)){
			$to = implode(', ',$to);
		}
		mail($to,$subject,$message,$headers);
		return $email->header['Message-Id'];
	}
	///attach a file to an email object
	/**
	@param	file	path to file
	*/
	function attach($file){
		$this->attached[] = $file;
	}
}
Return current item: Thought Push PHP Framework