Solved

how to show php variables in html email

Posted on 2016-09-24
4
35 Views
Last Modified: 2016-09-24
I am using php mailer and I can send html emails where the email is coming from a separate php file. Example:

 $m->Body = file_get_contents('basic-email-template/registration.php');

Open in new window


On the page where the user inputs their details I have:

 $_SESSION['firstname'] = clean_user_input($_POST['user_firstname']);

Open in new window


Then in the actual email received I have:

<h3>Hi, <?php echo $_SESSION['firstname'];?> !</h3>

But, the email is just showing Hi,          and no first name is displayed.

It's obviously meant to show the name from the session variable but doesn't.
0
Comment
Question by:Black Sulfur
  • 2
  • 2
4 Comments
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41813684
in your file that become the email body, you can create a custom tag (or anything to be marked that need to be replaced) in it, and when necessary, just replace it with the value you wanted.

like in registration.php, try define:
<h3>Hi, <SESSION[firstname]></h3>

Open in new window

then in your php, just do a replace:

$body = file_get_contents('basic-email-template/registration.php');
$body = str_replace("<SESSION[firstname]>", $_SESSION['firstname'], $body);
$m->Body = $body;

Open in new window

0
 

Author Comment

by:Black Sulfur
ID: 41813692
That worked, thanks! Is it necessary to unset the session?
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41813695
>>Is it necessary to unset the session?
if you're no longer using session, yes you can unset it.
0
 

Author Comment

by:Black Sulfur
ID: 41813698
Thanks so much!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now