Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, 22 July 2016

Emailing With PHPMailer And GMail SMTP



PHPMailer And GMail SMTP w3workers
PHPMailer And GMail SMTP

Almost every website require emailing function for sending emails to subscribers you can use emailing function in your application according to your website requirement.


1) For sending Autoresponder.

2) For sending Newsletters.
3) For Sending notification about new product.
4) For sending other information like about users account activities etc.

Emailing in PHP is very simple.

If your website hosting provider allows you to send mails from same hosting server (local mail server) in that case you can use 
default mailing function of php for sending emails.

Example of PHP mail() function



  1. <?php
  2. // Message
  3. $message = "Simple Text Message";

  4. // Calling 
  5. mail('w3workers@gmail.com', 'Subject of Message', $message);
  6. ?>


But in case if you want more flexible emailing you can use SMTP configuration with your website.


SMTP stands for Simple Mail Transfer Protocol which is used for sending emails, Various SMTP service provider allows email sending with lower cost and high inbox delivery.


List of some popular SMTP providers 



  • Amazon SES
  • MailGun
  • SendGrid
  • Sparkpost
  • Postmark
apart from this list also GMail Provides email sending from GMail SMTP server free of cost with daily sending limit upto 150 emails.

PHPMailer is the most popular open source flexible emailing library for PHP.


Below is steps for emailing with PHPMailer:


Example of subscriber form for sending Autoresponder.

 Step 1:
Create a directory name "emailer".

Step 2:

Create two php files inside directory "emailer".
index.php
sendmail.php

Step 3: 

