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 protected]';
 private $from_name = 'Firstname Last';
 private $reply_to = '[email protected]';
 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, '[email protected]')) {
    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 protected]')) {
    return true;
   } else {
    return false;
   }

  }
 }
}
Use :
$sendmail = new AttachmentEmail('[email protected]', '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])'