Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:50:37+00:00 2026-05-26T13:50:37+00:00

I have an email template that is inside a PHP script. I am using

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:50:37+00:00Added an answer on May 26, 2026 at 1:50 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template for an email that I've put in a localized strings
I have an email template that I would like to launch from within the
I have created an email template in Ax, using the %varname% as a placeholder
I have an HTML transactional email that I am sending out. The email template
I have created an email template, I use PHP to send the email, but
I have started creating a Perl package that contains a default email template. The
I have a email template, that has French copy. If I load this email
I'm programming in PHP and would like to create web pages which have email
I have an e-mail template that I use to send out account emails and
I'm using Zend_View to template text email content. However, I've come across one problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.