Magento Coding Solutions

programaticaly send email template magento

Step1 )  create new email template
template Id=8;

<div style="background: #F6F6F6; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; margin: 0; padding: 0;">
<table style="width: 100%;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="padding: 20px 0 20px 0;" align="center" valign="top">
<table style="border: 1px solid #e0e0e0; width: 650px;" border="0" cellspacing="0" cellpadding="10" bgcolor="#FFFFFF"><!-- [ header starts here] -->
<tbody>
<tr>
<td valign="top"><a href="{{store url=""}}"><img style="margin-bottom: 10px;" src="{{var logo_url}}" alt="{{var logo_alt}}" border="0" /></a></td>
</tr>
<!-- [ middle starts here] -->
<tr>
<td valign="top">
<h1 style="font-size: 22px; font-weight: normal; line-height: 22px; margin: 0 0 11px 0;">Hello</h1>
<p style="font-size: 12px; line-height: 16px; margin: 0;">Client has submitted order,order id : #{{var order_id}} . You can check the status of your order by <a style="color: #1e7ec8;" href="{{store url="customer/account/"}}">logging into your account</a>.</p>
<p style="font-size: 12px; line-height: 16px; margin: 0;">Your order details is below.</p>
</td>
</tr>
<tr>
<td>{{var order_items}}</td>
</tr>
<tr>
<td style="background: #EAEAEA; text-align: center;" align="center" bgcolor="#EAEAEA"><center>
<p style="font-size: 12px; margin: 0;">Thank you</p>
</center></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>

Step 2) create a php file
emailSend.php in root

and past below code and set Order_id = 60 like
<?php
include_once 'app/Mage.php';
Mage::app();
$order_id=62;
$order = Mage::getModel('sales/order')->load($order_id);
$items = $order->getAllItems();
$emailSendData=array();
    foreach($items as $item){      

        if($item->getParentItemId() == '' || $item->getParentItemId() == null){

             $product_id = $item->getProductId();
$product=$item->getProduct();
//echo $product->getName();
//echo $product->getProduct_boutique();
//echo $product->getAttributeText('product_boutique');
$_boutique = Mage::getModel('boutiques/boutiques')->load($product->getProduct_boutique());
$emailBoutique=$_boutique->getEmail_de_contact();
$toname=$_boutique->getNom_du_responsable();
$emailSendData[$emailBoutique]['toname']=$toname;
$emailSendData[$emailBoutique]['orderId']=$order_id;

$templateId ='Fav Email';
$emailTemplate = Mage::getModel('core/email_template')->loadByCode($templateId);
  $item_price = $item->getPrice();
  $item_price_row = $item->getRow_total();
  $qty_ordered = $item->getQty_ordered();


       $itemInfoitem='<tbody bgcolor="#F6F6F6">
        <tr>
<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc">
        <strong style="font-size:11px">'.$product->getName().'</strong></td>
<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc">'.$product->getSku().'</td>
<td align="left" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc">'.$product->getAttributeText('product_boutique').'</td>
<td align="center" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc">'.(int)$qty_ordered.'</td>
<td align="right" valign="top" style="font-size:11px;padding:3px 9px;border-bottom:1px dotted #cccccc">
        <span>'.$item_price_row.'</span> </td>
</tr>
</tbody>';  
$emailSendData[$emailBoutique]['order_item'][]=$itemInfoitem;

}




    }



$itemInfo='<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #eaeaea"><thead>
<tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Article</th>
<th align="left" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Réf.</th>
<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Nom du magasin</th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Qté</th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Sous-total</th>
        </tr>
</thead>';

foreach($emailSendData as $email=>$emailVal){
//print_r($emailVal);
$itemInfo='<table cellspacing="0" cellpadding="0" border="0" width="650" style="border:1px solid #eaeaea"><thead>
<tr>
            <th align="left" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Article</th>
<th align="left" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Réf.</th>
<th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Nom du magasin</th>
            <th align="center" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Qté</th>
            <th align="right" bgcolor="#EAEAEA" style="font-size:13px;padding:3px 9px">Sous-total</th>
        </tr>
</thead>';

$emailto=$email;
$toName=$emailVal['toname'];
$orderId=$emailVal['orderId'];

for($i=0;$i<count($emailVal['order_item']);$i++){
$itemInfo.=$emailVal['order_item'][$i];
}
$itemInfo.='</table>';
//echo $itemInfo;
sendMail($emailto,$toName,$orderId,base64_encode($itemInfo));
unset($itemInfo);

}

   

  function sendMail($toemail,$toName,$orderId,$orderItems){
  $orderItems=base64_decode($orderItems);
   //$cusotmer = Mage::getModel('customer/customer')->load($uid);
   $to_email = $toemail;
   $to_name = $toName;
  // $first_name =$cusotmer->getFirstname();
   //$last_name =$cusotmer->getLastname();
   $from_email = Mage::getStoreConfig('trans_email/ident_general/email');
   $from_name = Mage::getStoreConfig('trans_email/ident_general/name');
 
    $mailTemplate = Mage::getModel('core/email_template');
    $translate  = Mage::getSingleton('core/translate');
    $templateId = 8;
    $template_collection =  $mailTemplate->load($templateId);                    $template_data = $template_collection->getData();
   // print_r($template_data);
   if(!empty($template_data))
                    {
                        $templateId = $template_data['template_id'];
                        $mailSubject = $template_data['template_subject'];                        
                        $sender = array('name'  => $from_name,
                                        'email' => $from_email);
     
                        $vars = array('order_id'=>$orderId,'order_items'=>$orderItems);              
                        $storeId = '1';
                        $model = $mailTemplate->setReplyTo($sender['email'])->setTemplateSubject($mailSubject);
                        $email = $to_email;
                        $name = $to_name;                                          
                        $model->sendTransactional($templateId, $sender, $email, $name, $vars, $storeId);                  
                        if (!$mailTemplate->getSentSuccess()) {
                                throw new Exception();
                        }
                        $translate->setTranslateInline(true);
                    }
   
  }
?>

No comments:

Post a Comment