php contact form

0

I have a contact form, the problem is that it does not send the message, I have already looked at spam and others and I have no idea why it is not sent

I put the code. (to perform the tests, I upload it to a hostinger server)

contact.php

<?php
$nombre = $_POST['nombre'];
$email = $_POST['email'];
$mensaje = $_POST['mensaje'];
$para = "[email protected]";
$titulo = "CONTACTO DESDE WEB"; 
$headers = array("From: " .$email,
    "Reply-To: [email protected]",
    "X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("\r\n", $headers);
$msjCorreo = "Nombre:" .$nombre ."\n E-Mail:" .$email ."\n Mensaje:\n"                   .$mensaje;

if ($_POST['submit']) {
    if (@mail($para, $titulo, $msjCorreo, $headers)) {
        echo "
        <script language='javascript'>
        alert('Mensaje enviado, muchas gracias.');
        window.location.href =     'http://..web../index.html#contact';
        </script>";
    } else {
        echo "
        <script language='javascript'>
        alert('Mensaje enviado, muchas gracias.');
        window.location.href = 'Falló el envio';
        </script>";
    }
}
?>

index.html

<div class="col-lg-6 col-sm-5 wow fadeInUp delay-05s">
    <div class="form">
        <form action="contacto.php" method="post">
            <input class="input-text" type="text" name="nombre" value="Nombre y apellidos *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
            <input class="input-text" type="text" name="email" value="E-mail *" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">
            <textarea class="input-text text-area" name="mensaje" cols="0" rows="0" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;">Mensaje *</textarea>
            <input class="input-btn" type="submit" name="submit" value="Enviar">
        </form>
    </div>  
</div>
    
asked by Ruben Manzano 21.09.2016 в 16:59
source

3 answers

1

Hello! I did not remember what was the use of the arroba in the code, and refreshing my memory with a search, I see that it is to suppress error messages. You should call the function mail , but without the arroba, because the same and is generating an error that is not displayed by the same. Maybe you should also place the code in a try / catch block to handle the error.

Greetings.

I leave the league of the at

    
answered by 21.09.2016 в 20:53
1

You must remove the @ of the function mail , here is an example:

<?php
 $to = "[email protected], [email protected]";
 $subject = "HTML email";

 $message = "
 <html>
 <head>
 <title>mi correo</title>
 </head>
 <body>
 <p>Esto funciona :)</p>

 </body>
 </html>
 ";


 $headers = "MIME-Version: 1.0" . "\r\n";
 $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";

 $headers .= 'Cc: [email protected]' . "\r\n";

 mail($to,$subject,$message,$headers);
 ?>

Head over to the W3C ;)

    
answered by 21.09.2016 в 20:56
1

A super easy way to do it is with PHPMailer

require_once "PHPMailerAutoload.php";

$nombre = $_POST['nombre'];
$mail = $_POST['email'];
$mensaje = $_POST['mensaje'];
$para = "[email protected]";
$titulo = "CONTACTO DESDE WEB"; 
$email = new PHPMailer;
$email->isSMTP();
$email->Host = 'smtp.gmail.com';  //Servidor de correo saliente en este caso gmail
$email->SMTPAuth = true;
$email->Username = '[email protected]';                 
$email->Password = 'pass';                          
$email->SMTPSecure = 'ssl';                            
$email->Port = 465;      

$email->From = $mail; //quien envia el correo
$email->FromName = $nombre; //Nombre de quien lo envie
$email->Subject = $titulo; //Titulo
$email->Body = $mensaje; //Aquí todo el contenido del mensaje
$email->AddAddress($para); //la direccion para quien es el correo
if (!$email->Send()) {
    echo $email->ErrorInfo;
}else{
    echo "Se envio";
}

You can find the library PHPMailer

    
answered by 22.10.2016 в 21:54