Friday, July 8, 2016

Dynamic Parse Mail Template

Most of us want a mailing system with template ...Simple Mailing does not always deliver best result


While we use Library like PHPMailer to send mail we Need to send the data  as text/HTML and that also dynamic Data to be sent in proper format .

Here is Example of How you can dynamically parse a mail template.


/////////////////////////////////////////////////////////////////////////////////////////////////////////////
// ABS_PATH = ABSOLUE PATH
// DS = SAPERATOR
// MAIL_TEMPLATE_PATH = TEMPLATE FILE PATH

argument 1 is name of the html or htm file to be parsed
argument 2 is the array which is to be parsed.

function ParseMailTemplate($Template, $Custom=""){
$GeneralKywords = array();
$GeneralKywords["SERVER_ROOT"]=SERVER_ROOT;

$f = fopen(ABS_PATH.DS.MAIL_TEMPLATE_PATH."/".$Template,"r");
if(!$f){
return NULL;
}
$TemplateBody = fread($f,filesize(ABS_PATH.DS.MAIL_TEMPLATE_PATH."/".$Template));
fclose($f);

$HTMLBody=$TemplateBody;

if(is_array($Custom)){
foreach($Custom as $Find=>$ReplaceWith){
$TemplateBody = str_replace("{".$Find."}",$ReplaceWith,$TemplateBody);
}
}

foreach($GeneralKywords as $Find=>$ReplaceWith){
$TemplateBody = str_replace("{".$Find."}",$ReplaceWith,$TemplateBody);
}

return $TemplateBody;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Demo template file : contact.html

Here variable in curly bracket are sent via array in function

Example

$mail_body = $this->ParseMailTemplate("contact.html",
array("name"=>$_POST['name'],
                                                                                "city"=>$_POST['city'],
                                                                                "phone"=>$_POST['phone'],
         "email"=>$_POST['email'],
 "msgbox"=>$_POST['msgbox'],
 )
 );


Then send the $mail_body as message in your mail .

All Done
Thanks


Thursday, July 7, 2016

Function to generate Random Key


HERE is a function to generate a random key in PHP

$Length = number of digit of which you need to generate random key


function GenerateRandomKey($Length){
  $random_chars="";
  $characters = array(
"A","B","C","D","E","F","G","H","J","K","L","M",
"N","P","Q","R","S","T","U","V","W","X","Y","Z",
"1","2","3","4","5","6","7","8","9");

//make an "empty container" or array for our keys
$keys = array();

//first count of $keys is empty so "1", remaining count is 1-6 = total 7 times
while(count($keys) < $Length) {
    //"0" because we use this to FIND ARRAY KEYS which has a 0 value
    //"-1" because were only concerned of number of keys which is 32 not 33
    //count($characters) = 33
    $x = mt_rand(0, count($characters)-1);
    if(!in_array($x, $keys)) {
       $keys[] = $x;
    }
}

foreach($keys as $key){
   $random_chars .= $characters[$key];
}
return $random_chars;



 }


Just pass the length parameter and will get random key generated

Example

echo $keygen = GenerateRandomKey(13);

OUTPUT

Every time you call the function and it would return a random key

KFZBHR18TN3X4
Y38JGTDVHCP9L
9DMNF6ACE37XQ
MUGSFC4RT5BKH

You can make the key more complex by adding character and digits  to $character array

Thanks

Cheers