I am trying to make my own custom contact form. While testing it to make improvements, I found out the emails aren't coming in HTML format. I am not sure why this happens, but I figured out I must be doing something wrong, since my email service provider receives HTML formatted emails from other sources. I think the HTML code inside the message body variable is correct as well, so it must be something related to the headers variable or the way I set it.
EDIT: Unlike someone suggested, using style tags isn't my problem since I am not using Gmail (that doesn't accept them), but Outlook (that does accept them).
if ($_POST['Submit']) {
if (empty($_POST['Phone'])) {
$content = "<html>
<head></head>
<body>
<div class='boldtext'>From:</div>
$name
<br/>
<div class='boldtext'>E-mail:</div>
$email
<br/><br/>
$message
<style>
.boldtext {
font-weight: bold;
}
</style>
</body>
</html>";
}
Above is part of my PHP code for the form action. The remaining part is some regex validations for each input and echos as response to those. The submissions are sent to an Outlook email address, which I know is compatible with the style tag in the head or body. What am I doing wrong?
Sample of email literally received by client:
<html>
<head></head>
<body>
<div class='boldtext'>From:</div>
John Doe
<br/>
<div class='boldtext'>E-mail:</div>
[email protected]
<br/><br/>
This is a message.
<style>
.boldtext {
font-weight: bold;
}
</style>
</body>
</html>
I found out the reason the emails sent were not HTML formatted. It was not HTML or CSS related, The error was in the mail(); function:
The parameter $from interfered with $headers and did not let the HTML format work.
mail($to, $subject, $message, $from, $headers);
mail($to, $subject, $message, $headers);
Everything works correctly now and the CSS structure depends on the email client compatibility.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With