Page 1 of 1
Dynamic Emails stored in a table.
Posted: Sun Sep 05, 2004 12:10 am
by hawleyjr
I have multiple HTML emails that I send out to users. Each email contains dynamic information in the body; such as name, email address and url(s).
Here is an example of how I hard code the PHP currently:
(I removed most of the copy to make the HTML more readible)
Code: Select all
<?php
$email_message = '
<style>
/* body properties */
body {
background-color: #006699;
font-size : 100%; color : black;
}
/* Font settings for page elements */
body, ul, td, th, p, h1, h2, h3, h4, small, .small,
.tableTitle, .tableExtras {
font-family: verdana, arial, helvetica, sans-serif;
color: black;
font-size: 12px;
}
/* body border */
.bodyline { background-color: #cccccc; border: 1px #98AAB1 solid; }
</style>
<table width="100%" height="100%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr>
<td valign="top" class="bodyline"><div align="center">
<br> <br> <br> <br> <br> <br> <br> <br> Hello, '.$userName.' <br><br><br> Click
<a href="'.$dynamicURL.'" target="_blank">
Here</a> to Complete Registration.</div></td> </tr></table>';
?>
I can easily call this via a function but I would like to store the html in a table. My question is how do I store it in a table but still be able to insert the needed variables? For instance,
Code: Select all
<?php
"SELECT email from email_table where email_id = 1";
//GET RESULTS
//INSERT VALUES INTO returned email HTML
//????????
//send mail
mail();
?>
Posted: Sun Sep 05, 2004 12:38 am
by feyd
template engine principles can apply here well:
html data in database
Code: Select all
<style>
/* body properties */
body {
background-color: #006699;
font-size : 100%; color : black;
}
/* Font settings for page elements */
body, ul, td, th, p, h1, h2, h3, h4, small, .small,
.tableTitle, .tableExtras {
font-family: verdana, arial, helvetica, sans-serif;
color: black;
font-size: 12px;
}
/* body border */
.bodyline { background-color: #cccccc; border: 1px #98AAB1 solid; }
</style>
<table width="100%" height="100%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr>
<td valign="top" class="bodyline"><div align="center">
<br> <br> <br> <br> <br> <br> <br> <br> Hello, {S_USERNAME} <br><br><br> Click
<a href="{U_COMPLETE_REGISTRATION}" target="_blank">
Here</a> to Complete Registration.</div></td> </tr></table>
basic code to "insert" the data:
Code: Select all
$pairs = array(
'S_USERNAME'=>'Fooie',
'U_COMPLETE_REGISTRATION'=>'http://foo.com/whatever',
);
foreach($pairs as $key => $val) $targets = ( empty($targets) ? '' : $targets . '|' ) . preg_quote( $key, '#' );
$replacements = array_values($pairs);
$email_html = preg_replace('#\{(' . $targets . ')\}#',$replacements,$html_data);
Posted: Sun Sep 05, 2004 12:41 am
by hawleyjr
Feyd, all I can say is YOU ROCK!
Posted: Fri Sep 17, 2004 7:47 pm
by hawleyjr
I am trying to get the code Feyd got me to work and I'm getting the following error:
Code: Select all
Warning: preg_replace(): Parameter mismatch, pattern is a string while replacement in an array.
What I'm trying to do:
Code: Select all
<?php
$email_body = '
<table width="100%" height="100%" border="0" align="center" cellpadding="5" cellspacing="0"> <tr> <td valign="top" class="bodyline"><div align="center">
<p><br>
<br>{A_COMPANY_NAME}
</p>
<p> Account Created.<br>
<br>
<br>
Click <a href="http://www.example.com/agentcstmrsite/users/verify.php?id={A_VERIFICATION_ID}" target="_blank">
Here</a> to Complete Registration.</p>
</div></td> </tr></table>';
$pairs = array('A_COMPANY_NAME'=>'some company',
'A_VERIFICATION_ID'=>'id');
foreach($pairs as $key => $val) {
$targets = ( empty($targets) ? '' : $targets . '|' ) . preg_quote( $key, '#' );
$replacements = array_values($pairs);
$email_html = preg_replace('#\{(' . $targets . ')\}#',$replacements,$email_body);
}
?>
Any help is greatly appreciated.
Posted: Fri Sep 17, 2004 7:54 pm
by feyd

sorry about that.. needs something like:
Code: Select all
$email_html = preg_replace('#\{(' . $targets . ')\}#e','$replacementsї''\\1'']',$email_body);
Posted: Fri Sep 17, 2004 8:18 pm
by hawleyjr
Thanks Feyd.
There was an 'e' in
Code: Select all
preg_replace('#\{(' . $targets . ')\}#e'
that was making the preg_replace do nothing. After I took that out my html looked like this:
Code: Select all
.
..
...
<br>$replacementsї'A_COMPANY_NAME']
.
..
...
Click <a href="http://www.example.com/agentcstmrsite/users/verify.php?id=$replacementsї'A_VERIFICATION_ID']" target="_blank">
how do I show the variable values and not the variable name?
Posted: Fri Sep 17, 2004 9:14 pm
by feyd
it's supposed to have an e.
anyway, here's a different way:
Code: Select all
$targets = '';
foreach($pairs as $key => $val)
{
$targets .= ( empty($targets) ? '' : '|' ) . preg_quote( $key, '#' );
}
while(preg_match('#\{(' . $targets . ')\}#', $email_body,$match))
$email_body = str_replace($matchї0],$pairsї$matchї1]],$email_body);
Posted: Fri Sep 17, 2004 9:23 pm
by hawleyjr
Thanks Feyd I really appreciate it. Regular expressions are definitely my weak spot. I’ve been looking at different tutorials. This has just been harder for me to grasp then everything else.