1

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?

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

You could even replace ob_get_contents(); and ob_end_clean(); with the one function ob_get_clean(); to keep it more readable, it'll get the contents of the output buffer and clean it
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.