Showing posts with label Zend Framework. Show all posts
Showing posts with label Zend Framework. Show all posts

Saturday 2 September 2017

How to attach a CSV file to email - Zend

How to attach a CSV file to email - Zend

To send CSV in email, you must have following.
  1. Receiver email address
  2. CSV file that want to send as attachment.
  3. SMTP Credentials like SMTP USRNAME, SMTP PASSWORD, SMTP HOST, SMTP PORT



Script Code
    $toEmail='toemailaddress@domain.com'; //Receiver email address
    $subject='Email with attachment';//Email subject
    $htmlEmail='Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut eget nibh consectetur, mattis mauris vitae, aliquam orci. Nunc pretium tristique metus eget pharetra. Ut condimentum purus in ex varius, quis bibendum ex venenatis. Praesent leo lacus, feugiat vel neque ac, finibus ultricies velit. Nunc venenatis sem eu ullamcorper interdum. Fusce a pulvinar risus, quis viverra nulla. Donec tempus ex congue mauris elementum, eget fermentum orci dapibus. Proin erat lacus, lacinia eu volutpat quis, sagittis suscipit erat. Maecenas nulla enim, tempor ac urna id, tincidunt consectetur metus. Maecenas tincidunt, tortor quis sodales rutrum, purus mi accumsan libero, id maximus purus leo id ante. In posuere erat ipsum, sed mattis est placerat eget. Phasellus consectetur diam augue, sed luctus libero consequat quis. Praesent vel mauris nec ligula malesuada faucibus. Suspendisse ultrices nunc velit, in ultricies nibh tempus id. Integer quis nisi ipsum.'
            . 'This is html email.';


    //Email Configuration
    $config = array(
        'auth' => 'login',
        'ssl' => 'tls',
        'port' => '587',
        'username' => 'MAIL_USERNAME',
        'password' => 'MAIL_PASSWORD'
    );
    $tr = new Zend_Mail_Transport_Smtp('MAIL_HOST', $config);
    Zend_Mail::setDefaultTransport($tr);
    $mail = new Zend_Mail('UTF-8');
    $mail->setFrom(MAIL_SENDER_EMAIL, MAIL_SENDER_NAME);
    $mail->addTo($toEmail);
    $mail->setSubject($subject);
    $mail->setBodyHtml($htmlEmail);


    //Attachment Email
    //Get the contents of csv file
    $csvContent=  file_get_contents('http://example.com/projects/export-csv.csv');
    $at = new Zend_Mime_Part($csvContent);
    $at->type        = 'text/csv';
    $at->disposition = Zend_Mime::DISPOSITION_INLINE;
    $at->encoding    = Zend_Mime::ENCODING_BASE64;
    $at->filename    = 'csv-file.csv'; 

    //Attach the attachment in Email
    $mail->addAttachment($at);

    //Send Email
    $mail->send(); 




Question: How to send image as attachment in Email?
Following are attachment code.
$myImage=  file_get_contents('http://example.com/images/powered-by-georama-dark.png');
$at = new Zend_Mime_Part($myImage);
$at->type        = 'image/gif';
$at->disposition = Zend_Mime::DISPOSITION_INLINE;
$at->encoding    = Zend_Mime::ENCODING_BASE64;
$at->filename    = 'imagename.gif'; 

$mail->addAttachment($at);



Friday 27 May 2016

Upload Image from URL to S3 in Zend Framework

Upload Image from URL to S3 in Zend Framework


$imageURL = 'https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEidv97qmJwUu4M0TAG5vrIuRLFN1YpSwJnDp6huR_eRyN3OeV2tKg_7hyK9adAH3ex6bAys6LZur073vkHRl3833KjuuoVwlaq3b1XfvqGUBKJo3TZZh54KkqgtAVGv9yQdSLkkCK47L-G2/s1600/Upload+image+from+URL+to+S3+in+Zend+Framework.png';
$extension = 'png';

$myAwsKey = 'AKIAP3APTHAC2DQLGJA'; //S3 AWS Key
$myAwsSecretKey = 'YJiKlVFERFuF/sadfzgS2jgj6z/Qhjkhsh'; //S3 Secret Key
$s3Media = 's3mediafolder'; //S3 Folder path

$s3 = new Zend_Service_Amazon_S3($myAwsKey, $myAwsSecretKey);
$uploadTo = 'clientProfileImages/thumb/205_test_push.png';

