| | Create free blog ( Türkçe , Deutsch , Español )

php snippets,php examples , php tutorials , php help, php codes , php scripts download

Email class php code



////////////////////////////////////////////////////////////
//   EmailClass 0.5
//   class for sending mail
//
//   Paul Schreiber
//   php@paulschreiber.com
//   http://paulschreiber.com/
//
//   parameters
//   ----------
//   - subject, message, senderName, senderEmail and toList are required
//   - ccList, bccList and replyTo are optional
//   - toList, ccList and bccList can be strings or arrays of strings
//     (those strings should be valid email addresses
//
//   example
//   -------
//   $m = new email ( "hello there",            // subject
//                    "how are you?",           // message body
//                    "paul",                   // sender's name
//                    "foo@foobar.com",         // sender's email
//                    array("paul@foobar.com", "foo@bar.com"), // To: recipients
//                    "paul@whereever.com"      // Cc: recipient
//                   );
//
//       print "mail sent, result was" . $m->send();
//
//
//

if ( ! defined( 'MAIL_CLASS_DEFINED' ) ) {
        define('MAIL_CLASS_DEFINED', 1 );

class email {

        // the constructor!
        function email ( $subject, $message, $senderName, $senderEmail, $toList, $ccList=0, $bccList=0, $replyTo=0) {
                $this->sender = $senderName . " <$senderEmail>";
                $this->replyTo = $replyTo;
                $this->subject = $subject;
                $this->message = $message;

                // set the To: recipient(s)
                if ( is_array($toList) ) {
                        $this->to = join( $toList, "," );
                } else {
                        $this->to = $toList;
                }

                // set the Cc: recipient(s)
                if ( is_array($ccList) && sizeof($ccList) ) {
                        $this->cc = join( $ccList, "," );
                } elseif ( $ccList ) {
                        $this->cc = $ccList;
                }
                
                // set the Bcc: recipient(s)
                if ( is_array($bccList) && sizeof($bccList) ) {
                        $this->bcc = join( $bccList, "," );
                } elseif ( $bccList ) {
                        $this->bcc = $bccList;
                }

        }

        // send the message; this is actually just a wrapper for 
        // PHP's mail() function; heck, it's PHP's mail function done right
        // you could override this method to:
        // (a) use sendmail directly
        // (b) do SMTP with sockets
        function send () {
                // create the headers needed by PHP's mail() function

                // sender
                $this->headers = "From: " . $this->sender . "\n";

                // reply-to address
                if ( $this->replyTo ) {
                        $this->headers .= "Reply-To: " . $this->replyTo . "\n";
                }

                // Cc: recipient(s)
                if ( $this->cc ) {
                        $this->headers .= "Cc: " . $this->cc . "\n";
                }

                // Bcc: recipient(s)
                if ( $this->bcc ) {
                        $this->headers .= "Bcc: " . $this->bcc . "\n";
                }
        
                return mail ( $this->to, $this->subject, $this->message, $this->headers );
        }
}


}

Clever Email Validation Function ( Email-Validation ) php code


/* ======================================================================= 
   
 ifsnow's email valid check function SnowCheckMail Ver 0.1 
  
 funtion SnowCheckMail ($Email,$Debug=false) 

 $Email : E-Mail address to check. 
 $Debug : Variable for debugging. 

 * Can use everybody if use without changing the name of function. 

 Reference : O'REILLY - Internet Email Programming 

 HOMEPAGE : http://www.hellophp.com 

 ifsnow is korean phper. Is sorry to be unskillful to English. *^^*;; 

========================================================================= */ 

