Saturday, September 12, 2015

PHP Send html email

PHP has an inbuilt functionality to send email.
We can send email in php using the mail function.
The following function uses the inbuilt mail function to php to send email but in HTML format.
HTML emails are popular nowadays and almost 90% of the websites uses this features to send HTML emails to their customers.

function sendHTMLemail($HTML,$from,$to,$subject,$cc='')
{
// First we have to build our email headers
// Set out "from" address
$headers = "From: $from\r\n";
if($cc!='')
$headers.= "CC: $cc\r\n";
// Now we specify our MIME version
$headers .= "MIME-Version: 1.0\r\n";
// Create a boundary so we know where to look for
// the start of the data
$boundary = uniqid("HTMLEMAIL");
// First we be nice and send a non-html version of our email
$headers .= "Content-Type: multipart/alternative;".
"boundary = $boundary\r\n\r\n";
$headers .= "This is a MIME encoded message.\r\n\r\n";
$headers .= "--$boundary\r\n".
"Content-Type: text/plain; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode(strip_tags($HTML)));

// Now we attach the HTML version

$headers .= "--$boundary\r\n".
"Content-Type: text/html; charset=ISO-8859-1\r\n".
"Content-Transfer-Encoding: base64\r\n\r\n";
$headers .= chunk_split(base64_encode($HTML));

// And then send the email ....
mail($to,$subject,"",$headers);
}

 The function can be used as follows:

$HTML = "Hello John, <br /> How are you Today? <br /> Thanks!";
$from= = "Your email id here";
$to = "Email address of the person to whom you want to send the email";
$subject = "Test Email";
$cc = 'Email of the person to whom you want to keep in CC';

sendHTMLemail($HTML,$from,$to,$subject,$cc);

PHP create random password

There are many functions to create password like MDS, hash etc.
we can create a function to create our encryption method to create a password.
The following function create a random password using its own method and will not be easily identified the method of creating the password.

function STEM_Key()
{
// Create the meta-password
$sMetaPassword = "";
$ahPasswordGenerator = array(
"C" => array('characters' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'minimum' => 10, 'maximum' => 10),
"S" => array('characters' => "!@()-_=+?*^&", 'minimum' => 1, 'maximum' => 2),
"N" => array('characters' => '1234567890', 'minimum' => 10, 'maximum' => 10)
);
foreach ($ahPasswordGenerator as $cToken => $ahPasswordSeed){
$sMetaPassword .= str_repeat($cToken, rand($ahPasswordSeed['minimum'], $ahPasswordSeed['maximum']));
$sMetaPassword = str_shuffle($sMetaPassword);
}

// Create the real password
$arBuffer = array();
for ($i = 0; $i < strlen($sMetaPassword); $i ++){
$arBuffer[] = $ahPasswordGenerator[(string)$sMetaPassword[$i]]['characters'][rand(0, strlen($ahPasswordGenerator[$sMetaPassword[$i]]['characters']) - 1)];
}

return implode("", $arBuffer);
}

 You can view the output of the function in the following way.

echo STEM_Key();

PHP get extension of a file

PHP supports file uploads. user can design a system where he can allow users to upload files with specific extension type.
In many website we have the provision to upload files with specific file extension.
For example facebook we can upload file with extension jpeg, jpg,png etc for the profile picture.

Similarly it can be achieved in PHP. Following function returns you the extension of the given file type.

function getExtension($str)
{
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}

$filename = "images/image01.jpeg" ;
$extension=getExtension($filename);
echo "Extension of the file is: ".$extension;

The output of the above function call will be:

Extension of the file is: jpeg

php connect to ftp server

FTP Stands for File Transfer Protocol. It is used to transfer and download files from server.
The available tools are FileZilla, SFTP, FTPZilla etc..
The ftp_connect function is supported by php 4, 5.


If you want to connect to the server using FTP and using PHP programming language then you can do it in the following way.

$ftp_server = "ftp.yousite.com";
//trying to connect to the server using PHP
$conn_id = ftp_connect($ftp_server) or die("Couldn't connect to $ftp_server");

if($conn_id){
echo "Your connection ID is: ".$conn_id;
}

Check Disk space in Linux

