Problem sending HTML email in your body using PHP [duplicate]

0

The code works in parts, sends everything normal, fills in and the like, the only problem I'm having is in $ body, when email arrives in email, the HTML code is displayed and not how it should arrive

PHP code from my contact_me.php

<?php
// Verificar campos vazios
if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['phone']) || empty($_POST['message']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
  http_response_code(500);
  exit();
}

$name = strip_tags(htmlspecialchars($_POST['name']));
$email = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));
$data_envio = date('d/m/Y');
$hora_envio = date('H:i:s');

// É necessário indicar que o formato do e-mail é html
 $headers  = 'MIME-Version: 1.0' . "\r\n";
      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
      $headers .= 'From: $nome <$email>';
  //$headers .= "Bcc: $EmailPadrao\r\n";

// Create the email and send the message
$to = "[email protected]"; // Adicione seu endereço de e-mail entre os "" substituindo [email protected] - Aqui é onde o formulário enviará uma mensagem.
$subject = "Contato GOV:  $name";
$body = "
  <style type='text/css'>
  body {
  margin:0px;
  font-family:Verdane;
  font-size:12px;
  color: #666666;
  }
  a{
  color: #666666;
  text-decoration: none;
  }
  a:hover {
  color: #FF0000;
  text-decoration: none;
  }
  </style>
    <html>
        <table width='510' border='1' cellpadding='1' cellspacing='1' bgcolor='#CCCCCC'>
            <tr>
              <td>
  <tr>
                 <td width='500'>Nome:$nome</td>
                </tr>
                <tr>
                  <td width='320'>E-mail:<b>$email</b></td>
     </tr>
      <tr>
                  <td width='320'>Telefone:<b>$phone</b></td>
                </tr>
                <tr>
                  <td width='320'>Mensagem:$message</td>
                </tr>
            </td>
          </tr> 
          <tr>
            <td>Este e-mail foi enviado em <b>$data_envio</b> às <b>$hora_envio</b></td>
          </tr>
        </table>
    </html>
  ";
$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header = "From: [email protected]\n"; // Este é o endereço de e-mail da mensagem gerada. Recomendamos usar algo como [email protected].
$header .= "Reply-To: $email";  

if(!mail($to, $subject, $body, $header))
  http_response_code(500);
?>

and my HTML form is:

            <form name="sentMessage" id="contactForm" novalidate="novalidate">
                <div class="control-group">
                    <div class="form-group floating-label-form-group controls mb-0 pb-2">
                        <label>Nome</label>
                        <input class="form-control" id="name" type="text" placeholder="Nome" required="required" data-validation-required-message="Por favor, insira seu nome.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="control-group">
                    <div class="form-group floating-label-form-group controls mb-0 pb-2">
                        <label>Seu Email</label>
                        <input class="form-control" id="email" type="email" placeholder="[email protected]" required="required" data-validation-required-message="Por favor, indique o seu endereço de e-mail.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="control-group">
                    <div class="form-group floating-label-form-group controls mb-0 pb-2">
                        <label>Phone Number</label>
                        <input class="form-control" id="phone" type="tel" placeholder="Contato" required="required" data-validation-required-message="Por favor, digite seu número de telefone.">
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <div class="control-group">
                    <div class="form-group floating-label-form-group controls mb-0 pb-2">
                        <label>Message</label>
                        <textarea class="form-control" id="message" rows="5" placeholder="Mensagem" required="required" data-validation-required-message="Por favor, digite uma mensagem."></textarea>
                        <p class="help-block text-danger"></p>
                    </div>
                </div>
                <br>
                <div id="success"></div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary btn-xl" id="sendMessageButton">Enviar</button>
                    <button type="reset" class="btn btn-primary btn-xl">Limpar</button>
                </div>
            </form>

So, arrive at the email: link

    
asked by anonymous 10.10.2018 / 19:38

1 answer

1

To send e-mail in HTML, the Content-type header must be defined.

$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

Source: PHP: Documentation

    
10.10.2018 / 19:47