Monday 29 January 2018

phpmailer gmail - PHP MAILER setting to send email through Gmail

PHP MAILER setting to send email through Gmail

Step 1:
Download a recent version of PHPMailer.

Step 1:
Use Following code to send an email from Gmail Account.

 
// Import PHPMailer classes into the global namespace
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

//Load composer's autoloader
require 'vendor/autoload.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = 'user@example.com';                 // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 587;                                    // TCP port to connect to

    //Email Sender
    $mail->setFrom('from@example.com', 'Mailer');

    //Recipients
    $mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
    $mail->addAddress('ellen@example.com');               // Name is optional
    $mail->addReplyTo('info@example.com', 'Information');
    $mail->addCC('cc@example.com');
    $mail->addBCC('bcc@example.com');

    //Attachments (IF there is attachment then only include)
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML (true) 
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

If you get similar to following Error
Please log in via your web browser and then try again.
5.7.14 Please log in via your web browser and then try again. 5.7.14 Learn more at 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 fr13sm3613053pdb.81

If Yes, 
whether you have "turned on 2-Step Verification" OR Google need Verification that you want to use your gmail as SMTP credential.

You can fix this in couple of minutes using below link:
https://support.google.com/mail/answer/78754

If any issue, please comment below in comment box:



Friday 12 January 2018

Zend Framework 2 Routes

Zend Framework 2 Routes Tutorial

Question: What is routing?
Routing is the act of matching a request to a given controller.



Question: What are different type of routing?
Following are different type of routing
  1. Literal literal route is one that matches a specific string. In this one rule match to one
  2. Segment A segmented route is used for whenever your url is supposed to contain variable parameters. parameters are used to identify certain objects within your application.



Question: Give example of Literal route?
For Example:
http://domain.com/about-me
http://domain.com/contact-us
http://domain.com/terms

    'router' => array(
        'routes' => array(
        'staticpages1' => array(
             'type' => 'literal',
             'options' => array(
                 'route'    => '/about-me',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'aboutme',
                 ), 
             ),
         ),            
        'staticpages2' => array(
             'type' => 'literal',
             'options' => array( 
                 'route'    => '/contactus',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'contactus',
                 ),
                
             ),
         ),            
        'staticpages3' => array(
             'type' => 'literal',
             'options' => array(
                 'route'    => '/terms',
                 'defaults' => array(
                     'controller' => 'Album\Controller\Album',
                     'action'     => 'terms',
                 ),
             ),
         ), 
        ),
    ),



Question: Give example of Segment route?
For Example: http://domain.com/album
http://domain.com/album/add
http://domain.com/album/edit/1
http://domain.com/album/delete/1


    'router' => array(
        'routes' => array(
            'album' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/album[/:action][/:id]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        'id' => '[0-9]+',
                    ),
                    'defaults' => array(
                        'controller' => 'Album\Controller\Album',
                        'action' => 'index',
                    ),
                ),
            ),
        ),
    ),



Question: What is database abstraction layer?
A database abstraction layer is a simplified "representation of a database" in the form of a written description or a diagram.

There are 3 abstraction layers:
  1. User model
  2. Logical Model
  3. Physical Model



Question: Why we should use database abstraction?
Once after the application development with MySQL, there is requirement to change the internal database from MySQL to SQL.
IF we have developed application with database abstraction, then we need to change the interface only.
IF we have not developed application with database abstraction, then we need to update re-write the code.