To check disk space in linux operating system through command line use the following command.

df -h //show all the media and the total amount of space available and total space used.

du -h --max-depth=1 //foldername (for eg var, html, data)

This command shows you the space occupied by the folder till level 1.
If you want to see more deep than you can replace the depth=1 by any number for nth level.

Destroy a session in PHP

Suppose a session have started and you want to destroy the whole all data of session.
You can use the following inbuilt function of php.

session_start();
session_destroy();

This will destroy all the session and all the data associated with it.

Session ID in PHP

If a session is already started or if you want to check if a session have started or is active/valid then you can check in the following way.

session_start();
$session_id = session_id();

if($session_id){
echo "Session is Vaild and ID is: ".$session_id;
}
else{
echo "No Session have started";
}

Sunday, May 26, 2013

Change Background Color In Java

/*Program for background color changing using scrollbar*/

import java.awt.*;
import java.awt.event.*;

class sroll extends Frame implements AdjustmentListener,ActionListener
   {
        Button b;
        Scrollbar sr,sg,sb;
        Label lr,lg,lb;

public sroll()
  {
           b=new Button("EXIT:");
                       lr=new Label("RED:");
          lg=new Label("GREEN:");
          lb=new Label("BLUE:");

                       sr=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sg=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);
          sb=new Scrollbar(Scrollbar.HORIZONTAL,0,1,1,255);

           b.addActionListener(this);
           sr.addAdjustmentListener(this);
          sg.addAdjustmentListener(this);
          sb.addAdjustmentListener(this);

                      setLayout(new FlowLayout());

          add(lr);  add(sr);  add(lg);  add(sg);  add(lb);  add(sb);
add(b);

}


    public void actionPerformed(ActionEvent a1)
      {

System.exit(0);

       }



                 public void adjustmentValueChanged(AdjustmentEvent ae)
      {

 int r=sr.getValue();
 int g=sg.getValue();
 int b=sb.getValue();

Color c=new Color(r,g,b);

   setBackground(c);
   }

public static void main(String args[])
 {


sroll a=new sroll();

a.setSize(200,200);

a.setVisible(true);
}

    }

Convert Checkbox To Radio Button In Java

/*Program to convert checkboxes into radio buttons*/

import java.awt.*;

class elec extends Frame
  {
     Label l1,l2;
     Checkbox a,b,c,d,e,f;
     CheckboxGroup x,y;

        public elec()
{
  setTitle("UNIVERSITY FORM:");
               l1=new Label("OPTION 1:");
  l2=new Label("OPTION 2:");
               x=new CheckboxGroup();
               y=new CheckboxGroup();
              a=new Checkbox("maths",false,x);
              b=new Checkbox("chem",false,x);
              c=new Checkbox("physics",false,x);
              d=new Checkbox("biology",false,y);
              e=new Checkbox("english",false,y);
              f=new Checkbox("hindi",false,y);

          setLayout(new FlowLayout());

          add(l1); add(a);  add(b);  add(c);
          add(l2); add(d);  add(e);  add(f);
  }

    public static void main(String args[])throws Exception
       {
 
           elec r=new elec();

           
            r.setSize(200,300);
            r.setVisible(true);
       }
 
 } 

File Writing In Java

/*Program For File Writing In Java*/

import java.io.*;


class simple
  {
      public static void main(String args[])throws Exception
        {
                File f=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc");
            String fnames[]=f.list();
             

    System.out.println("NAME"+"  "+"LENGTH"+"  "+"STATUS"+"  "+"TYPE");

            for(int i=0;i<fnames.length;i++)
{
  File f1=new File("F:\\YOGESH (COLLEGE PROJECTS)\\sybsc",fnames[i]);
       
               System.out.print(" "+f1.getName());
   System.out.print(" "+f1.length());
if(f1.canRead()==true)
{
  System.out.print(" "+"R");
}
if(f1.canWrite()==true)
 {
   System.out.print(" "+"w");
 }
if(f1.isFile()==true)
{
  System.out.print(" "+"FILE");
}
if(f1.isDirectory()==true)
{
 System.out.print(" "+"DIR");
}
                        System.out.println("");
       }
      }
 }

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...