MCrosbie Posted January 14, 2008 Share Posted January 14, 2008 I have a function for sending email using email templates. This is for transactional emails. Inside these email templates are variables, e.g. {var customer.id}. This is the function: // TRANSACTIONAL EMAILS SEND function transaction_email($tag,$var = array(),$sendto){ $res_template = mysql_query("Select * from DM_email_templates where tag='".$tag."'"); $row['template'] = mysql_fetch_array($res_template); $message = $row['template']['content']; $subject = $row['template']['subject']; foreach ($var as $tags => $data) { $message = eregi_replace("{" . $tags . "}", $data, $message); $subject = eregi_replace("{" . $tags . "}", $data, $subject); } $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: '.DM_site('website_name').' <'.DM_site('admin_email').'>' . "\r\n"; mail($sendto['name']."<".$sendto['email'].">",$subject,$message, $headers); } This is a call to the function for it to send: $sendto['name'] = $form['billed_to']; $sendto['email'] = $row['custid']['email']; transaction_email('new_invoice',array( "var invoice.total" => $calc[0], "var invoice.id" => $invoice_id, "var customer.id" => $row['custid']['id'], "var customer.company" => $calc[4]['company'], "var customer.fname" => $calc[4]['fname'], "var customer.lname" => $calc[4]['lname'], "var customer.address" => $calc[4]['address'], "var customer.address2" => $calc[4]['address2'], "var customer.city" => $calc[4]['city'], "var customer.state" => $calc[4]['state'], "var customer.country" => $calc[9], "var customer.telephone" => $calc[4]['telephone'], "var customer.email" => $calc[4]['email'], "var invoice.created" => $calc[5]['created'], "var invoice.due" => $calc[5]['due'], "var invoice.paid" => $calc[5]['paid'], "var invoice.statement" => $calc[10], "var site.name" => DM_SITE('website_name'), "var site.addr" => "http://".$_ENV['SERVER_NAME']."" ),$sendto); Now the problem I have, and it has only happened with the above example, is that both var invoice.total and var invoice.id don't come through. var invoice.total simply replaces the tag with nothing. I have checked and the value ($calc[0], lets say it is 28 for now), and it is passed all the way till eregi_replace, and even afterwards, but for some reason it doesn't replace it with the value, rather it replaces it with " " - nothing. var invoice.id is a similar situation, but instead of replacing it with nothing it replaces it with a "?". I can think of no logical reason for why it would be doing this. I have tried to change the names of the variable and change the values, but nothing seems to change the outcome. Please help! Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/ Share on other sites More sharing options...
toplay Posted January 14, 2008 Share Posted January 14, 2008 So, I assume your templates are something like this: This is Invoice # {var invoice.id} for customer id # {var customer.id} Well, your template might be wrong. Show us your 'new_invoice' template, and the values of $calc[0] and $invoice_id. Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439349 Share on other sites More sharing options...
MCrosbie Posted January 14, 2008 Author Share Posted January 14, 2008 This is the template here: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" /> </head> <body style="margin: 0px; height: 100%; min-height: 100%; background-color: rgb(255, 255, 255); font-family: Geneva,Verdana,Arial,Helvetica; font-size: 14px; color: rgb(51, 51, 51); font-stretch: semi-expanded;"> <table width="750" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff" align="center"> <tbody> <tr> <td width="750" height="150" colspan="2"> <p style="border: 1px solid rgb(190, 188, 183); padding: 13px 18px; background: rgb(248, 247, 245) none repeat scroll 0% 50%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;">A new invoice has been billed to your account.</p> <img width="750" height="150" alt="9th Design . web design . print media . corporate identity . logos . web hosting . web domains . creating opportunities" src="http://www.9thdesign.co.nz/dimensions/skins/default/media/sales/invoice_header.gif" /></td> </tr> <tr> <td width="50%" valign="top" height="139" style="font-family: Geneva,Verdana,Arial,Helvetica; font-size: 14px; color: rgb(51, 51, 51);"> <p><b>Billed To:<br /> </b>{var customer.id}<br /> {var customer.company}<br /> {var customer.fname} {var customer.lname}<br /> {var customer.address}<br /> {var customer.address2}<br /> {var customer.city}<br /> {var customer.state}<br /> {var customer.country}<br /> <br /> {var customer.email} <strong><br /> <br /> </strong></p> </td> <td width="50%" valign="top" style="font-family: Geneva,Verdana,Arial,Helvetica; font-size: 14px; color: rgb(51, 51, 51);"> <p><strong>Invoice number: </strong>{var invoice.id}<br /> <strong>Date issued:</strong> {var invoice.created}<br /> <strong>Date due: </strong>{var invoice.due}<br /> <strong>Date paid: </strong>{var invoice.paid}<br /> <strong>Invoice Total: </strong>${var invoice.total}</p> </td> </tr> <tr> <td colspan="2"> <p align="left">{var invoice.statement}</p> <p align="left"><span style="font-weight: bolder;">Late payment policy</span><br /> All accounts must be paid by the 30th of the month due. If not paid by the 30th interest will be applied at a rate of and will be carried forward.</p> <p align="left">If you have questions concerning this invoice, please email: <br /> <br /> </p> </td> </tr> <tr> <td colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tbody> <tr style="border-bottom: 0px solid rgb(204, 204, 204);"> <td colspan="3" style="border-bottom: 1px solid rgb(204, 204, 204);"><strong>Payment options </strong></td> </tr> <tr style="border-bottom: 0px solid rgb(204, 204, 204);"> <td><strong>Cheques:</strong></td> <td><strong>PayPal:</strong></td> <td><strong>Bank transfer </strong></td> </tr> <tr style="border-bottom: 0px solid rgb(204, 204, 204);"> <td>Make payable to </td> <td>Make payments to </td> <td>Account name: 9th Design<br /> Bank number: <br /> Bank: <br /> Branch: <br /> Reference:</td> </tr> </tbody> </table> </td> </tr> <tr> <td height="50" colspan="2" style="border-bottom: 1px solid rgb(204, 204, 204);"> <div align="center"> <p style="font-family: Geneva,Verdana,Arial,Helvetica; font-size: 12px; color: rgb(105, 105, 105);">Copyright 2007-9, 9th Design. All rights reserved.</p> </div> </td> </tr> </tbody> </table> </body> </html> The values are as follows: $calc[0] = 28 $invoice_id = 101067 Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439351 Share on other sites More sharing options...
toplay Posted January 15, 2008 Share Posted January 15, 2008 Type cast the values as string or format the number in the case of the total. Example: transaction_email('new_invoice',array( "var invoice.total" => number_format($calc[0], 2, '.', ','), "var invoice.id" => (string) $invoice_id, . . . Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439364 Share on other sites More sharing options...
MCrosbie Posted January 15, 2008 Author Share Posted January 15, 2008 Worked like a charm! Can you explain the difference that that has made and why it worked? Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439366 Share on other sites More sharing options...
toplay Posted January 15, 2008 Share Posted January 15, 2008 Not really sure since I don't work with ereg_xxx functions too much. If you use preg_replace() instead you won't have that problem. This: $message = preg_replace("/{" . $tags . "}/i", $data, $message); will work fine with what you already have: transaction_email('new_invoice',array( "var invoice.total" => $calc[0], "var invoice.id" => $invoice_id, . . . Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439371 Share on other sites More sharing options...
MCrosbie Posted January 15, 2008 Author Share Posted January 15, 2008 Brilliant. Thanks for all your help, it's greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/86019-solved-eregi_replace-replaces-needle-with/#findComment-439374 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.