Jump to content

Archived

This topic is now archived and is closed to further replies.

Agreaves

Help with smarty fetch email program

Recommended Posts

In my code below whenever smarty fetches the template file it doesnt pass the values to the variables upon sending the email, but if i display it on the webpage it works perfectly. what am i not doing or doing wrong?

if(isset($_POST['send']))
{
$smarty->assign('row',$data);
$name = $_POST['name'];
$address = $_POST['address1'];
$address2 = $_POST['address2'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$to = 'Andrew Greaves <a_greaves@live.com>';
$subject = 'Invoice';
$headers = array( "From: $name <$email>",'MIME-Version: 1.0','Content-type: text/html; charset=ISO-8859-1');
$smarty->assign('name',$name);
$smarty->assign('address',$address);
$smarty->assign('address2',$address2);
$smarty->assign('telephone',$telephone);
$body = $smarty->fetch('email_body.html');

mail($to, $subject, $body, implode("\r\n",$headers));
$message = '<p><b>Your invoice was sent successfully</b></p>';
}

Share this post


Link to post
Share on other sites

1. Is the file actually called .html, or is it .tpl?

2. Can you post the template please?

Share this post


Link to post
Share on other sites

Its called .tpl, the table is displayed in the email but empty, not even the pictures show up and the background images I set in the table.

<html>
<style type="text/css">
#title {
width: 400px;
font-family: Verdana;
font-size: 14px;
font-weight: bold;
color: #000;
text-decoration: underline;
}
#hdg_bkgrnd {
background-image: url(../../images/tbl_bkgrd.jpg);
}
#pic_bdr {
border: 1px solid #CCC;
vertical-align: middle;
text-align: center;
width: 950px;
display: table;
}
.details_box {
display: table-cell;
text-align: center;
vertical-aign: middle;
position: relative;
padding: 0px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-image: url(../../images/tbl_bkgrd.jpg);
}
#total {
font-family: Verdana;
font-size: 12px;
font-weight: bold;
color: #000;
}
.main_bdr {
border: 0.5px solid #CCCCCC;
}
#cols_bkgrnd {
background-color: #E5E5E5;
background-image: url(../images/row_bkgrnd.jpg);
}
#grnd_total {
font-size: 14px;
font-weight: bold;
color: #F00;
text-decoration: underline;
}
</style>
<body>
<table width="950" border="0">
  <tr>
    <td width="171" height="40" align="center" class="details_box"><strong>BILL TO</strong></td>
    <td width="424"> </td>
    <td width="82"> </td>
    <td width="82"> </td>
    <td width="34"> </td>
    <td width="171" height="40" class="details_box"><strong>INVOICE</strong></td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td align="center">{$date}</td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
  <tr>
    <td align="left"> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
  </tr>
</table>
<table width="950" border="0" align="center" id="pic_bdr">
  <tr id="hdg_bkgrnd">
    <td width="160" align="center" height="40"><strong>Thumbnail</strong></td>
    <td width="160" align="center" height="40"><strong>Id</strong></td>
    <td width="160" align="center" height="40"><strong>Name</strong></td>
    <td width="160" align="center" height="40"><strong>Quantity</strong></td>
    <td width="160" align="center" height="40"><strong>Price</strong></td>
    <td width="160" align="center" height="40"><strong>Amount</strong></td>
  </tr>
  {foreach from=$row item=item key=key}
  <tr height="55">   
    <td width="160" align="center" ><img name="" src="{$item.Thumbnail}" alt=""></td>
    <td width="160" align="center" id="cols_bkgrnd">#{$item.Id}</td>
    <td width="160" align="center" id="cols_bkgrnd">{$item.Name}</td>
    <td width="160" align="center" id="cols_bkgrnd">{$item.quantity}</td>
    <td width="160" align="center" id="cols_bkgrnd">${$item.Price}</td>
    <td width="160" align="center" id="cols_bkgrnd">${$item.amount}</td>
  </tr>
  {/foreach}
  <tr height="40" id="hdg_bkgrnd">
    <td align="center" > </td>
    <td align="center" id="cols_bkgrnd2"> </td>
    <td align="center" id="cols_bkgrnd2"> </td>
    <td align="center" id="cols_bkgrnd2"> </td>
    <td align="right" id="cols_bkgrnd2"><strong id="total">TOTAL</strong></td>
    <td align="center" id="cols_bkgrnd2"><strong id="grnd_total">${$item.total}</strong></td>
  </tr>
</table>
</body>
</html>

Share this post


Link to post
Share on other sites

I did a test by sending the fetched template upon compiling it and the information showed in the email except the picture, but once I use post from the form it erases all the session variables before sending the email leaving me with a blank table.

Share this post


Link to post
Share on other sites
but once I use post from the form it erases all the session variables

 

What form? What session variables?

 

We cannot help you with tail-end symptoms without having all the code that reproduces those symptoms. There can be a half-dozen different things your code could be doing that could cause any one symptom, but without seeing all the relevant code, it is not possible to narrow them down to the one thing that is causing the problem.

Share this post


Link to post
Share on other sites

Here is the code for the form

 

echo "<form method=\"post\" name=\"body\" action=\"index.php\">";	
echo "<table align=\"center\" id=\"tbl_bdr\" width=\"950\" border=\"0\" cellspacing=\"0\" cellpadding=\"10\">
<tr align=\"left\">
<td align=\"left\"><b>Name:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"name\"/></td>
<td align=\"left\"><b>Email:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"email\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Address:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address1\"/></td>
<td align=\"left\"><b>Address(ext):</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"address2\"/></td>
  	</tr>
