This topic is locked
[SOLVED]

Managing eMail Template Files

5/9/2022 10:20:08 PM
PHPRunner General questions
A
Ace Drummond author

I want to maintain multiple email templates.

I created a db table 'email_templates' with fields as follow:

seq int <primary key, auto increment
message_id mediumtext
message_subject varchar(50)
message_text mediumtext
on_date datetime

I am using a rich text editor to edit the message_subject field.

I want to create a text file for each email template and include message_subject & message_text as two records within the .txt file.

I've written a simple after update event to be used on add & edit that would place the email template .txt file in the 'output' folder within the email_templates folder.

On my test system I fully specify the location of the file I wanted to create (this works).

My question is how do I structure the $filename correctly so that it works both on my local webserver (WAMP) and when I upload to my hosting service server.

I guess then the second question is how do I address the saved /email_templates/$filename to use it elsewhere.

Here is my event coding I am trying to test............................................................................................................

// Get fields from the table email_templates

$filename = $values["message_id"];
$subject = $values["message_subject"];
$msg_text = $values["message_text"];

print 'File Name: '.$filename.'
'.'Subject: '.$subject.'
'.'Text: '.$msg_text.'
';

// Construct Filename for eMail Template

$filename = "c:/wamp64/projects/rmac_email/output/email_templates/".$filename.".txt";

print 'Actual File Name: '.$filename.'
';

// Write output file with 2 records (Subject & Text)

$template_file = fopen($filename, "w") or die("Unable to open '$filename'!");
fwrite($template_file, $subject.PHP_EOL);
fwrite($template_file, $msg_text.PHP_EOL);
fclose($template_file);

Thank you//

A
Ace Drummond author 5/10/2022

I found a solution that will work on my web hosting server by using php realpath to find the path to a file in the directory where I wanted to output.

It starts out line this: '/home/public_html/' then add directory information and filename.ext

Still trying to figure out how I can make this easily work for both testing on my windows PC and webserver?

admin 5/11/2022

The recommended approach is to use a relative path to the template file like email_templates\template.txt. This way it will work without any changes on both localhost and on your live server. Since PHP will require a full path to the file you can use one of the many available ways to convert a relative path to the absolute one. One of the options is to use PHPRunner's getabspath() function.

getabspath("email_templates\template.txt")

A
Ace Drummond author 5/12/2022

Problem solved - thank you!