user547794
user547794

Reputation: 14511

Set PHP to be include file

I have an email template that is inside a PHP script. I am using PHPmailer and the message that is sent is stored in a variable called $msg.

How can I set $msg to be the email template include file? I'm hoping there is a way to do this dynamically with an include file so that I don't have to store 20 copies of the template in different scripts.

Upvotes: 0

Views: 130

Answers (6)

Tadeck
Tadeck

Reputation: 137320

You have multiple approaches, three of which I will mention below.

Solution no. 1 - simple inclusion

First of all, if you include a file like in the following example:

include('myfile.php');

its code will be executed exactly as if it was pasted instead of the above mentioned line. Thus if you will add value declaration in myfile.php, the variable will be available just after include() function call (mentioned above).

So, basically, you can do something like that in myfile.php file:

<?php

$msg = 'my template';

and use $msg variable anywhere after include() call in the main file (unless the variable has been overwritten).

Solution no. 2 - inclusion with use of return

For use in some cases, eg. when you only include files containing some values you want to use (the main purpose of the file is to give some value to be processed), you may wish to use the following example. It should not interfere with your current variables (if used correctly).

In the main file:

$msg = include('myfile.php');

and in the myfile.php file:

<?php

return 'my template';

This way your file works only by returning specific variable and does not break the naming convention from your other files. Also you are free to call the variable the way you like without the need to change the configuration file's content.

Solution no. 3 - output buffering

In some cases (messy coding, HTML etc.) you may be required to use this solution. The following code catches everything emitted by the file and saves it into variable $msg, then turns the buffering off.

ob_start();
include('myfile.php');
$msg = ob_end_flush();

This way, if you have eg. echo calls, or some static HTML, within the included file, it will not be written "as is" and could be processed further (eg. some tokens from your template may be replaced with the actual data you want to use).

So, basically, in this case you can even write in myfile.php something like:

<?php

echo 'my template';

or even:

my template

(just a text, not PHP code).

Upvotes: 3

Ghost-Man
Ghost-Man

Reputation: 2187

If you want to get the contents of the template file into the $msg variable you can do the following:

<?php
 $msg= get_include_contents('email_template_file.php');

 function get_include_contents($filename) {
 if (is_file($filename)) {
    ob_start();
    include $filename;
    return ob_get_clean();
 }
 return false;
 }

 ?>

Now your $msg variable has the text of your email. If you have some generic terms like Username, phone-no etc you want to be different for different users. Store them in tokens like {username}, {phone-number} in your template file. Once you get the contents in $msg variable, you can use the below:

<?php str_replace('{username}',$username,$msg); ?>

to change the values dynamically for each user you want to send mail.

Upvotes: 0

jli
jli

Reputation: 6623

You can either save it into a raw text file and load it with file_get_contents, or save a string with the text in it in another php file and include it.

Example:

Template.php:

<?php
$globalMessage = "template string goes here";
?>

Every other file:

<?php
include 'Template.php';
echo $globalMessage; // has access
?>

Upvotes: 0

Michael Mior
Michael Mior

Reputation: 28752

You can have $msg = "…"; in a PHP script and include this where necessary. However, if it's just plain text, you may want to consider saving it as a .txt file and using file_get_contents.

Upvotes: 0

lc2817
lc2817

Reputation: 3742

You can write in your template file:

$msg = """ the message
bla bla bla""";

Then when you include the file from another php module you can access to this variable.

Upvotes: 0

Blender
Blender

Reputation: 298126

Not sure if I am understanding you correctly, but why can't you just do this?

include_once('filename.php');

Upvotes: 3

Related Questions