Location: PHPKode > scripts > mailMine > mailmine/mail_mine.class.php
<?php

/*
    SMTP class, developer version
    Ivan Markovic
*/

/* Usage

    include "mail_mine.class.php";
    
    // - Create object
    $mymail = new mail_mine();
    // - Addresses
    $mymail->to_mine = "";
    $mymail->cc_mine = "";
    $mymail->bcc_mine = "";
    $mymail->from_mine = "";
    $mymail->replyto_mine = "";
    // - Adds
    $mymail->subject_mine = "test smtp object";
    $mymail->message_mine = "Lorem ipsum dolor sit amet, <strong>consectetur</strong> adipisicing elit, ...";
    // - Options
    $mymail->htmlemail = true;
    $mymail->haveattach = false;  
    // Attachments
    $mymail->attachments = array("attach0.pdf","attach1.ppt","attach9.ppt");
    // - Functions
    $mymail->send_mail();
    $mymail->error_machine(1);
    
		if($mymail->debug_mine === true && $mymail->error_mine == "") {
			echo "Ok, mail is sent.";
		} else {
			echo "Error!";
		}
    
*/    

class mail_mine {

var $to_mine = "";                                // send to
var $cc_mine = "";                                // cc
var $bcc_mine = "";                               // bcc
var $subject_mine = "";							              // subject
var $message_mine = "";							              // message
var $from_mine = "";							                // from
var $replyto_mine = "";							              // reply to

var $htmlemail = false;                           // html email
var $haveattach = false;                          // attachment

var $debug_mine = "";                             // error output
var $error_mine = "";                             // error handler

var $headers_mine = "";                           // headers param

var $not_valid_mails = array();                   // Not valid email address

var $attachments = array();                       // Atachments
var $not_valid_attachments = array();             // Not valid email atachments

    // Constructor
    function mail_mine() {

    }
    
    // Check this
    function check_this() {
    
      $mails = array("to_mine" => "$this->to_mine", "cc_mine" => "$this->cc_mine", "bcc_mine" => "$this->bcc_mine",
    				         "from_mine" => "$this->from_mine", "replyto_mine" => "$this->replyto_mine");
    	
    	$regexp_email = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

    	foreach ($mails as $mails_key => $mails_val) {
    			
    			if (strstr($mails_val,",")) {
    				  // If more than one mail (must be separated by coma)
    				  $mails_plus = explode (",",$mails_val);
    				  foreach ($mails_plus as $mpk => $mpv) {
    				    
    				    if ($mails_key == "to_mine" && !eregi($regexp_email, $mpv)) {
    				    	$this->error_mine .= "'To' field is not valid.<br />";
	    				    break;	
    				    }
    				    
    				    if (!eregi($regexp_email, $mpv)) {
	    				    array_push($this->not_valid_mails, $mails_key);	
	    				}	
    				  }
    				  
    			} else {
	    			if ($mails_key == "to_mine" && $mails_val == "") {
	    			    $this->error_mine .= "'To' field is empty.<br />";
	    				break;
	    			} elseif ( $mails_key == "to_mine" && !eregi($regexp_email, $mails_val) ) {
	    				$this->error_mine .= "'To' field is not valid.<br />";
	    				break;
	    			} else {
	    				if (!eregi($regexp_email, $mails_val)) {
	    				    array_push($this->not_valid_mails, $mails_key);	
	    				}
	    			}
		      }

    	}
    }
    
    // Headers creator
    function mail_header() {
    	
    	 $cc_minez = "cc: " . $this->cc_mine . "\n"; 
    	 $bcc_minez = "Bcc: " . $this->bcc_mine . "\n";
    	 $from_minez = 'From: '. $this->from_mine . "\n";
    	 $replyto_minez = 'Reply-To: '. $this->replyto_mine . "\n" .
    	 
    	 $mailsxx = array("cc_mine" => "$this->cc_mine", "bcc_mine" => "$this->bcc_mine",
    					  "from_mine" => "$this->from_mine", "replyto_mine" => "$this->replyto_mine");
    	 
    	 foreach ($mailsxx as $mails_key => $mails_val) {
    	 			
 			    if ( !in_array($mails_key, $this->not_valid_mails) ) {
 			        $this->headers_mine .= ${$mails_key . "z"};			
 			    }	
    	 			
    	 }
    	 
    	 
    	 if ( $this->htmlemail === true && $this->haveattach === false) {
    	    $this->headers_mine .= "MIME-Version: 1.0\n" . 
		                           "Content-type: text/html; charset=utf-8\n";		    
    	 }
    	
    	 // + Attach
    	 if ( $this->haveattach === true ) {
    	 		
	 		// Generate a boundary string 
				$semi_rand = md5(time()); 
				$mime_boundary = '------------'.$semi_rand;
				
			// Add the headers for a file attachment 
			$this->headers_mine .= "MIME-Version: 1.0\n" . 
              					   "Content-Type: multipart/mixed;\n" . 
                                   " boundary=\"{$mime_boundary}\"";
                      
            // Add a multipart boundary above the message 
      	    $this->message_mine = "--{$mime_boundary}\n" . 
					              "Content-Type: text/html; charset=\"utf-8\"\n" . 
					              "Content-Transfer-Encoding: 7bit\n\n" . 
					              $this->message_mine . "\n\n";
											         
					foreach ($this->attachments as $keya => $vala) { 
					   
							// Read the file to be attached ('rb' = read binary)
							if (file_exists($vala)) {
									$file = fopen($vala,'rb'); 
									$data = fread($file,filesize($vala)); 
									fclose($file);
									
									$fileatt_type = mime_content_type($vala);
							 
									// Base64 encode the file data 
									$data = chunk_split(base64_encode($data));
				
				
									// Add file attachment to the message 
									$this->message_mine .= "--{$mime_boundary}\n" . 
												           "Content-Type: {$fileatt_type};\n" . 
												           " name=\"{$vala}\"\n" . 
												           "Content-Disposition: inline;\n" . 
												           " filename=\"{$vala}\"\n" . 
												           "Content-Transfer-Encoding: base64\n\n" . 
												           $data .
												           "--{$mime_boundary}\n\n";
													           
						  } else {
						  	  array_push($this->not_valid_attachments,$vala);
						  }
						  
					}								           
    	 }
    	 
    	 // X mailer
    	 // $this->headers_mine .= "X-Mailer: PHP " . phpversion() . "\n";
    }
    
    // Send mail
    function send_mail() {
				
		$this->check_this();
		$this->mail_header();
		
		if ($this->error_mine == "" ) {
			
			$ret_mine = mail($this->to_mine,$this->subject_mine,$this->message_mine,$this->headers_mine);
			$this->debug_mine = $ret_mine;

		}
    				
    }

    // Debug
    function error_machine($swr = 0) {
    
      if ($swr == 1) {
      	
			    echo "\n<!--\n\n";
					echo "Mail function: " . $this->debug_mine . "\n\n";
					echo "Mail class: " . $this->error_mine . "\n\n";
					echo "Not valid params: " . implode( ",", $this->not_valid_mails ) . "\n\n";
					echo "Not valid attach: " . implode( ",", $this->not_valid_attachments ) . "\n\n";
					echo "-->\n";
    	}
    	
    }
    
}

?>
Return current item: mailMine