Problem sending email with HTML in your body using PHP

Asked

Viewed 79 times

0

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

PHP code of 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>

It comes in the email like this: http://prntscr.com/l4jdm5

  • I closed as duplicate because the other explains in detail, but your post is more case of error, if to analyze coldly. It already starts with the fact that it has markup outside the <html> tag. Another thing that would be good is to fix the block "header", has a "header" and a "headers", I can see that is the last one worth, but is confused in the question.

  • Try applying the concepts of the linked post to the closure (with a lot of attention), but if you can’t, leave a comment here or edit the post in more detail (with example raw output, if you can, but after you fix the HTML structure). The question can eventually be reopened if given to detect a problem that was not solved in the other post.

  • I managed to solve using this post that suggested me, thank you very much, the problem was in .= and now I am using UTF-8, thus: $header .= 'Content-type: text/html; charset=utf-8' . " r n";

  • I’m glad you solved it! Now that you’ve commented, I noticed that your From was actually writing Mimetype, these things just rereading it really carefully.

1 answer

1

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

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

Source: PHP: Documentation

  • I put it again and nothing, I left it like this: http://prntscr.com/l4jv44 but the error persists

  • @Wesleyvicente in the Content-Type headers change the charset to utf-8 and see if it works.

  • 1

    The real problem is in this line: $header = "From: [email protected]\n"; - note that the author forgot to concatenate, should be .= and not = (the author himself located the error)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.