<tr align=\"left\">
<td align=\"left\"><b>Telephone:</b></td>
<td><input id=\"txt_box\" type=\"text\" name=\"telephone\"/></td>
  	</tr>
</table>";
echo "<table id =\"cart_bdr\" align =\"center\" border=\"0\" width=\"950\">";//format the cart using a HTML table
echo "<tr id=\"hdg_bkgrnd\" height=\"40\"><td width=\"160\" align=\"center\"><b>Thumbnail</b></td><td width=\"160\" align=\"center\"><b>Id</b></td><td width=\"160\" align=\"center\"><b>Name</b></td><td width=\"160\" align=\"center\"><b>Quantity</b></td><td width=\"160\" align=\"center\"><b>Price</b></td><td width=\"160\" align=\"center\"><b>Amount</b></td></tr>";
$total = 0;		
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach ($_SESSION['cart'] as $product_id => $quantity) {

    //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
    $sql = sprintf("SELECT Id, Name, Price, Pix, Thumbnail FROM products WHERE Id = %d;", $product_id);

    $result = mysqli_query($con,$sql);

$row = mysqli_fetch_assoc($result);

$items['quantity'] = $quantity;

$Id = $row['Id'];
$Name = $row['Name'];
$Price = $row['Price'];
$Pix = $row['Pix'];
$Thumbnail = $row['Thumbnail'];

$amount['amount'] = number_format($quantity*$Price,2);

    $line_cost = number_format( $Price * $quantity,2); //work out the line cost

$total = number_format($total + $line_cost,2);

$grand_total['total'] = $total;

$data[] = array_merge($row,$items,$amount,$grand_total);

    echo "<tr height=\"55\">";
echo "<td align='center'><div id='thumb_bdr'><img src=\"$Thumbnail\"/></div></td>";
echo "<td align=\"center\" id=\"cols_bkgrnd\"><div name=\"id\">#$Id</div></td>";
    //show this information in table cells
    echo "<td align=\"center\" id=\"cols_bkgrnd\"><div id='items'>$Name</div></td>";
    //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current 			product
    echo "<td align=\"center\" id=\"cols_bkgrnd\">X $quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\"><img border='0' src='../images/down_arrow.gif' width='9' height='21' alt='Descrease' /> </a><a href=\"$_SERVER[php_SELF]?action=add&id=$product_id\"><img border='0' src='../images/up_arrow.gif' width='9' height='21' alt='Increase' /></a></td>";
echo "<td align=\"center\" id=\"cols_bkgrnd\"><div id='items'>$$Price</div></td>";
    echo "<td align=\"center\" id=\"cols_bkgrnd\"><div id='items'>$$line_cost</div></td>";
    echo "</tr>";
}

//show the total
    echo "<tr id=\"hdg_bkgrnd\" height=\"40\">";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
    echo "<td align='right'><div><b>TOTAL</b></div></td>";
    echo "<td align='center'><b id='grnd_total'>$$total</b></td>";
    echo "</tr>";
echo "</table>";
// Create table to hold submit and emapty cart buttons 
echo "<table width=\"950\" align=\"center\">";
    echo "<tr>"; 
   	echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\"></td>";
echo "<td width=\"237\" align=\"right\"><input id=\"submit_btn\" value=\"Send\" name=\"send\" type=\"submit\"/></td>"; 
    echo "<td width=\"237\"><input id=\"cart_btn\" type=\"button\" value=\"Empty\" onclick=\"window.location.href='$_SERVER[php_SELF]?action=empty'\" /></td>";
    echo "</tr>";
    echo "</table>";
    echo "<b id='ship'>SHIPPING AND HANDLING NOT INCLUDED</b>";
    echo "</form>";

 

// Email invoice program
if(isset($_POST['send']))
{
$smarty->assign('row',$data);
$email = $_POST['email'];
$body = $smarty->fetch('email_body.tpl');
$html = $body;
$crlf = "\n";
$hdrs = array(
              'From'    => 'Andrew Greaves <moregal63@hotmail.com>',
              'Subject' => 'Invoice'
              );

$mime = new Mail_mime(array('eol' => $crlf));

$mime->setHTMLBody($html);

$body = $mime->get();
$hdrs = $mime->headers($hdrs);

$mail =& Mail::factory('mail');
$mail->send('a_greaves@live.com', $hdrs, $body);
}

Share this post


Link to post
Share on other sites

The only things I can tell from the fragments of code are -

 

1) You don't have a session_start() statement, so any $_SESSION variables won't exist outside of the page request they were set on.

 

2) If you mean that the $data array doesn't contain any data, when you use it in - $smarty->assign('row',$data);, that's because web servers are stateless and the $data array you are setting in the form code only exists on that page and for the http request for that page.

 

3) You should not be executing a query inside of a loop. You should get all the product id's at once (see array_keys) and form a mysql IN(id,id,id,....) term for the WHERE clause to get all the product information in one query.. You would then simply loop over the result set from that query to output the cart contents. As you are looping over the result from the query, you would get the quantity by using the current product_id from the row you are looping over as an index into the session cart array.

Share this post


Link to post
Share on other sites

Also I do have a session_start on the cart page. If I do a smarty->display the template prints to the screen. I am looking into adjusting the code for the cart to remove the query from the loop.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.