What potential pitfalls should be considered when using eval() to evaluate PHP code from a database for email templates?

Using eval() to evaluate PHP code from a database for email templates can pose security risks as it allows for the execution of arbitrary code. This can lead to code injection attacks and compromise the security of your application. To mitigate this risk, it is recommended to use alternative methods such as storing the template as a string and using placeholders to dynamically insert data.

// Sample code snippet using placeholders instead of eval() for email templates
$template = "Hello {name}, your account balance is {balance}.";
$data = [
    'name' => 'John Doe',
    'balance' => '$100.00'
];

// Replace placeholders with actual data
foreach ($data as $key => $value) {
    $template = str_replace('{' . $key . '}', $value, $template);
}

// Send email with the populated template
// mail($recipient, $subject, $template);