function SnowCheckMail($Email,$Debug=false) 
{ 
    global $HTTP_HOST; 
    $Return =array();  
    // Variable for return. 
    // $Return[0] : [true|false] 
    // $Return[1] : Processing result save. 

    if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) { 
        $Return[0]=false; 
        $Return[1]="${Email} is E-Mail form that is not right."; 
        if ($Debug) echo "Error : {$Email} is E-Mail form that is not right.
"; return $Return; } else if ($Debug) echo "Confirmation : {$Email} is E-Mail form that is not right.
"; // E-Mail @ by 2 by standard divide. if it is $Email this "lsm@ebeecomm.com".. // $Username : lsm // $Domain : ebeecomm.com // list function reference : http://www.php.net/manual/en/function.list.php // split function reference : http://www.php.net/manual/en/function.split.php list ( $Username, $Domain ) = split ("@",$Email); // That MX(mail exchanger) record exists in domain check . // checkdnsrr function reference : http://www.php.net/manual/en/function.checkdnsrr.php if ( checkdnsrr ( $Domain, "MX" ) ) { if($Debug) echo "Confirmation : MX record about {$Domain} exists.
"; // If MX record exists, save MX record address. // getmxrr function reference : http://www.php.net/manual/en/function.getmxrr.php if ( getmxrr ($Domain, $MXHost)) { if($Debug) { echo "Confirmation : Is confirming address by MX LOOKUP.
"; for ( $i = 0,$j = 1; $i < count ( $MXHost ); $i++,$j++ ) { echo "        Result($j) - $MXHost[$i]
"; } } } // Getmxrr function does to store MX record address about $Domain in arrangement form to $MXHost. // $ConnectAddress socket connection address. $ConnectAddress = $MXHost[0]; } else { // If there is no MX record simply @ to next time address socket connection do . $ConnectAddress = $Domain; if ($Debug) echo "Confirmation : MX record about {$Domain} does not exist.
"; } // fsockopen function reference : http://www.php.net/manual/en/function.fsockopen.php $Connect = fsockopen ( $ConnectAddress, 25 ); // Success in socket connection if ($Connect) { if ($Debug) echo "Connection succeeded to {$ConnectAddress} SMTP.
"; // Judgment is that service is preparing though begin by 220 getting string after connection . // fgets function reference : http://www.php.net/manual/en/function.fgets.php if ( ereg ( "^220", $Out = fgets ( $Connect, 1024 ) ) ) { // Inform client's reaching to server who connect. fputs ( $Connect, "HELO $HTTP_HOST\r\n" ); if ($Debug) echo "Run : HELO $HTTP_HOST
"; $Out = fgets ( $Connect, 1024 ); // Receive server's answering cord. // Inform sender's address to server. fputs ( $Connect, "MAIL FROM: <{$Email}>\r\n" ); if ($Debug) echo "Run : MAIL FROM: <{$Email}>
"; $From = fgets ( $Connect, 1024 ); // Receive server's answering cord. // Inform listener's address to server. fputs ( $Connect, "RCPT TO: <{$Email}>\r\n" ); if ($Debug) echo "Run : RCPT TO: <{$Email}>
"; $To = fgets ( $Connect, 1024 ); // Receive server's answering cord. // Finish connection. fputs ( $Connect, "QUIT\r\n"); if ($Debug) echo "Run : QUIT
"; fclose($Connect); // Server's answering cord about MAIL and TO command checks. // Server about listener's address reacts to 550 codes if there does not exist // checking that mailbox is in own E-Mail account. if ( !ereg ( "^250", $From ) || !ereg ( "^250", $To )) { $Return[0]=false; $Return[1]="${Email} is address done not admit in E-Mail server."; if ($Debug) echo "{$Email} is address done not admit in E-Mail server.
"; return $Return; } } } // Failure in socket connection else { $Return[0]=false; $Return[1]="Can not connect E-Mail server ({$ConnectAddress})."; if ($Debug) echo "Can not connect E-Mail server ({$ConnectAddress}).
"; return $Return; } $Return[0]=true; $Return[1]="{$Email} is E-Mail address that there is no any problem."; return $Return; }

Algorithms - aprint() - php code





function _aprint($arr, $tab = 1) // similar to print_r()
{	
	if( !is_array($arr) ) return " <span style=\"color:#336699\">".ucfirst(gettype($arr))."</span> "._slashes($arr);

	$space = str_repeat("\t", $tab);
	$out = " <span style=\"color:#336699\">array(</span>\n";
	end($arr); $end = key($arr); 
	
	if( count($arr) == 0 )
		return "<span style=\"color:#336699\">array()</span>";
	
	foreach( $arr  as $key=>$val ){
		if( $key == $end ) $colon = ''; else $colon = ',';
		if( !is_numeric($key) ) $key = "<span style=\"color:#993366\">'".str_replace( array("\\","'"), array("\\\\","\'"), htmlspecialchars($key) )."'</span>";
		if(  is_array($val)   ) $val = _aprint($val, ($tab+1)); else
		if( !is_numeric($val) ) $val = "<span style=\"color:#993366\">'".str_replace( array("\\","'"), array("\\\\","\'"), htmlspecialchars($val) )."'</span>";
		$out .= "$space$key => $val$colon\n";
	}
	
	if( $tab == 1 )
	return "$out$space<span style=\"color:#336699\"> )</span>;"; else
	return "$out$space<span style=\"color:#336699\"> )</span>";
}


function aprint( $arr, $prefix=''){
	if( ltrim($prefix) != '' ) $prefix = '<span style="color:#336699">'.$prefix.'</span> =';
	echo "\n\n<table style=\"width:100%; margin:1px; background:#F0F2F4; border:1px solid #D8DDE6;\"><tbody><tr><td><pre style=\"color:#000000;\">$prefix"._aprint($arr)."</pre></td></tr></tbody></table>\n\n";
	// overflow:auto;
}


	

Algorithms -aasort() sort Associative Arrays by one or more Keys - php code


/*
I use it for sorting the rows of MYSQL_ASSOC-results without the need to
query the database again... it helps when you have slow querys whose result only needs to be sorted again.

First transfer the result into an array like this:
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) $DB_Array[] = $row;

Syntax: aasort($assoc_array, array("+first_key", "-second_key", etc..));

Example: aasort($db_array, array("+ID", "-AGE", "+NAME"));
Where the "+" in front of the keys stands for "ASC" and "-" for "DESC".

This sorts the array first ascending by "ID", then descending by "AGE" and
finally ascending by "NAME".

Note: the function does no error handling... so make sure all keys exist in the
array and they have a + or - as the first character.
To keep the $DB_Array between pages use sessions.

To use the function with other arrays, they must have the following format:

$array[1] = array("key" => "value", "key" => "value", etc..);
$array[2] = array("key" => "value", "key" => "value", etc..);
$array[4] = array("key" => "value", "key" => "value", etc..);
etc.

*/


function aasort(&$array, $args) {
	foreach($args as $arg) {
		$order_field = substr($arg, 1, strlen($arg)); 
		foreach($array as $array_row) {
			$sort_array[$order_field][] = $array_row[$order_field];
		}
		$sort_rule .= '$sort_array['.$order_field.'], '.($arg[0] == "+" ? SORT_ASC : SORT_DESC).',';
	}
	eval ("array_multisort($sort_rule".' &$array);');
}

Algorithms - a more readable option that "print_r" - php code


  
  /*
    Function coded by Stephan Pirson
    contact: saibot@hesperia-mud.org
  */
  
  function print_a( $TheArray )
  { // Note: the function is recursive
    echo "<table border=1>\n";
    
    $Keys = array_keys( $TheArray );
    foreach( $Keys as $OneKey )
    {
      echo "<tr>\n";
      
      echo "<td bgcolor='#727450'>";
      echo "<B>" . $OneKey . "</B>";
      echo "</td>\n";
      
      echo "<td bgcolor='#C4C2A6'>";
        if ( is_array($TheArray[$OneKey]) )
          print_a($TheArray[$OneKey]);
        else
          echo $TheArray[$OneKey];
      echo "</td>\n";
      
      echo "</tr>\n";
    }
    echo "</table>\n";
  }

Algorithms - ( minus - subtract ) arrays - php code



function RestaDeArrays($vectorA,$vectorB)
         {
           $cantA=count($vectorA);
           $cantB=count($vectorB);
           $No_saca=0;
           for($i=0;$i<$cantA;$i++)
                {
                 for($j=0;$j<$cantB;$j++)
                      {
                       if($vectorA[$i]==$vectorB[$j])
                           $No_saca=1;
                      }

                 if($No_saca==0)
                    $nuevo_array[]=$vectorA[$i];
                   else
                   $No_saca=0;
                }

           return $nuevo_array;
         }