Failed to connect to mailserver

0

See, once I was answered that "edo" now I have another problem, it happens that when I send it I get this error:

  

Warning: mail (): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set () in C: \ xampp \ htdocs \ php \ valida.php on line 35

Here is my code: Urgent please.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Envia mail</title>
</head>
<body>
<?php
if(isset($_POST['edo'])){
    $nombre = $_POST['nombre'];
    $apellido = $_POST['apellido'];
    $correo = $_POST['correo'];
    $comentario = $_POST['comentario'];
    if($nombre==""){
        print "<p>El campo 'nombre' es obligatorio.</p>";
    } else if($apellido == ""){
        print "<p>El campo 'apellido' es obligatorio.</p>";
    } else if($correo == ""){
        print "<p>El campo 'correo' es obligatorio.</p>";
    }  else if($comentario == ""){
        print "<p>El campo 'comentario' es obligatorio.</p>";
    } else {
        $mail = "[email protected]";
        $mensaje = "<p>$nombre $apellido te acaba de enviar un correo eléctronico</p>\n";
        $mensaje .= "<p>El correo es $correo y su comentario es:</p>";
        $mensaje .= $comentario;

        $headers = "MIME-Version: 1.0\r\n"; 
        $headers .= "Content-type:text/html; charset=UTF-8\r\n"; 
        $headers .= "From: ".$_POST['correo']."\r\n"; 
        $headers .= "Repaly-to: $correo\r\n";

        $asunto = "$nombre $apellido te envió un email.";

        if(mail($mail, $asunto, $mensaje,$headers)){
            print "<p></p>";
        } else {
            print "Error en el envío de su correo, intentarlo más tarde<br>";
        }

    }

} else {
?>
<form method="post" action="valida.php">
<p>Nombre:  </p><p><input type="text" name="nombre"></p>
<p>Apellido: </p><p><input type="text" name="apellido"></p>
<p>Correo:   </p><p><input type="text" name="correo"></p>
<p>Comentario: </p>
<p><textarea name="comentario" cols="50" rows="6" wrap="off"></textarea> </p>
<br>
<input type="submit" value="Enviar" >
<input type="hidden" value="1" name="edo" >
</form>
<?php } ?>
</body>
</html>
    
asked by Gianni Esquivel 17.09.2017 в 05:39
source

1 answer

3

It happens because you have not configured a mail server on your pc, try uploading your sources to your public server (hosting).

I recommend using PHPMailer , documentation and download: link this will serve you in any environment (local pc, server)

<?php
require './src/PHPMailer.php';
require './src/SMTP.php';

$mail=new PHPMailer();
$mail->CharSet = 'UTF-8';

$body = 'Cuerpo del correo de prueba';

$mail->IsSMTP();
$mail->Host       = 'smtp.gmail.com';
$mail->SMTPSecure = 'tls';
$mail->Port       = 587;
$mail->SMTPDebug  = 1;
$mail->SMTPAuth   = true;
$mail->Username   = '[email protected]';
$mail->Password   = 'tuclave';
$mail->SetFrom('[email protected]', "juliocpiro");
$mail->AddReplyTo('[email protected]','no-reply');
$mail->Subject    = 'Correo de prueba PHPMailer';
$mail->MsgHTML($body);

$mail->AddAddress('[email protected]', 'Gianni');
$mail->send();
?>

In my case I had some errors of not detecting classes, if it happens to you in the two imported files ( src/PHPMailer.php and src/SMTP.php ), comment line 21 of each one where it says namespace PHPMailer \ PHPMailer;

In case you use a GMAIL account to send emails, you will receive an email indicating that an application is trying to log in, it will give you a link to allow access.

    
answered by 17.09.2017 / 07:39
source