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, 1 September 2017

Unix Shell Scripting Tutorial - page 6

Unix Shell Scripting Tutorial - page 6

Question: How can we search in file using Regular expression?
We use grep command.
grep "search string" filename.txt



Question: How to search a string in file?
grep "search string" filename.txt

search string is text searched in filename.txt. By Default its case sensitive.


Question: How to search a case-insensitive string in file?
use -i to search case insensitive.
grep -i "search string" filename.txt



Question: How to search a string in mutiple file?
separate the filename by space.
grep -i "search string" filename.txt filename2.txt filename3.txt 



Question: How to search a string in file using regular expression? Use regex character for example .* used to search all
grep -i "textname .*" filename.txt 

It will return all after the textname.


Question: What are the basic Regex characters?
  1. ? The preceding item is optional and matched at most once.
  2. * matched zero or more times.
  3. + matched one or more times.
  4. {n} matched exactly n times.
  5. {n,} matched n or more times.
  6. {,m} matched at most m times.
  7. {n,m} matched at least n times, but not more than m times.



Question: How to search a words in file?
Use -w to search words.
grep -iw "textname" filename.txt 

It will not search textnameA.


Question: How to search and return next line?
grep -A 3 -i "textname" filename.txt

It will search and return next 3 lines after search.
You can use -B to return Before 3 lines.


Question: How to search recursively in folder? Use -r to search recursive.
grep -r "textname" 



It will search in current folder and sub-folder.


Question: How to find number of matches ? Use -c to return number of matches.
grep -c "textname" fiename.txt

It will return number of matches in number.


Question: What are the Performance Tools in unix?
  1. nice/renice: Runs a program with modified scheduling priority.
  2. netstat: Prints network connections, routing tables and interface statistics.
  3. time: give resource usage
  4. uptime: This is System Load Average.
  5. ps: Current process list
  6. vmstat: Virtual Memory stats
  7. top: Top Task List



Question: What is syslog?
Syslog is a way for network devices to send event messages to a server. Also known as Syslog server.
SysLog is stored in /etc/syslogd OR /etc/syslog.
syslog.conf have following configuration.
*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice           /var/log/messages
lpr.info                     /var/log/lpr.log
mail.*                       /var/log/mail.log
ftp.*                        /var/log/ftp.log
mark.*                       /dev/console



Question: What is command for logging?
logger [-i] [-f file] [-p priority] [-t tag] [message]