I am unable to send this form to e-mail

Asked

Viewed 60 times

1

I am unable to send this form to e-mail, 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!";
?>
  • Is there an error message? If so, please add the question. You know you need to have the server smtp correctly configured?

1 answer

2

You forgot some basic things to email with PHP.

When you started the form on <form action="enviar.php"> you have forgotten the method which in this your case is POST being like this:

<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">

Should stay like this:

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

In your PHP you need to define a subject variable, besides declaring variables within the message without concatenating and using double quotes, so you concatenate only the next line. The function mail does not include all variables, because they go inside the variable mensagem.

See the full 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";
    }

?>

Browser other questions tagged

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