Problems with php mail ()

0

This is my code, it seems to be running, but I do not receive any mail, I am running on server with windows, php 5.3

<!doctype html>
<html>
    
    <head>
        <title>Pruebas</title>
        <meta charset="utf-8">
    </head>
    
    <body>
        <form action="enviar.php" method="post">
            <input type="text" name="formNombre" placeholder="nombre">
            <input type="email" name="formCorreo" placeholder="correo">
            <input type="tel" name="formTelefono" placeholder="telefono">
            <button type="submit">Enviar</button>
        </form>
    </body>


    
    </html>
<?php

$nombre = $_POST ["formNombre"];
$correo = $_POST ["formCorreo"];
$telefono = $_POST ["formTelefono"];

$destino = "[email protected]";
$asunto = "Contacto de $nombre";
$mensaje = "Hola soy $nombre y este es mi teléfono $telefono";

$cabeceras = "From: $correo";

if (mail($destino, $asunto, $mensaje, $cabeceras))
{
    echo("Correo enviado");
}
else
{
    echo("Error en el envío");
}


?>
    
asked by Ana Herz 26.05.2017 в 00:37
source

3 answers

1

Hello friend, I had the same problem a few days ago and yrnia a very similar code, I recommend you upload your code to a hosting I use hostinger for previews, I recommend it a lot if you only want to test, your code is correct, the problem will be your local server if you use XAMPP is a very typical problem that is not sent I recommend you use hostinger.

My code is this you can adapt it to your needs:

<?php 
if(isset($_POST['submit'])){
    $to = "[email protected]"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $subject = "Lovi acabados";
    $subject2 = "Copy of your form submission";
    $message = $first_name . " " . $last_name . " Te a enviado este mensaje : " . "\n\n" . $_POST['message'];
    $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];

    $headers = "From:" . $from;
    $headers2 = "From:" . $to;
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender

    echo "Gracias por enviarnos tu mensaje" . $first_name . ", Nos contactaremos lo mas pronto contigo.";
    // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>

This is the hostinger link

    
answered by 26.05.2017 в 02:48
0

The problem may arise because your computer or server does not have an SMTP server configured

The code is correct, I think that may be the error.

If PHP does not give you an error, check it by enabling them to be displayed by modifying the php.ini

Search:

display_errors = Off

Change it to:

display_errors = On

And restart Apache this will serve so that you can see some additional error.

Greetings

    
answered by 26.05.2017 в 02:11
0

To send an email you must have a service that makes it, in linux it is used postfix or others, they are responsible for sending it. The phpmail class returns ok because it does its part but the mail does not arrive because there is no service that takes care of it. If your app is going to be displayed on a linux server, my recommendation is that you use vagrant and not xampp, it allows you to have an environment similar to production and minimize this type of errors.

    
answered by 26.05.2017 в 03:17