Pages

PHP Class to send email

Sending email by PHP is very easy . You just have to supply value of receiver's email address, email subject line and body of email in mail function. 
class AttachmentEmail {
 private $from = 'email@tld.com';
 private $from_name = 'Firstname Last';
 private $reply_to = 'email@tld.com';
 private $to = '';
 private $subject = '';
 private $message = '';
 private $attachment = '';
 private $attachment_filename = '';

 public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '') {
  $this -> to = $to;
  $this -> subject = $subject;
  $this -> message = $message;
  $this -> attachment = $attachment;
  $this -> attachment_filename = $attachment_filename;
 }

 public function mail() {
  if (!empty($this -> attachment)) {
   $filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
   $path = dirname($this -> attachment);
   $mailto = $this -> to;
   $from_mail = $this -> from;
   $from_name = $this -> from_name;
   $replyto = $this -> reply_to;
   $subject = $this -> subject;
   $message = $this -> message;

   $file = $path.'/'.$filename;
   $file_size = filesize($file);
   $handle = fopen($file, "r");
   $content = fread($handle, $file_size);
   fclose($handle);
   $content = chunk_split(base64_encode($content));
   $uid = md5(uniqid(time()));
   $name = basename($file);
   $header = "From: ".$from_name." <".$from_mail.">\r\n";
   $header .= "Reply-To: ".$replyto."\r\n";
   $header .= "MIME-Version: 1.0\r\n";
   $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
   $header .= "This is a multi-part message in MIME format.\r\n";
   $header .= "--".$uid."\r\n";
   $header .= "Content-type:text/html; charset=iso-8859-1\r\n";
   $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
   $header .= $message."\r\n\r\n";
   $header .= "--".$uid."\r\n";
   $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use diff. tyoes here
   $header .= "Content-Transfer-Encoding: base64\r\n";
   $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
   $header .= $content."\r\n\r\n";
   $header .= "--".$uid."--";

   if (mail($mailto, $subject, "", $header, '-fwebmaster@bpkihsentrance.net')) {
    return true;
   } else {
    return false;
   }
  } else {
   $header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
   $header .= "Reply-To: ".($this -> reply_to)."\r\n";

   if (mail($this -> to, $this -> subject, $this -> message, $header,'-email@tld.com')) {
    return true;
   } else {
    return false;
   }

  }
 }
}
Use :
$sendmail = new AttachmentEmail('w38g0ru@anish.com.np', 'Testing mail() class', 'Your message here ', '/home/images/img.jpg');

$sendmail -> mail();

Validatinng date with regular expression

Regular expressions are sequence or pattern of characters . They provide the foundation for pattern-matching functionality. He is a example of how to validate date with regular expression.

Condition:
Date d/m/yy and dd/mm/yyyy
1/1/00 through 31/12/99 and 01/01/1900 through 31/12/2099
Matches invalid dates such as February 31st
'\b(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}\b'
Condition:
Date dd/mm/yyyy
01/01/1900 through 31/12/2099
Matches invalid dates such as February 31st
'(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9]{2}'
Condition:
Date m/d/y and mm/dd/yyyy
1/1/99 through 12/31/99 and 01/01/1900 through 12/31/2099
Matches invalid dates such as February 31st
Accepts dashes, spaces, forward slashes and dots as date separators
'\b(0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])[- /.](19|20)?[0-9]{2}\b'
Condition:
Date mm/dd/yyyy
01/01/1900 through 12/31/2099
Matches invalid dates such as February 31st
'(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9]{2}'
Condition:
Date yy-m-d or yyyy-mm-dd
00-1-1 through 99-12-31 and 1900-01-01 through 2099-12-31
Matches invalid dates such as February 31st
'\b(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])\b'
Condition:
Date yyyy-mm-dd
1900-01-01 through 2099-12-31
Matches invalid dates such as February 31st
'(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'

Implementing gravatars with PHP

It is quite simple. PHP provides strtolower(), md5(), and urlencode() functions, allowing us to create the gravatar URL with ease.
It can be constructed with the following php code.
$gravatar = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( "email@email.com") ) ) . "?d=" . urlencode( "http://url.tld/images/default.jpg" ) . "&s= 80";
In above example s=80 is size of the image.
Once the gravatar URL is created, you can output it whenever you please:
<.img alt="" src="<?php echo $gravatar; ?>" />
Alternate method by using function :
/**
 * Get either a Gravatar URL or complete image tag for a specified email address.
 *
 * @param string $email The email address
 * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
 * @param boole $img True to return a complete IMG tag False for just the URL
 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
 * @return String containing either just a URL or a complete image tag
 * @source http://gravatar.com/site/implement/images/php/
 */
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
    $url = 'http://www.gravatar.com/avatar/';
    $url .= md5( strtolower( trim( $email ) ) );
    $url .= "?s=$s&d=$d&r=$r";
    if ( $img ) {
        $url = ' $val )
            $url .= ' ' . $key . '="' . $val . '"';
        $url .= ' />';
    }
    return $url;
}
source : https://en.gravatar.com/site/implement/images/php/

PHP Function to trace Login information

Get domain name from where request is generated
$_SERVER['HTTP_HOST']
File Name
$_SERVER['PHP_SELF']
Internet Server via
getenv('HTTP_VIA')
I.P. Address
getenv('REMOTE_ADDR')
Your Network I.P. Address
getenv('HTTP_X_FORWARDED_FOR')
Your ISP server name
gethostbyaddr($_SERVER['REMOTE_ADDR'])
Browser
$_SERVER['HTTP_USER_AGENT']
Current Server Time
date('D,F j, Y, H:i:s A')

The New CSS Font-Family List

ARIAL:¹
font-family:Arial,'DejaVu Sans','Liberation Sans',Freesans,sans-serif;
ARIAL NARROW:
font-family:'Arial Narrow','Nimbus Sans L',sans-serif;
ARIAL BLACK:¹
font-family:'Arial Black',Gadget,sans-serif;
BOOKMAN: Easy-reading, wider and heavier than most serif fonts.
font-family:'Bookman Old Style',Bookman,'URW Bookman L','Palatino Linotype',serif;
CENTURY GOTHIC: Widely installed and should be used more in headings.
font-family:'Century Gothic',futura,'URW Gothic L',Verdana,sans-serif;
COMIC SANS MS:¹ People love it or hate it—there’s even a movement devoted to banning it. No Linux equivalent.

Analytics.js

Every project needs analytics. But you shouldn't have to litter your codebase with third-party-specific calls. Changing or adding new services should be a snap.

That's where analytics.js comes in! Instead of adding hooks for every single analytics service you integrate, you add a single set of provider-agnostic hooks that then route to any analytics service you want!

Source : http://segmentio.github.com/analytics.js/