we can send custom email with attachment in magneto
i am sending email acc to date of orders. this email contain 2 csv file of both pending and invoiced orders
create a email template system->Transactional Emails
i’e
Template Name :- Daily Order Reports Template Subject :- {{var subject}} Template Content Hi <p> Please check attached csv file of {{var subject}} </p>
save it u will get email Template id once u save this email
create one file in root folder of project paste this below code and save it
<?php define('MAGENTO', realpath(dirname(__FILE__))); require_once MAGENTO . '/../app/Mage.php'; Mage::app(); $storename='default'; $string="sku,alu,name,qty,mrp,sold price,size,color,final price\n"; function add_dateAction($givendate,$day=0,$mth=0,$yr=0,$hr=0,$min=0,$sec=0) { $cd = strtotime($givendate); $newdate = date('Y-m-d H:i:s', mktime(0+$hr, 0+$min, 0+$sec, date('m',$cd)+$mth, date('d',$cd)+$day, date('Y',$cd)+$yr)); return $newdate; } $from=add_dateAction(now(),-1,0,0); $report_date=date("d_M_y", mktime(0, 0, 0, date("m"),date("d")-1,date("Y"))); echo 'Report On '.$report_date.'<br />'; $to= add_dateAction(now(),-1,0,0,23,59,59); $orderCollection = Mage::getResourceModel('sales/order_collection'); $orderCollection->addFieldToFilter('status', array('eq'=>'pending'))->addAttributeToSelect('entity_id'); $orderCollection->getSelect() ->where("main_table.created_at BETWEEN '".$from."' AND '".$to."'"); // $orderCollection->printlogquery(true); exit; $i=0; foreach($orderCollection->getItems() as $order){ $orderModel = Mage::getModel('sales/order'); $orderModel->load($order['entity_id']); $i++; //echo $orderModel->getIncrementId().', '.$orderModel->getPayment()->getMethod().'<br />'; foreach ($orderModel->getAllItems() as $_product) { $_loadedprod=Mage::getModel('catalog/product')->load($_product->getData('product_id')); $size=Mage::getModel('catalog/product') ->load($_loadedprod->getId()) ->getAttributeText('size'); $color=Mage::getModel('catalog/product') ->load($_loadedprod->getId()) ->getAttributeText('color'); $string .= $_product->getSku() . ',' .$_loadedprod->getData('alu') . ',' . $_product->getData('name') . ',' .$_product->getData('qty_ordered'). ',' .$_product->getData('orginal_mrp'). ',' .$_product->getData('original_price'). ',' .$size . ',' .$color . ',' .$_product->getData('base_row_total_incl_tax') . "\n"; } } echo 'Total Pending orders:-'.$i.'<br />'; //Invoice Order Report $string2="sku,alu,name,qty,mrp,sold price,size,color,final price\n"; $orderCollection = Mage::getResourceModel('sales/order_invoice_collection')->addAttributeToSelect('entity_id'); //$orderCollection->addFieldToFilter('status', array('eq'=>'pending')); $orderCollection->getSelect() ->where("main_table.created_at BETWEEN '".$from."' AND '".$to."'"); // $orderCollection->printlogquery(true); exit; $i=0; foreach($orderCollection->getItems() as $order){ $i++; //echo $orderModel->getIncrementId().', '.$orderModel->getPayment()->getMethod().'<br />'; foreach ($order->getAllItems() as $_product) { $orderitem=Mage::getModel('sales/order_item')->load($_product->getData('order_item_id')); $_loadedprod=Mage::getModel('catalog/product')->load($_product->getData('product_id')); $size=Mage::getModel('catalog/product') ->load($_loadedprod->getId()) ->getAttributeText('size'); $color=Mage::getModel('catalog/product') ->load($_loadedprod->getId()) ->getAttributeText('color'); $string2 .= $_product->getSku() . ',' .$_loadedprod->getData('alu') . ',' . $_product->getData('name') . ',' .$_product->getQty(). ',' .$orderitem->getOrginalMrp(). ',' .$_product->getPrice(). ',' .$size . ',' .$color . ',' .$_product->getData('base_row_total_incl_tax') . "\n"; } } echo 'Total inviced orders:-'.$i.'<br />'; $templateId =6; // Set sender information $senderName = 'report '; // Mage::getStoreConfig('trans_email/ident_support/name'); $senderEmail = Mage::getStoreConfig('trans_email/ident_support/email'); //$size = $this->getRequest()->getParam('size'); $sender = array('name' => $senderName, 'email' => $senderEmail); // Set recepient information $recepientEmail = 'youremail@doine.com'; //Mage::getStoreConfig('trans_email/ident_support/email'); //email from support admin side $recepientName = 'pradeep'; // Get Store ID $store = Mage::app()->getStore()->getId(); // Set variables that can be used in email template $vars = array('subject' => 'Daily Order Report 0n '.date("M d Y", mktime(0, 0, 0, date("m"),date("d")-1,date("Y")))); $translate = Mage::getSingleton('core/translate'); $transactionalEmail = Mage::getModel('core/email_template')->setDesignConfig(array('area'=>'frontend', 'store'=>1)); $transactionalEmail->getMail()->createAttachment($string,'text/UTF-8')->filename = 'pending_orders_'.$report_date.'.csv'; $transactionalEmail->getMail()->createAttachment($string2,'text/UTF-8')->filename = 'invoiced_orders_'.$report_date.'.csv'; if($transactionalEmail->sendTransactional($templateId, $sender, $recepientEmail, "Admin", $vars)) { echo 'mail sent'; //$this->getResponse()->setRedirect($url); }else{ echo 'mail not sent'; } ?>
No comments:
Post a Comment