try {
    $s3->putFile($imageURL, "$s3Media/$uploadTo", array(
        Zend_Service_Amazon_S3::S3_ACL_HEADER => Zend_Service_Amazon_S3::S3_ACL_PUBLIC_READ,
        'x-amz-storage-class' => 'REDUCED_REDUNDANCY',
        'ContentType' => $extension
    ));
} catch (Exception $e) {
    echo $e->getMessage();
    die;
}
echo 'Image Uploaded Successfully in S3';

Wednesday 2 March 2016

How to use view helper in zend framework?

How to use view helper in zend framework?

Question: What is zend view helper?
View Helpers are common file(s) which is available in every view file.
Most of the time we kept helpers file in views/helpers/

Following the simple following steps to use view helper.
  1. Open configs\application.ini, Add Following line
    resources.view.helperPath.Application_View_Helper = APPLICATION_PATH "/views/helpers"
  2. Create General.php file in application\views\helpers (Might be need to create helpers folder)
  3. Add Following code in General.php
    class Zend_View_Helper_General extends Zend_View_Helper_Abstract {
        function General(){
            return $this;
        }
         function twice($a)
        {
            return $a * 2;
        }
        
        function thrice($a)
        {
            return $a * 3;
        }    
    }
  4. Test Following in any view files
    echo  $this->General()->twice(2);//4
    echo  $this->General()->thrice(2);//6
    echo  $this->General()->twice(3);//6
    echo  $this->General()->thrice(3);//9
    
  5. Done


If above is not working in other modules like admin, then copy/paste following function in Bootstrap.php

 protected function _initViewHelpers() {
        $this->bootstrap('layout');
        $layout = $this->getResource("layout");
        $view = $layout->getView();
        $view->addHelperPath(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'helpers', "Application_View_Helper");
    }





Sunday 24 January 2016

Zend Framework Remember me login Form

Zend Framework Remember me login Form

Question: What is Remember Me meaning in Login Form?
When user close the browser, he logout automatically from the website due to session terminate.
When he come back on website, he need to re-login.

If we use remember me functionality, then user does not logout when he exit from website.


Question: How to use remember me in Zend Framework?
Before writing the data into session, Add following code.
Zend_Session::rememberMe(10*86400);  //Remember me for 10 days



Question: Can we use remember me with Zend_Auth?
Yes, See Example:
$result = $auth->authenticate($adapter);    
if ($result->isValid()) {
 $loginDetail = $adapter->getResultRowObject();
 $user = $adapter->getResultRowObject();   
 Zend_Session::rememberMe(7*86400); //7 days
 $namespace = new Zend_Session_Namespace('Zend_Auth'); 
 $auth->getStorage()->write($user); 
}



Question: How to keep user login even after 2 hour of inactivity?
You need to set the expiration time,
$namespace = new Zend_Session_Namespace('Zend_Auth');
$namespace->setExpirationSeconds(7*86400); // Session will expire after 7 days  



Question: What is forgetMe?
This function complements rememberMe() by writing a session cookie that has a lifetime.
Zend_Session::forgetMe(); 



Question: What is expireSessionCookie? Why It is used?
This is method of Zend_Session used for delete the session cookie in client side.

Thursday 24 December 2015

How to install Zend Framework 2 in windows


How to install Zend Framework 2 in windows

  1. Add PHP.exe Path to Your Windows Path Variable.
    Means we need to add php.exe's path to windows path variable So that you can execute php commands.
    (My php.exe Path: E:\wamp\bin\php\php5.4.3)
    Not understand OR any Doubt 
  2. Make sure you have >=PHP5.4 Version.
  3. Now download Zend Framework 2.3.2 From https://github.com/zendframework/ZendSkeletonApplication/releases/tag/release-2.3.2
  4. Unzip this zipped file .
  5. Rename ZendSkeletonApplication to zf2.
  6. Copy this zf2 folder to E:\wamp\www
  7. Login to command prompt & Go to www folder of wamp (Path: E:\wamp\www\zf2)
  8. Now download composer with following command.
    php -r "readfile('https://getcomposer.org/installer');" | php
    If the above fails, enable php_openssl.dll in php.ini & Restart wamp Server.
  9. Now execute following command to install the composer
    php composer.phar install
  10. Apache Setup  (Path: E:\wamp\bin\apache\apache2.2.22\conf\extra)
    <virtualhost> DocumentRoot "D:\wamp\www\zf2\public" ServerName zf2.loc <directory public="" wamp="" www="" zf2=""> Options FollowSymLinks MultiViews AllowOverride All Order allow,deny Allow from all </directory> </virtualhost>
  11. Append following line in host File (Path:C:\Windows\System32\drivers\etc)
    127.0.0.1       zf2.loc
  12. ReStart your wamp Server.
  13. http://zf2.loc in Web Browser