Location: PHPKode > scripts > Forum Class > mime_mail_php5.php
<?
class Mailer{
	private $sender;
	private $reply_to;
	private $recipient;
	private $subject;
	private $headers=array();
	private $mimeTypes=array();
	private $html=array();
	private $attachments=array();
	public function __construct($sender,$reply_to,$recipient,$subject,$message){
		// validate incoming parameters
		if(!preg_match("/^.+@.+$/",$sender)){
			throw new Exception('Invalid value for email sender.');
		}
		if(!preg_match("/^.+@.+$/",$reply_to)){
			throw new Exception('Invalid value for email reply-to.');
		}
		if(!preg_match("/^.+@.+$/",$recipient)){
			throw new Exception('Invalid value for email recipient.');
		}
		if(!$subject||strlen($subject)>255){
			throw new Exception('Invalid length for email subject.');
		}
		//if(!$message){
			//throw new Exception('Invalid value for email message.');
		//}
		$this->sender=$sender;
		$this->reply_to=$reply_to;
		$this->recipient=$recipient;
		$this->subject='=?UTF-8?B?'.base64_encode($subject).'?=';
		$this->message=$message;
		// define some default MIME headers
		$this->headers['MIME-Version']='1.0';
		$this->headers['Content-Type']='multipart/mixed;charset=UTF-8;boundary="MIME_BOUNDRY"';
		$this->headers['From']='<'.$this->sender.'>';
		$this->headers['Return-Path']='<'.$this->sender.'>';
		$this->headers['Reply-To']=$this->reply_to;
		$this->headers['X-Mailer']='PHP5';
		$this->headers['X-Sender']=$this->sender;
		$this->headers['X-Priority']='3';
	}
	// add new MIME header
	public function addHeader($name,$value){
		$this->headers[$name]=$value;
	}
	// add HTML to message
	public function addHTML($html){
		if(!$html){
			throw new Exception('Invalid HTML.');
		}
		$this->html[]=$html;
	}
	// add new attachment
	public function addAttachment($attachment){
		if(!file_exists($attachment)){
			throw new Exception('Invalid attachment.');
		}
		$this->attachments[]=$attachment;
	}
	// get MIME Type of attachment
	/*private function getMimeType($attachment){
		$attachment=explode('.',basename($attachment));
		if(!$mimeType=array_search(strtolower($attachment[count($attachment)-1]),$this->mimeTypes)){
			throw new Exception('MIME Type not found.');
		}
		return $mimeType;
	}*/
	private function getMimeType($file_path){
		$mtype = '';
		if (function_exists('mime_content_type')){
	    	     $mtype = mime_content_type($file_path);
	  	}
		else if (function_exists('finfo_file')){
	    	     $finfo = finfo_open(FILEINFO_MIME);
	    	     $mtype = finfo_file($finfo, $file_path);
	    	     finfo_close($finfo);  
	  	}
		if ($mtype == ''){
	    	     $mtype = "application/force-download";
	  	}
		return $mtype;
	}
	// create message MIME headers
	private function buildHeaders(){
		foreach($this->headers as $name=>$value){
			$headers[]=$name.': '.$value;
		}
		return implode("\n",$headers)."\nThis is a multi-part message in MIME format.\n";
	}
	// create text part of the message
	private function buildTextPart(){
		if($this->message){
			return "--MIME_BOUNDRY\nContent-Type: text/plain; charset=utf-8\nContent-Transfer-Encoding: quoted-printable\n\n\n".$this->message."\n\n";
		}else{
			return '';
		}
	}
	// create HTML part of the message
	private function buildHTMLPart(){
		if(count($this->html)>0){
			$htmlPart='';
			foreach($this->html as $html){
				$htmlPart.="--MIME_BOUNDRY\nContent-Type: text/html; charset=utf-8\nContent-Transfer-Encoding: 8bit\n\n\n".$html."\n\n";
			}
			return $htmlPart;
		}
	}
	// create attachments part of the message
	private function buildAttachmentPart(){
		if(count($this->attachments)>0){
			$attachmentPart='';
			foreach($this->attachments as $attachment){
				if(!$fileStr=file_get_contents($attachment)){
					throw new Exception('Error reading contents of attachment.');
				}
				$fileStr=chunk_split(base64_encode($fileStr));
				$attachmentPart.="--MIME_BOUNDRY\nContent-Type: ".$this->getMimeType($attachment)."; name=\"".basename($attachment)."\"\nContent-disposition:attachment\nContent-Transfer-Encoding: base64\n\n".$fileStr."\n\n";
			}
			return $attachmentPart;
		}
	}
	// send email
	public function send(){
		$to=$this->recipient;
		$subject=$this->subject;
		$headers=$this->buildHeaders();
		$message=$this->buildTextPart().$this->buildHTMLPart().$this->buildAttachmentPart()."--MIME_BOUNDRY--\n";
		if(!mail($to,$subject,$message,$headers)){
			throw new Exception('Error sending email.');
		}
	}
}
?>
Return current item: Forum Class