How can string templates be utilized in PHP to improve readability and simplify modifications to email bodies?

Using string templates in PHP can improve readability and simplify modifications to email bodies by separating the content from the code logic. By storing the email body as a template string, it allows for easy editing without having to navigate through complex PHP code. This also makes it easier to reuse the template for multiple emails with different variables.

// Define email template
$emailTemplate = "
Hello {name},

Thank you for your purchase of {product}! 
Your order total is {total}.

Regards,
Your Company
";

// Replace placeholders with actual values
$emailBody = str_replace(['{name}', '{product}', '{total}'], ['John Doe', 'Product X', '$50'], $emailTemplate);

// Send email with $emailBody as the content