Dynamic Emails stored in a table.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Dynamic Emails stored in a table.

Post 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();
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

template engine principles can apply here well:
html data in database

Code: Select all

&lt;style&gt;
/* body properties */
body &#123;
   background-color: #006699;
   font-size : 100%; color : black;
&#125;
/* Font settings for page elements */
body, ul, td, th, p, h1, h2, h3, h4, small, .small,
.tableTitle, .tableExtras &#123;
    font-family: verdana, arial, helvetica, sans-serif;
   color: black;
   font-size: 12px;
&#125;
/* body border */
.bodyline   &#123; background-color: #cccccc; border: 1px #98AAB1 solid; &#125;
&lt;/style&gt;
&lt;table width="100%" height="100%" border="0" align="center" cellpadding="5" cellspacing="0"&gt; &lt;tr&gt; 
&lt;td valign="top" class="bodyline"&gt;&lt;div align="center"&gt;
&lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;br&gt; Hello, &#123;S_USERNAME&#125; &lt;br&gt;&lt;br&gt;&lt;br&gt; Click
&lt;a href="&#123;U_COMPLETE_REGISTRATION&#125;" target="_blank"&gt;
Here&lt;/a&gt; to Complete Registration.&lt;/div&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/table&gt;
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);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Feyd, all I can say is YOU ROCK!
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

:oops: sorry about that.. needs something like:

Code: Select all

$email_html = preg_replace('#\{(' . $targets . ')\}#e','$replacements&#1111;''\\1'']',$email_body);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Thanks Feyd.

There was an 'e' in

Code: Select all

preg_replace('#\&#123;(' . $targets . ')\&#125;#e'
that was making the preg_replace do nothing. After I took that out my html looked like this:

Code: Select all

.
..
...
    <br>$replacements&#1111;'A_COMPANY_NAME']
.
..
...
Click <a href="http://www.example.com/agentcstmrsite/users/verify.php?id=$replacements&#1111;'A_VERIFICATION_ID']" target="_blank">
how do I show the variable values and not the variable name?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's supposed to have an e.

anyway, here's a different way:

Code: Select all

$targets = '';

foreach($pairs as $key =&gt; $val)
{
    $targets .= ( empty($targets) ? '' : '|' ) . preg_quote( $key, '#' );
}

while(preg_match('#\{(' . $targets . ')\}#', $email_body,$match))
    $email_body = str_replace($match&#1111;0],$pairs&#1111;$match&#1111;1]],$email_body);
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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.
Post Reply