Ian Fako
Ian Fako

Reputation: 1210

PHP getting HTML email layout and pass variable

I want to send emails and store all email layouts in a seperate subfolder. Let's assume the file emails/mailLayout.php contains something like

<a><?php echo $item["name"]?></a>

Approach to get the content:

$item["name"] = "John Doe";
$emailContent = file_get_contents("emails/mailLayout.php);

//send mail...

$emailContent should contain <a>John Doe</a> but thats not the case

Any solutions or better approaches?

Upvotes: 1

Views: 83

Answers (2)

prakash tank
prakash tank

Reputation: 1267

For this just do one thing :

For this first get the content from the .php file and then do something like this :

 $name = "John Doe";
 $text = file_get_contents("emails/mailLayout.php");
 $text = str_replace('USERNAME', $name, $message);

and in your mailLayout.php just replace the php value with some Constants. for ex : Hi this is USERNAME or any unique value.

Hope it helps!!!

Upvotes: 0

LP154
LP154

Reputation: 1497

You can use php's output buffer :

$item["name"] = "John Doe";
ob_start();
require "emails/mailLayout.php";
$emailContent = ob_get_contents();
ob_end_clean();
//send mail...

file_get_contents does not interpret PHP code

Upvotes: 2

Related Questions