How can PHP variables be properly inserted into the $message string for sending HTML emails?

When inserting PHP variables into the $message string for sending HTML emails, you can concatenate the variables within double quotes to ensure they are properly inserted. This allows you to include dynamic content such as user input or database values in your email messages.

$email = "recipient@example.com";
$subject = "Hello, $name!";
$message = "<html><body>";
$message .= "<h1>Welcome, $name!</h1>";
$message .= "<p>Your email address is: $email</p>";
$message .= "</body></html>";

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: sender@example.com' . "\r\n";

// Send email
mail($email, $subject, $message, $headers);