What are the best practices for sending form data via email in PHP to avoid unexpected errors such as "unexpected T_STRING"?

When sending form data via email in PHP, it is important to properly format the email content to avoid errors such as "unexpected T_STRING." One common mistake that leads to this error is using double quotes within double quotes without proper escaping. To avoid this, you can use single quotes for the email content and concatenate variables using the dot (.) operator.

// Example of sending form data via email in PHP without unexpected T_STRING error
$to = "recipient@example.com";
$subject = "Form Submission";
$message = 'Name: ' . $_POST['name'] . "\r\n";
$message .= 'Email: ' . $_POST['email'] . "\r\n";
$message .= 'Message: ' . $_POST['message'];

$headers = 'From: sender@example.com';

mail($to, $subject, $message, $headers);