I had a specific requirement to generate a CSV file and send the same as an attachment in a mail, without saving it at any physical location.
I am listing here the way to do it.
<?php $csv = ''; $_columns = array( 'N', 'SKU', 'Name' ); $data = array(); // prepare CSV header… foreach ($_columns as $column) { $data[] = '"'.$column.'"'; } $csv .= implode(',', $data)."\n"; $productData = array(); $productData[] = array('sku'=>'S1','name'=>'PN1'); $productData[] = array('sku'=>'S2','name'=>'PN2'); $i = 1; foreach($productData as $k => $v) { $tmp = array(); $tmp[] = '"'.$i.'"'; $tmp[] = '"'.$v['sku'].'"'; $tmp[] = '"'.$v['name'].'"'; $csv .= implode(',',$tmp)."\n"; $i++; } //end if(!empty($csv)) { $template = Mage::getStoreConfig('mail/template/path',Mage::app()->getStore()->getId()); $translate = Mage::getSingleton('core/translate'); $translate->setTranslateInline(false); $emails = 'abc@abc.com,bcd@bcd.com'; $mailTemplate = Mage::getModel('core/email_template'); $name = 'Admin'; //admin name $from = 'general'; $content = $csv; $html = "Mail content goes here"; $subject = 'Mail subject goes here'.date('Y-m-d'); foreach (explode(',', $emails) as $_email) { $attachment = $mailTemplate->getMail()->createAttachment($content); //add the created file as an attachment $attachment->filename = 'FileName_'.date('Y-m-d').'.csv'; try{ $mailTemplate->setDesignConfig(array('area' => 'backend')) ->sendTransactional($template, $from, trim($_email), $name, array( 'mail_content' => $html, 'mail_subject' => $subject ) ); }catch(Exception $e) { echo $e->getMessage(); } } } ?>
For further read
Mail in Magento
Add attachment to order email in Magento