1

I have implemented send email in single image file as attachment. but now i want to send multiple images as attchment in email.How can i acheive that? if anyone can help me.

I have tried below code for single image send in email and that is working fine.

I have used a request quote folder as requestquote in media directory for saving for uploaded images. There is an array of custom variables to be passed to transactional email.

$uploadfilename = '';

if( !empty($_FILES["rfloorplanattachment"]["name"])  )
{

    $image_ext = end(explode('.',$_FILES["rfloorplanattachment"]["name"]));
    $allowed_ext =  array('gif','png' ,'jpg','jpeg','pdf','doc','docx','rtf','odt');

    $uploadfilename = md5(substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, rand(1,100).rand(1,100))).str_replace(" ","_",$_FILES["rfloorplanattachment"]["name"]); 
    $source_upl         = $_FILES["rfloorplanattachment"]["tmp_name"];
    $target_path_upl = Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename;  
    if(in_array($image_ext ,$allowed_ext ) ) {
        @move_uploaded_file($source_upl, $target_path_upl);
    }
}


$senderName = Mage::getStoreConfig('trans_email/ident_general/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');

$templateId = 3;
$sender = Array('name' => $senderName,'email' => $senderEmail);


$requestquotesvars = array(
            'firmname'     =>  $customer->getFirstname()
        );


$emaiName = 'Request Quote Firms';

$storeId = Mage::app()->getStore()->getId();

$translate = Mage::getSingleton('core/translate');
$transactionalEmail = Mage::getModel('core/email_template');
if(file_exists(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename) )
{
$transactionalEmail->getMail()
                ->createAttachment(
        file_get_contents(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename),
        Zend_Mime::TYPE_OCTETSTREAM,
        Zend_Mime::DISPOSITION_ATTACHMENT,
        Zend_Mime::ENCODING_BASE64,
        basename($uploadfilename)
    );
}
$transactionalEmail->sendTransactional($templateId, $sender, $companymail, $emailName, $requestquotesvars, $storeId);
$translate->setTranslateInline(true);

   unlink(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename);
1

Please check the below code..

 if( !empty($_FILES["customerimageuploads"]["name"][0]))
        {
            $total = count($_FILES['customerimageuploads']['name']);
            for( $i=0 ; $i < $total ; $i++ ) {
                $image_ext           = end(explode('.',$_FILES["customerimageuploads"]["name"][$i]));
                $allowed_ext         = array('gif','png' ,'jpg','jpeg','pdf','doc','docx','rtf','odt');
                $uploadfilename      = $_FILES["customerimageuploads"]["name"][$i]; 
                $source_upl          = $_FILES["customerimageuploads"]["tmp_name"][$i];
                $target_path_upl     = Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename;  

                if(in_array($image_ext ,$allowed_ext ) ) {
                    @move_uploaded_file($source_upl, $target_path_upl);
                }
            }
        }    

$templateId = 3;

 /* FOR EMAIL LOOP */
        for( $i=0 ; $i < $total ; $i++ ) {
                $uploadfilename      = $_FILES["customerimageuploads"]["name"][$i]; 
                if(file_exists(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename) )
                {
                    $transactionalEmail->getMail()
                                ->createAttachment(
                        file_get_contents(Mage::getBaseDir('media').DS.'requestquote'.DS.$uploadfilename),
                        Zend_Mime::TYPE_OCTETSTREAM,
                        Zend_Mime::DISPOSITION_ATTACHMENT,
                        Zend_Mime::ENCODING_BASE64,
                        basename($uploadfilename)
                    );
                }
        }
            $translate->setTranslateInline(true);
            $transactionalEmail->setTemplateSubject($senderSubject); 
            /*  Admin email */
            $transactionalEmail->sendTransactional($templateId, $sender, $toEmail, $toName, $params, $storeId);
  • It works as expected. Thanks :) happy coding...!!! – Balwant Singh Oct 7 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.