Allow html in mail php

A classic issue!

By default, PHP's mail() function does not allow HTML content in the email body. This is because the mail() function is designed to send plain text emails, and HTML content can be problematic when sent as plain text.

To allow HTML in your email, you have a few options:

Option 1: Use the Content-Type header

You can set the Content-Type header to text/html to indicate that the email body contains HTML content. Here's an example:

$headers = array(
    'From' => '[email protected]',
    'Content-Type' => 'text/html; charset=UTF-8'
);

mail('[email protected]', 'Subject', '<html><body>This is an HTML email</body></html>', $headers);

Option 2: Use a library or framework

Many PHP libraries and frameworks, such as PHPMailer, SwiftMailer, or Symfony's Mailer component, provide built-in support for sending HTML emails. These libraries often handle the Content-Type header and other email formatting details for you.

Option 3: Use a workaround

If you don't want to use a library or framework, you can use a workaround by encoding your HTML content using a technique like HTML entities or base64 encoding. Here's an example:

$htmlBody = '<html><body>This is an HTML email</body></html>';
$htmlBody = htmlentities($htmlBody); // or base64_encode($htmlBody)
mail('[email protected]', 'Subject', $htmlBody, $headers);

Keep in mind that this workaround may not work perfectly, as it can lead to issues with email clients that don't support HTML emails or have limited HTML rendering capabilities.

Important note

When sending HTML emails, make sure to set the Content-Type header correctly and use a valid HTML structure to ensure that your email is properly rendered by email clients. Additionally, be aware of email client limitations and potential security concerns when sending HTML emails.