Wednesday, February 10, 2010

Send Mail of Dynamic generate table in php

If we need to send some data comes from database in table format via email to other email automatically to using PHP script like newsletter.


Code:-

<?php
$senderheaders  = "From: dept@xyz.com\r\n";
  $senderheaders .= "Content-type: text/html\r\n";
  
  $subject="Dept Category Item Detail";
  $sendId='test@xyz.com';
  $textTosend='
  
  
  
  <table width="700px" align="center">
  <tr>
  <td colspan="3" align="center"  class="title" height="30px"><strong>Category Item Detail </strong></td>
  </tr>
  
  
  <tr>
  <td >
  <strong>Item Name</strong> </td>
  <td >
  <strong>Item Cost</strong> </td>
  <td >
  <strong>Item Qty.</strong> </td>
  
  
  </tr>';
$selItem="select * from item where cate_id=$Id";
  $itemRs=mysql_query($selItem,$dbc);
  while($itemRow=mysql_fetch_array($itemRs)){
  $itemDetail=itemDetail($itemRow["product_id"],$dbc);
  
  $textTosend.='<tr>
  <td >
  '.$itemDetail["item_name"].' </td>
  <td >
  '.$itemDetail["price_unit"].' </td>
  <td >
  '.$itemRow["quantity"].' </td>
  
  </tr>
  ';
  
  }
$textTosend.='</table><br>
    
    
  ';
if(mail($sendId,$subject,$textTosend,$senderheaders)){
    
    
  echo '<div class="success">Echo Mail Send  SuccessFully</div>';
  }
  ?>


4 comments:

  1. wheres the function? because I always got an error on itemDetail()

    ReplyDelete
  2. itemDetail() is a function that is use for get some information from other table u no need to use that function remove it and use variable directly

    ReplyDelete
  3. Hi Omkar I already used the method but it will only send 1 result to the email and the other details wont send. for example 1 banana, 1 mango and in the email only 1 banana will appear.

    ReplyDelete
  4. Hi thank you so much Omkar I already got it....

    ReplyDelete