Scripts

[CLASS] Mail

Met deze class kun je (hopelijk:D) makkelijker mailtjes versturen. Alles spreekt voor zich. Gebruik:

class-mail
[code]
<?php
/*
* @Name    Weet ik veel meel:D
* @Author  Mark Pieper
* @Date    23-01-07
* @Todo    Bijlagen
?>
*/
class Mail {
 
 	/**
 	* Email onderwerp
 	*/
 	protected $subject;
 	
 	/**
 	* Email bericht
 	*/
 	protected $msg;
 
 	/**
 	* Email ontvanger
 	*/
 	protected $email_recipient;
 	
 	/**
 	* Naam ontvanger
 	*/
 	protected $name_recipient;
 	
 	/**
 	* Email zender
 	*/
 	protected $email_sender;
 	
 	/**
 	* Naam zender
 	*/
 	protected $name_sender;
 	
 	/**
 	* Carbon Copy
 	*/
 	protected $cc = array();
 	
 	/**
 	* Blind Carbon Copy
 	*/
 	protected $bcc =  array();
 	
 	/**
 	* Email headers
 	*/
 	protected $headers = array();
 	
 	/**
 	* Constructor
 	* @param: $content_type = html of text
 	*/
	public function __construct($content_type = 'text') {
		if($content_type == 'html') {
			$this -> add_header('Content-Type: text/html; charset=iso-8859-1');
		}
		else
		{
			$this -> add_header('Content-Type: text/plain; charset=iso-8859-1');
		}
		$this -> add_msg('');
	}

	/**
 	* Verzend de email
 	*/	
	public function send_mail() {
		$header = $this -> make_headers();		
		if(mail($this -> email_recipient, $this -> subject, $this -> msg, $header)) {
			return 'Versturen gelukt';
		}
		else
		{
			trigger_error('<p><strong>Er is een fout opgetreden bij het verzenden van de email!</strong></p>');
		}
	}	
	
	/**
	* Stel het onderwerp in
 	* @param: $subject = het onderwerp van de email
 	*/
	public function set_subject($subject) {
        $this -> subject = $subject;
	}
	
	/**
 	* Stel de naam en de email van de ontvanger in
 	* @param: $name = de naam van de ontvanger
 	* @param: $email = het emailadres van de ontvanger
 	*/
 	public function set_recipient($name, $email) {
 		$this -> name_recipient =  $name;
		$this -> email_recipient =  $email;
	}
 	
 	/**
 	* Stel de naam en de email van de verzender in
 	* @param: $name = de naam van de verzender
 	* @param: $email = het emailadres van de verzender
 	*/
 	public function set_sender($name, $email) {
 		$this -> name_sender =  $name;
		$this -> email_sender =  $email;
	}
	
	/**
	* Voeg een CC toe
 	* @param: $cc = Carbon Copy (Mag een array zijn)
 	*/
	public function add_cc($cc) {
		if(is_array($cc)) {
        	foreach($cc as $waarde) {
				$this -> cc[] = $waarde;
			}
		}
		else
		{
        	$this -> cc[] = $cc;
		}		
	}
	
	/**
	* Maak het bericht
 	* @param: $msg = het bericht van de email
 	*/
	public function add_msg($msg) {
        $this -> msg .= $msg;
	}
	
	/**
	* Voeg een BCC toe
 	* @param: $cc = Blind Carbon Copy (Mag een array zijn)
 	*/
	public function add_bcc($bcc) {
        if(is_array($bcc)) {
        	foreach($bcc as $waarde) {
				$this -> bcc[] = $waarde;
			}
		}
		else
		{
        	$this -> bcc[] = $bcc;
		}
	}
	
	/*** Protected Functions ***/
	
	/**
 	* @param: $header = De header die moet worden toegevoegd
 	*/
	protected function add_header($header) {
        $replace = array("\n", "\r");
        $header = str_replace($replace, '', $header);
        $this -> headers[] = $header."\r\n";
	}
	
	/**
 	* Maak de headers, en voeg ze toe
 	*/
	protected function make_headers() {	 
	 	$this -> add_header('MIME-Version: 1.0');
	 	$this -> add_header('X-Priority: 3');
	 	$this -> add_header('X-MSMail-Priority: Normal');
	 	$this -> add_header('X-Mailer: PHP/'.phpversion());
		$this -> add_header('Return-Path: '.$this -> name_sender.' <'.$this -> email_sender.'>');
		$this -> add_header('Reply-To: '.$this -> name_sender.' <'.$this -> email_sender.'>');
		if(count($this -> cc != 0)) { // Als Carbon Copy is ingesteld, maak de header
			$cc = implode(', ', $this -> cc);
			$this -> add_header('Cc: '.$cc.'');
		}
		if(count($this -> bcc != 0)) { // Als Blind Carbon Copy is ingesteld, maak de header
			$bcc = implode(', ', $this -> bcc);
			$this -> add_header('Bcc: '.$bcc.'');
		}	
		$this -> add_header('To: '.$this -> name_recipient.' <'.$this -> email_recipient.'>');
		$this -> add_header('From: '.$this -> name_sender.' <'.$this -> email_sender.'>');
        $headers = implode("\n", $this -> headers);
		return $headers;	
	}
}
[/code]

Reacties

0
Nog geen reacties.