I have on my client's page a contact form like this:
<form name="sentMessage" id="contactForm" novalidate="novalidate">
<div>
<div>
<label>Name</label>
<input id="name" type="text" required="required">
<p></p>
</div>
</div>
<div>
<div>
<label>mail</label>
<input id="email" type="email" required="required">
<p></p>
</div>
</div>
<div>
<div>
<label>phone</label>
<input id="phone" type="tel" required="required">
<p></p>
</div>
</div>
<div>
<div>
<label>Message</label>
<textarea id="message" required="required"></textarea>
<p></p>
</div>
</div>
<div id="success"></div>
<div>
<button type="submit" id="sendMessageButton">Send</button>
</div>
</form>
When I send the form, a plain text with the message arrives in my client's email, something like this:
This is PHP that does the sending:
<?php
// Check for empty fields
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "empty";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = '[email protected]'; // Add your email address inbetween the '' replacing [email protected] - This is where the form will send a message to.
$email_subject = "misitio.com - $name te escribió";
$email_body = "Tiene un nuevo mensaje en su pagina de misitio.com\n\n"."Estos son los detalles:\n\nNombre: $name\n\nEmail: $email_address\n\nTelefono: $phone\n\nMensaje:\n$message";
$headers = "From: [email protected]\n"; // This is the email address the generated message will be from. We recommend using something like [email protected].
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
I would like to know if it is possible to apply html tags with their respective CSS within these text strings, for example:
$email_body = "<h3>Tiene un nuevo mensaje en su pagina de <b>misitio.com</b><h3> <p>Estos son los detalles:</p><div class="detalle"><p>Nombre: $name ... etc </div>";
When I try it clearly it takes me as plain text except for the values$n
Note: Use jsBootstrapValidation
I thank you for your kind help.
You can make the content that is going to have it
email
in ahtml
separate part and include it in the message like thisand in that
html
you can do all thecss
, it is recommended that you do it in the same file so that there are no errors that do not load the styles, or if you want to do it in the same file youphp
can do it, with the same structure of ahtml
single that, take keep in mind that it is very longdemonstration:
HTML templateEmail.html
PHP
just as you have it, I just move the variable$email_body
and add something elseAnd in the same way with the other, if you have any other questions or it doesn't work for you, tell me
I recommend you to use PHPMailer, it allows you to send emails with HTML format.
You can download it here: https://github.com/PHPMailer/PHPMailer
Apart I leave you an example of how to build a mail with PHPMailer.