I am using a MySQL database to hold email templates. When I call upon the templates PHP fills in the variables and then sends the email. This all works wonderfully except the formatting is horrible. Is there a way to keep the original template format intact?
Thank you for your assistance. Sorry for the long posting.
Here is the revelant code:
template_actions
Code:
<?php
/*
this file contains fuctions for use with email templates
bool template_fetch($key, &$subject, &$body);
str template_fill_vars($string_in, $variables);
usage:
include($unix_root . 'libs/email/template_actions');
if(template_fetch('hosp_progress', $subject, $body)) {
$tempdata[job_id] = '123';
$body = template_fill_vars($body, $tempdata);
}
*/
/
** Ensure that these functions aren't included twice
/
$included_flag = 'INCLUDE_' . basename(FILE);
if(defined($included_flag))
//if this module has been included already, then the
//constant will be defined. Note the unusual use of
//the return statement here. Control will return to
//the file that included it.
return;
define($included_flag, TRUE);
FUNCTION template_fetch($id, &$subject, &$body) {
if(!($temp_q = mysql_query("SELECT subject, body FROM mail_template WHERE id='$id';", $GLOBALS['vet_sqlid']))) {
vet_log(FILE . ':' . LINE, mysql_error(), 9);
$result = 0;
} else {
list($subject, $body) = mysql_fetch_row($temp_q);
$result = 1;
}
return $result;
}
FUNCTION template_fill_vars($text, $variables) {
$num_var = sizeof($variables);
$var_keys = array_keys($variables);
for($cnt = 0; $cnt < $num_var; $cnt++) {
$var_name = $var_keys[$cnt];
$var_data = $variables[$var_name];
$text = ereg_replace("\$$var_name", $var_data, $text);
}
return $text;
}
?>
The code that fetches the email
Code:
<script language="php">
include("/usr/home/vet/web_data/libs/log.php3");
include("/usr/home/vet/web_data/libs/email/template_actions");
$vet_last = $_POST['name'];
if(!vet_num_errors())
{
if(!template_fetch('doc_bid_update', $doc_notify_subject, $doc_notify_body))
{
vet_error("Error fetching doc_bid_update template.");
} else {
if(strstr($email, '@'))
{
$doc_notify_subject = template_fill_vars($doc_notify_subject, array('id' => "$bid_id"));
$doc_notify_body = template_fill_vars($doc_notify_body, array('id' => "$bid_id", 'doc_last_name' => $vet_last, 'areacode' => $areacode));
$doc_sent = mail($email, $doc_notify_subject, $doc_notify_body, "Wrom: JJPHSCRTNHGSWZIDREXC
}
}
}
if(!vet_num_errors())
{
echo "<br> <font size=\"5\" color=\"#0000dd\">
Your update email has been sent to the doctor.</font>";
}
</script>
Here is how the email is received:
Dr. Tester: There is a message waiting for you at www.......com. This job communication memo is regarding your bid# 8723, for work at a hospital located in the 818 area code. Here's how to access this message: 1. Login to your online account at www.......com. 2. On the left side of your screen (beneath the veterinary caduceus) are the navigation links. Find the heading called Bids and click on ‘List’ 3. Next, click on the corresponding bid #:8723 . 4. Find the hospital’s reply in the Job Communication Memos. 5. Add your own comments to the message thread. Here’s how: a. Click on ‘New Memo for Hospital’. b. Type your message then click on save. That’s it. Leave us a job communication memo, email, or call us with any questions.