Create html form in index.php file.



  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Emailer</title>
  6. <style>
  7. .subscriber-form{
  8.        margin-top:50px;
  9. }
  10. .txt{
  11.       width: 20%;
  12.     padding: 12px 20px;
  13.     margin: 8px 0;
  14.     box-sizing: border-box;
  15.       font-size: 16px;
  16. }
  17. .lbl{
  18.       display:block;
  19.       font-size:18px;
  20. }
  21. .btn{
  22.       width: 20%;
  23.       background-color: #4CAF50;
  24.     border: none;
  25.     color: white;
  26.     padding: 15px 32px;
  27.     text-align: center;
  28.     text-decoration: none;
  29.     display: inline-block;
  30.     font-size: 16px;
  31.     margin: 3px 2px;
  32.     cursor: pointer;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="subscriber-form">
  38. <form method="post" action="sendmail.php">
  39. <h2>Subscribe with w3workers</h2>
  40. <div class='row'>
  41. <label class="lbl" for="txt_name">Your Name</label>
  42. <input type="text" id="txt_name" name="txt_name" class="txt" placeholder="Your Name"/>
  43. </div>
  44. <div class='row'>
  45. <label class="lbl" for="txt_email">Your Email</label>
  46. <input type="text" id="txt_email" name="txt_email" class="txt" placeholder="w3workers@gmail.com"/>
  47. </div>
  48. <div class='row'>
  49. <input type="submit" id="btn_send" name="btn_send" value="Subscribe" class="btn"/> 
  50. </div>
  51. </form>
  52. </div>
  53. </body>
  54. </html>



Subscribers Form w3workers
Subscribers Form

Step 4:
Download PHPMailer open source library

Download Link:
www.github.com/PHPMailer/PHPMailer

PHPMailer GitHub w3workers
PHPMailer GitHub
Click on "Clone or download" button for downloading PHPMailer-master.zip.

Extract PHPMailer-master.zip in "emailer" directory.

and rename folder "PHPMailer-master" to phpmailer.

Step 5:
Get Gmail SMTP Details you need to set in PHPMailer

Host:
smtp.gmail.com

Username:
Your Gmail Id example : w3workers@gmail.com

Password: 
Your Gmail Account Password

SMTPSecure:
tls

port
587

Now Login your GMail account and go to Following Link:

myaccount.google.com/intro/security?pli=1#connectedapps

Allow Less Secure app in Google account

click on "Connected apps & sites" and set "Allow less secure apps: ON".

Step 6:
Now Configure PHPMailer library in sendmail.php file.

Edit: sendmail.php

  1. <?php
  2. require 'phpmailer/PHPMailerAutoload.php';
  3. if(isset($_POST['txt_email'])){
  4. $from_email='w3workers@gmail.com';
  5. $from_name='Emailer';
  6. $to=$_POST['txt_email'];
  7. if (!filter_var($to, FILTER_VALIDATE_EMAIL) === false) {
  8.   $mail = new PHPMailer;
  9. $mail->isSMTP();                // Set mailer to use SMTP
  10. $mail->Host = 'smtp.gmail.com';  // Specify SMTP servers
  11. $mail->SMTPAuth = true;   // Enable SMTP auth
  12. $mail->Username = 'w3workers@gmail.com';  // Gmail Id
  13. $mail->Password = 'your_gmail_account_password';     
  14. $mail->SMTPSecure = 'tls';            // Enable TLS 
  15. $mail->Port =587 ;                         // TCP port  
  16. $mail->setFrom($from_email,$from_name);
  17. $mail->addAddress($to);     // Add a recipient             
  18. $mail->addReplyTo($from_email, 'Information');
  19. $mail->isHTML(true);         // Set email format to HTML
  20. $mail->Subject = 'Thanks For Subscribing';
  21. $mail->Body    = 'This is the HTML message body <b>in bold w3workers</b>';
  22. $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

  23. if(!$mail->send()) {
  24.     echo 'Message could not be sent.';
  25.     echo 'Mailer Error: ' . $mail->ErrorInfo;
  26. } else {
  27.     echo 'Thanks For Subscribing with us !';
  28. }
  29. } else {
  30.   echo("$to is not a valid email address");
  31. }
  32. }else{
  33. echo "Invalid Email";
  34. }

  35. ?>

So thats all done now your Gmail SMTP emailing Function Completed with PHPMailer API.

You can configure any SMTP service you want with PHPMailer Library and can create many type of Emailing Function in Your Website.


Thanks for reading if you have any question add in comments section.

Wednesday, 20 July 2016

List of php frameworks


PHP frameworks w3workers
List of PHP Frameworks
PHP frameworks are very helpful for PHP developers to create application in well organized architecture also frameworks provides predefined libraries and functions to make things easy.

When you have to work in team its very important for each member of team to follow some architecture so they can share files and merge files easily with the help of well organize files and directory in project.


Most of the PHP frameworks are based on MVC architecture.


So if you will develop project with some PHP framework which is based on MVC architecture in that case you can say that MVC architecture is Skeleton of your project.


MVC Architecture  

MVC Architecture w3workers
MVC Architecture


MVC stands for models, views and controllers. 


Models: models files are exist in models directory and these files used to write database queries so you can manage all your database queries into models files.


Views: views files are exist in views directory and these files used to write HTML content of your project so its easy to manage all

HTML files in one place.

Controllers: controllers are only way to access your project even you have to load some html page or you have to access some database query, all controllers files are exist in controllers directory.


So you can say frameworks are making things easy in development with files management, libraries.



List of php frameworks: 



Laravel


Laravel PHP frameworks w3workers
Laravel PHP framework


Download Link :  
www.laravel.com/docs/5.2#installing-laravel

Documentation Link :
www.laravel.com/docs

Symfony


Symfony PHP frameworks w3workers
Symfony PHP framework


Download Link : 
www.symfony.com/download

Documentation Link :
www.symfony.com/doc/current/index.html 

 CodeIgniter



Codeigniter PHP frameworks w3workers
Codeigniter PHP framework

Download Link : 

www.codeigniter.com/download

Documentation Link :
www.codeigniter.com/docs

 Yii 2



YII PHP frameworks w3workers
YII PHP framework

Download Link : 

www.yiiframework.com/download

Documentation Link :
www.yiiframework.com/doc

Phalcon



Phaicon PHP frameworks w3workers
Phaicon PHP framework

Download Link : 

phalconphp.com/en/download

Documentation Link :
docs.phalconphp.com/en/latest/index.html

 CakePHP



CakePHP Frameworks w3workers
CakePHP Framework
Download Link : 
www.cakephp.org

Documentation Link :
book.cakephp.org/3.0/en/index.html

 Zend Framework


ZEND PHP Frameworks w3workers
ZEND PHP Framework
Download Link : 
framework.zend.com/downloads

Documentation Link :
framework.zend.com/learn

 Slim

Slim PHP Frameworks w3workers
Slim PHP Framework

Download Link : 
www.slimframework.com

Documentation Link :
www.slimframework.com/docs

 FuelPHP



FuelPHP Framework
Download Link : 
www.fuelphp.com

Documentation Link :
www.fuelphp.com/docs

PHPixie



PHPixie Framework w3workers
PHPixie Framework
Download Link :
www.phpixie.com

Documentation Link :

www.phpixie.com/docs.html


Thanks for reading if you have any question add in comments section.

Sunday, 17 July 2016

List of database compatible with PHP



List of  database compatible with PHP w3workers
List of  database compatible with PHP

In dynamic websites database plays important role for storing, searching, modifying or extracting data.

various database magement systems available to help your website backend we will discuss some advanced and popular database which is fully compitable with php.

PHP is open source script so best combination with php is open source database like MySQL, MariaDB, PostgreSQL.


But other commercial database like oracle, MSSQL also supports PHP.



Short description of features which is supported by all database mention in list:


RDBMS: 

Relational database management system (RDBMS) is based on the relational model as introduced by E. F. Codd.
So any database management system(DBMS) based on relational model by E. F. Codd is supports standard RDBMS.

SQL compatibility: 

SQL is Structured Query Language standard for database queries.

Cache:

Most recently accessed data from database by application program stored in computer memory is called cache. 
cache is used to minimize the amount of physical disk I/O performed by the DBMS.

B-tree: 

database tables columns indexing method to provides fast access to data in searching.

Client/Server architecture:

client performed operations from PC,smartphone web browsers on server where database exist.

Cursor:

its like a pointer for certain rows selected by query and points each row to navigate.

Stored procedures: 

Stored procedure is advanced queries written with in database.

Triggers:

Automatically execute by server on certain operations like (insert,update,delete).

Unicode: 

supports various charsets like UTF8,UCS2. 

Foreign key constraints:

In relational database model foreign key (one or more columns) of a table refers to one and only existing primary key column into another table.

Transactions:

transaction means in database execute several operations in block and ensures that all operations completed successfully.

Administration tool: 

For administering database server like phpmyadmin.


Below is list of RDBMS which is compitable with php:


These all database engine supports features described above

Except SQlite.

because SQlite is RDBMS file system unlike other database in list.


MySQL:



MySQL the most popular open source database w3workers
MySQL the most popular open source database
MySQL still most popular database management system and first choice of beginners because easy tutorials, documentation and support available.

WebSite:

link: www.mysql.com

Documentation:

link: dev.mysql.com/doc

Licence:

Open Source

Administration tool: 

phpmyadmin

MariaDB:



MariaDB open source and faster alternative for MySQL

MariaDB is open source RDBMS faster than MySQL 

MariaDB is replacing MySQL database because supports mysqli driver so you can switch from MySQL to MariaDB anytime without any modification in your MySQL queries.

WebSite:

link: mariadb.org

Documentation:

link: mariadb.com/kb/en/mariadb/documentation

Licence:

Open Source

Administration tool: 

phpmyadmin


PostgreSQL:



PostgreSQL the worlds most advanced open source database w3workers
PostgreSQL the worlds most advanced open source database

PostgreSQL is worlds most advanced open source database management system if you planing to make new PHP application in that case you should use postgreSQL instead of MySQL or MariaDB.


WebSite:

link: www.postgresql.org

Documentation:

link: www.postgresql.org/docs

Licence:

Open Source

Administration tool:

phppgadmin

Oracle:

Oracle worlds most popular and advanced database management system w3workers
Oracle worlds most popular and advanced database management system
Oracle is worlds most advanced and secure database management system but it's not open source you have to buy commercial licence for oracle database. 

Oracle also own MySQL under oracle organization but MySQL still available opensource under oracle.


WebSite:

link: www.oracle.com/index.html

Documentation:

link: docs.oracle.com/en/database

Licence:

link: www.oracle.com/us/corporate/pricing/price-lists/index.html

Administration tool:

phporacleadmin


MSSQL:



Microsoft SQL server powerful dbms w3workers
Microsoft SQL server powerful dbms

Microsoft SQL server is also powerful database management system with commercial licence.


WebSite:

link: www.microsoft.com/en-in/server-cloud/products/sql-server

Documentation:

link: msdn.microsoft.com/en-us/library/ms130214.aspx

Licence:

link: www.microsoft.com/en-in/server-cloud/products/sql-server/purchasing.aspx


SQLite:

SQLite most widely deployed database engine w3workers
SQLite most widely deployed database engine


SQLite is an embedded SQL database engine. SQLite reads and writes directly to ordinary disk files.

Complete SQL database with multiple tables, indices, triggers, and views, is contained in a single disk file.  

SQLite is not as a replacement for MySQL,PostgreSQL,MariaDB etc but as a replacement for FileSystem like fopen().


WebSite:

link: www.sqlite.org

Documentation:

link: www.sqlite.org/docs.html

Licence:

Open Source


these database are fully supported php and having powerful features.




Thanks for reading if you have any question add in comments section.

Tuesday, 12 July 2016

Programming best practice guide for developers



programming best practice w3workers
Programming Best Practice 
Programming is the most interesting work for coders because you have so many challenges and creativity to do with your programming logic.

Programming work needed sharp brain and concentration to develop efficient code and many times you have to update your coding functions to keep your application free from bugs and optimized to run smoothly on allocated hardware/resources you provided to run that application.


If you are working on a complex and long term project then first step which is you have to keep in mind before starting work on programming how to manage your codes.


So here we will discuss how to manage large project and how to organize project files.


Every programmer having its own choice from largest group of programming language such as Java, ASP, PERL, RUBY, PHP etc and many more it’s your choice which language you want to use for your project.


Once you selected programming language you want to use for coding your project you can go for the next step.


In next step you have to plan architecture for your project to manage coding files so you can efficiently work together with your team.


Large application development needed team work and it’s very important to organize coding files to share with each member of your development team so they can able to work on same files and later can merge files.


Things to keep in mind for best practice coding and project management is describe below:



Select some good Framework:
  1. Frameworks means basic structure like skeleton.
  2. Frameworks is designed to organize your project files with some architecture and having predefined set of functions and libraries to make things easier.
  3. MVC architecture is most popular architecture used by latest frameworks.
  4. MVC stands for models,views and controllers. 
  5. Models used to write database queries and accessible from all controllers so you can manage all database queries in one place.
  6. Controllers to call functions, controllers is only way to access project based on mvc architecture.
  7. Views are HTML files for your projects.
Select IDE or SDK:
  1. Integrated Development Environment (IDE) is a software application consist editor, debugger, automation hints and tools to help coders for fixing errors quickly and can run project to see output.
  2. Software Development Kit is advance software tool for developing computer software or web application with the help of drag and drop components, emulator to run and check output for different devices, debuggers to fixing bugs.

Indentation:


Indentation term is used to provide structure and style to programs blocks. 

Example 1:

function nameNum()
{
      if(condition)
     {

     }
     else
    {

    }
}

Example 2:

function nameNum2(){
     if(condition){

           }else{

           }
}

its your choice what type of structure you want to use for your coding .


Commenting in codes:

Commenting in programming is readable hints to understand about that code by programmers. purpose of commenting to make source code easy to learn by other coders.


Inline-commenting:


// this is comment about variables initialization 

   var i=j=k=0;

Descriptive-blocks:

/**
   *@desc: description about that block of code 
   *@para: parameters purpose in function
   *@result: result return by function
*/

Example:


function addition($num1,$num2) {

/**
   *@desc: for adding two numbers 
   *@para: two parameter num1,num2 
   *@result: result return addition of parameters
*/
$addition = $num1+$num2;
return $addition;
}

These are some example of commenting in codes



  • Code grouping:


when you making a function having multiple blocks of code in that case you should use some lines gap between block.

For example:

  
function example(){
// comment about block1
     block1

// comment about block2

     block2

// comment about block3

     block3
}

We can see more detailed example


function calculation($num1,$num2){

     // variables initialization
     $sum=$multiply=$division=$subtraction=0;
      
     // calculation all operation
     $sum = $num1+$num2;
     $multiply = $num1*$num2;
     $division = $num1/$num2;
     $subtraction = $num1-$num2;

      // result array

      $arr=array( 
                         'sum'=>$sum, 
                         'multiply'=>$multiply,
                         'division=>$division,
                         'subtraction=>$subtraction 
                       );

       // return final result

       return $arr
}

in this example we have multiple blocks in same function to make function readable we need some spacing between blocks and small comments just above that block for quick learning of that block.



Functions naming convection:
when you writing function its very important to keep follow rules and same format for all functions in your code.

For example


Format1: In this example we have camelcase to keep after each word end and new word start with capital later.


function calculateValue(){

}

Format2: In this example we have underscore after each word.


function calculate_value(){

}

So try to follow such rules for all your function and do not make mistake like some function created with Format1 and some created with Format2 make sure which format you want to use and use same format in all functions in your code.


Variable naming convection:
some basic rules of variable declaration are:

allow capital letters (A-Z)

allow small letter (a-z)
allow underscore(_)
allow digits (0-9) only after letters or underscore

some variable combinations:


$num1 (Right)

$num_1 (Right)
$Num_ (Right)
$_num(Right)
$_Num(Right)
$2_Num (Wrong can not start variable with digits)

Do not use all capital letters to define like $NUM,$LENGTH

because in programming capital letters used to define constants. 

its very important to keep make variable readable avoid making variable like $x, $y, $z ,$a , $b, $c, $var1, $var2 or $value1, $value2.


Try to give some meaningful name to variable you using in functions.


Example:  


function area($val1,$val2){

       $val3=$val1*$val2;
}

in this function we have three variables val1,val2,val3 and we can't understand anything with this until we not read function what is the purpose of these variable.


So we can create some meaningful variable instead of val1=>length

, val2=>width, val3=>rectArea

function area($length,$width){

$rectArea=$length*$width;
}

Avoid Repetitions:
Avoid repetition of codes like if you have some section of code
which is you need in multiple function in that case you can create a global function to use it in all place.

Avoid Nesting:

Do not make complex nested loops or if else condition try to keep function smaller and easier to read and manage.

You can create multiple functions instead of creating larger function.


just pass argument to the function and call from place where you need it and avoid nesting and complexity. 



Capitalize SQL Special words:

SQL allows both small and capital letters to create database queries but in best practice try to use SQL special words in capital letters.

For example  


SELECT users FROM table_user;


So these are few things which should keep in mind to make your code readable and easier to maintain.



Thanks for reading if you have any question add in comments section.