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);



1 comment :

bouncing balls said...

Yes, the information I was looking for. Thank you
nodejs quiz

Post a Comment