简体   繁体   中英

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?

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

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!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM