<?php
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Class		:	email
* Version :	1.0
* Date    :	16 April 2004
* Author  :	Darwish A. Khalil
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* This class is used to send mails to different email addresses
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* Notes:
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
* All the data needed to connect to the SMTP server are stored
* in a seperate file to be included in this class for security reasons.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Class Definition
class email {

	// This function is used to send emails using SMTP socket connection
  function send_mail($auth=0, $to='', $subject='', $message='', $headers='') {
  	$lb = "\r\n";                 // Linebreak
   	$body_lb = "\r\n";            // Body linebreak

		// Read info from configuration file and save them in the variables of the function
		require_once("mail.cfg.php");
   	$loc_host = $local_host;   		// Localhost
    $smtp_host = $smtp_server;  	// SMTP Server
   	$smtp_acc = $smtp_username;   // Account (Username)
   	$smtp_pass = $smtp_password;	// Password
   	$from = $from_mail; // Email from


    // Prepare Headers
    if (empty($headers)) {
      $headers  = "MIME-Version: 1.0\r\n";
  	  $headers .= "Content-type: text/html; charset=windows-1256\r\n";
  	  $headers .= "From: $from\r\n";
    }
    
    //exit("Headers=$headers");
	  
    $hdr = explode($lb,$headers);

	  if($message) {$bdy = preg_replace("/^\./","..",explode($body_lb,$message));}

	  // Build the array for the SMTP dialog.
		// Line content is array(command, success code, additonal error message)
	  if($auth == 1){ // SMTP authentication methode AUTH LOGIN, use extended HELO "EHLO"
    	$smtp = array(
        // Call the server and tell the name of your local host
    	  array("EHLO ".$loc_host.$lb,"220,250","HELO error: "),
        // Request to authenticate
        array("AUTH LOGIN".$lb,"334","AUTH error:"),
        // Username
        array(base64_encode($smtp_acc).$lb,"334","AUTHENTIFICATION error : "),
        // Password
        array(base64_encode($smtp_pass).$lb,"235","AUTHENTIFICATION error : "));
	  }else{	// No authentication, use standard HELO
			$smtp = array(
	   	  // Call the server and tell the name of your local host
	      array("HELO ".$loc_host.$lb,"220,250","HELO error: "));
	  }

	  // Envelop: Mail From, To
    $smtp[] = array("MAIL FROM: <".$from.">".$lb,"250","MAIL FROM error: ");
    $smtp[] = array("RCPT TO: <".$to.">".$lb,"250","RCPT TO error: ");
    // Begin data
    $smtp[] = array("DATA".$lb,"354","DATA error: ");
    // Header
    $smtp[] = array("Subject: ".$subject.$lb,"","");
    $smtp[] = array("To:".$to.$lb,"","");
    foreach($hdr as $h) {$smtp[] = array($h.$lb,"","");}
    // End Header, Begin the body
    $smtp[] = array($lb,"","");
    if($bdy) {foreach($bdy as $b) {$smtp[] = array($b.$body_lb,"","");}}
    // End of message
    $smtp[] = array(".".$lb,"250","DATA(end)error: ");
    $smtp[] = array("QUIT".$lb,"221","QUIT error: ");

    // Open socket
    $fp = @fsockopen($smtp_host, 25);
    if (!$fp) echo "<b>Error:</b> Cannot connect to ".$smtp_host."<br>";

    $banner = @fgets($fp, 1024);
    // Perform the SMTP dialog with all lines of the list
    foreach($smtp as $req){
      $r = $req[0];
      // Send request
      @fputs($fp, $req[0]);
      // Get available server messages and stop on errors
      if($req[1]){
        while($result = @fgets($fp, 1024)){if(substr($result,3,1) == " ") { break; }};
        if (!strstr($req[1],substr($result,0,3))) echo"$req[2].$result<br>";
      }
    }
    $result = @fgets($fp, 1024);
    // Close socket
    @fclose($fp);
    //return 1;
	}
}
?>
