Mail body empty

0

The emails arrive with my body empty, first I thought it could be the name in the html but that solution did not work and I do not know what I can modify.

<form action="TestMail.php" method="post" name="contact-form" id="main-contact-form">
            <div class="form-group">
                <input type="text" required placeholder="Nombre" class="form-control" name="nombre" id="name" >
            </div>
            <div class="form-group">
                <input type="email" required placeholder="Email" class="form-control" name="email" id="email" >
            </div>
            <div class="form-group">
                <input type="text" required placeholder="Asunto" class="form-control" name="asunto" id="subject">
            </div>
            <div class="form-group">
                <textarea required placeholder="Mensaje" rows="8" class="form-control" name="mensaje" id="message"></textarea>
            </div>
            <button class="btn btn-primary" type="submit">Enviar</button>
                            </form>


<?php

        $mail_to = "[email protected]";
        $asunto="Contacto desde nuestra web";

        $name =  $_REQUEST["nombre"];
        $email = $_REQUEST["email"];
        $subject = $_REQUEST["asunto"];          
        $message = $_REQUEST["mensaje"];

        $content = "Nombres: $name\n";
        $content .= "E-mail: $email\n\n";
        $content .= "Mensaje:\n$message\n";

        $headers = "From: $name <$email>";

        $success = mail($mail_to, $asunto, $content, $headers);
        if ($success) {

            echo "¡Gracias! Tu mensaje ha sido enviado.";
        } else {

            echo "Oops! Algo salió mal, no pudimos enviar tu mensaje.";
        }

?>

what I get is:

Nombres: 
E-mail: 

Mensaje:
    
asked by Sebastian 28.10.2018 в 14:22
source

2 answers

0

Sending mail with mail is quite artisanal.
Each header has to end with "\r\n" .

$headers = "From: $name <$email>\r\n";

Additional comments:

You may also need this other header (for the answers):

"Reply-To: $name <$email>\r\n"

And if you put it in a hosting, probably also (to avoid being frowned upon as possible spam) -Apart from setting DKIM and SPF in the hosting -:

"Return-Path: $email\r\n"

The parameters received in the request must be validated to avoid possible manipulation by the user who makes the request.

One suggestion: If you want to simplify your life by sending mail, check PHPMailer.

Important note: $email in the examples is the email from the one who sends the email from the hosting (that is yours), not the user that is doing the submit. I mention it because in your example this is a bit confusing.

    
answered by 28.10.2018 / 15:30
source
0

If you look at the method of your form, put POST but then try to catch them with a request. So I think you're not taking the form data.

Try changing this and if you want to do the test of doing echo to see if you are receiving the data.

$name =  $_POST["nombre"];
$email = $_POST["email"];
$subject = $_POST["asunto"];          
$message = $_POST["mensaje"];
    
answered by 28.10.2018 в 15:14