I can not send this form to the e-mail

1

I'm not able to send this form to email, I followed a tutorial and it did not work!

HTML

<body>
    <div class="col-md-6 centro" method="POST">
        <img class="imagem" src="AMB.png">
        <form action="enviar.php">
            <div class="form-group">
                <label for="nome">Nome Completo:</label>
                <input type="text" class="form-control" id="nome">
            </div>
            <div class="form-group">
                <label for="cpf" onblur="TestaCPF(this)">CPF:</label>
                <input type="number" class="form-control" id="cpf">
            </div>
            <div class="form-group">
                <label for="crm">CRM:</label>
                <input type="number" class="form-control" id="crm">
            </div>
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" class="form-control" id="email">
            </div>
            <button type="enviar" class="botao btn btn-default">Enviar</button>
        </form>
    </div>
</body>

PHP

<?php
    $para = '[email protected]';
    $nome = $_POST['nome'];
    $cpf = $_POST['cpf'];
    $crm = $_POST['crm'];
    $email = trim($_POST['email']);

    $mensagem = '<strong>Nome: </strong>'.$nome;
    $mensagem .= '<br> <strong>CPF: </strong>'.$cpf;
    $mensagem .= '<br> <strong>CRM: </strong>'.$crm;
    $mensagem .= '<br> <strong>Email: </strong>'.$email;

    $headers = "MIME-Version: 1.1\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "From: $email\r\n";
    $headers .= "Return-Path: $para \r\n";

    $envio = mail($para, $nome, $cpf, $crm, $mensagem, $headers);

    echo "Formulário enviado com sucesso!";
?>
    
asked by anonymous 03.10.2016 / 17:08

1 answer

2

You've forgotten some basic things for sending email with PHP.

When you started the form in <form action="enviar.php"> you forgot the method that in this case is POST thus:

<form action="enviar.php" method="post">

In each input you need to declare name and not only id . For example:

<input type="text" class="form-control" id="nome">

It should look like this:

<input type="text" class="form-control" id="nome" name="nome">

In your PHP you need to define a subject variable, in addition to declaring in the message the variables without concatenating and using double quotation marks, then you concatenate only the next line. The mail function does not include all variables, since they are within the mensagem variable.

See the complete code to understand better:

YOUR HTML:

<body>
    <div class="col-md-6 centro" method="POST">
        <img class="imagem" src="AMB.png">
        <form action="enviar.php" method="post">
            <div class="form-group">
                <label for="nome">Nome Completo:</label>
                <input type="text" class="form-control" id="nome" name="nome">
            </div>
            <div class="form-group">
                <label for="cpf" onblur="TestaCPF(this)">CPF:</label>
                <input type="number" class="form-control" id="cpf" name="cpf">
            </div>
            <div class="form-group">
                <label for="crm">CRM:</label>
                <input type="number" class="form-control" id="crm" name="crm">
            </div>
            <div class="form-group">
                <label for="email">Email:</label>
                <input type="email" class="form-control" id="email" name="email">
            </div>
            <button type="enviar" class="botao btn btn-default">Enviar</button>
        </form>

    </div>
</body>

YOUR PHP:

<?php
    $para = '[email protected]';
    $assunto = '(SEU ASSUNTO AQUI)';
    $nome = $_POST['nome'];
    $cpf = $_POST['cpf'];
    $crm = $_POST['crm'];
    $email = trim($_POST['email']);



    $mensagem = "<strong>Nome: </strong>$nome";
    $mensagem .= "<br> <strong>CPF: </strong>$cpf";
    $mensagem .= "<br> <strong>CRM: </strong>$crm";
    $mensagem .= "<br> <strong>Email: </strong>$email";

    $headers = "MIME-Version: 1.1\r\n";
    $headers .= "Content-type: text/html; charset=utf-8\r\n";
    $headers .= "From: $email\r\n";
    $headers .= "Return-Path: $para \r\n";

    $envio = mail($para, $assunto, $mensagem, $headers);

    if($envio){
        echo "Formulário enviado com sucesso!";
    }
    else{
        echo "Erro";
    }

?>
    
03.10.2016 / 17:43