Error sending mail in php

4

I have tried a thousand different ways but I can not find a solution to this problem. I am trying to send an email with the code that I attached below, which I have in two other websites and they work perfectly. If someone can help me with this topic I would appreciate it very much.

<?php
if(isset($_POST['email'])) {

//Preferencias
$email_to = "[email protected]";
$email_subject = "Contacto desde el sitio web";
// Aquí se deberían validar los datos ingresados por el usuario
if(!isset($_POST['nombre']) ||
    !isset($_POST['email']) ||
    !isset($_POST['texto'])) {

    header('Location: error-contacto.php');
}

$email_message = "Detalles del formulario de contacto:\n\n";
$email_message .= "Nombre: " . $_POST['nombre'] . "\n\n";
$email_message .= "E-mail: " . $_POST['email'] . "\n\n";
$email_message .= "Núm. de Pasajeros: " . $_POST['pasajeros'] .                     "\n\n";
    if (isset($_POST['checkbox'])){
        $email_message .= "La persona no sabe exactamente las     fechas.\n\n";
    }
    else{
       $email_message .= "Fecha de llegada: " . $_POST['llegada'] .     "\n\n";
       $email_message .= "Fecha de salida: " . $_POST['salida'] .     "\n\n";
    }
$email_message .= "Mensaje:\n " . $_POST['texto'] . "\n\n";

// Ahora se envía el e-mail usando la función mail() de PHP
$headers = 'From: '.$_POST['email']."\r\n".
'Reply-To: '.$_POST['email']."\r\n" .
'X-Mailer: PHP/' . phpversion();
$resultado = mail($email_to, $email_subject, $email_message,     $headers);
    if ($resultado ==true) 
        { 
            header('Location: gracias.php');
        } 
        else{ 
            header('Location: error-contacto.php');
        }  
}
?>

I followed the advice of @abrahamhs and I started using PhpMailer, the problem is that now when I use it, I'm using http Error 500

require_once('includes/class.phpmailer.php');

$mail = new PHPMailer();
$nombre = $_POST['nombre'];
$direccion = $_POST['email'];

$body = "Detalles del formulario de contacto:\n\n";
$body .= "Nombre: " . $nombre . "\n\n";
$body .= "E-mail: " . $direccion . "\n\n";
$body .= "Núm. de Pasajeros: " . $_POST['pasajeros'] . "\n\n";
    if (isset($_POST['checkbox'])){
        $body .= "La persona no sabe exactamente las fechas.\n\n";
    }
    else{
       $body .= "Fecha de llegada: " . $_POST['llegada'] . "\n\n";
       $body .= "Fecha de salida: " . $_POST['salida'] . "\n\n";
    }
$body .= "Mensaje:\n " . $_POST['texto'] . "\n\n";
$mail->IsSMTP(); 
$mail->SMTPDebug = 2;

$mail->SMTPAuth = true;                 
$mail->Host = 'mail.midireccion.com'
$mail->Port = 587;
$mail->Username = '[email protected]';
$mail->Password = 'contrasena'; 

$mail->SetFrom('[email protected]', 'Contacto desde el sitio web');
$mail->AddReplyTo($direccion);
$mail->Subject = 'Contacto desde el sitio web';
$mail->AltBody = $body;
$mail->MsgHTML($body);

$mail->AddAddress('[email protected]', 'Contacto desde el sitio web');
if(!$mail->send()) {
    ini_set('display_errors', 1);
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
header('Location: gracias.php');
}

I was testing and every code I put before the $ mail-> send () is executed. Can it be a server configuration error? When I run the phpinfo function it turns out that the SMTP port is 25, but in the data of my account it says that it is 587. What use?

I already have PhpMailer configured but when I run it and try to send an email I get the following error:

  

2017-03-11 15:27:00 SERVER - > CLIENT: 220-c037-dr.dattaweb.com ESMTP   Exim 4.87_1 # 2 Sat, 11 Mar 2017 12:27:00 -0300 220- We do not   authorize the use of this system to transport unsolicited, 220 and / or   bulk e-mail 2017-03-11 15:27:00 CLIENT - > SERVER: EHLO   midominio.com.ar 2017-03-11 15:27:00 SERVER - > CLIENT:   250-c037-dr.dattaweb.com Hello midominio.com.ar [200.58.110.24]   250-SIZE 52428800 250-8BITMIME 250-PIPELINING 250-AUTH LOGIN   250-STARTTLS 250 HELP 2017-03-11 15:27:00 CLIENT - > SERVER: STARTTLS   2017-03-11 15:27:00 SERVER - > CLIENT: 220 TLS go ahead 2017-03-11   15:27:00 SMTP Error: Could not connect to SMTP host. 2017-03-11   15:27:00 CLIENT - > SERVER: QUIT 2017-03-11 15:27:00 SERVER - > CLIENT:   221 c037-dr.dattaweb.com closing connection 2017-03-11 15:27:00 SMTP   connect () failed.    link Mailer   Error: SMTP connect () failed.    link

I do not know if I should have to configure something on the server or if I have to add or remove lines of php code, but I'm using port 587 and I have the configurations SMTPAuth = true
SMTPSecure = 'tls'

What other configuration should I perform so that the emails are sent correctly and the server authorizes me?

    
asked by Joaquín Bozzalla 10.03.2017 в 00:36
source

1 answer

1

This looks like something that has happened to me, I apologize if I do not give the answer but I can not comment yet.

E problem: This is a problem with the provider since they have their own security block certain characteristics that the phpMailer uses.

My solution: Use the native function of PHP mail() , since it is orthodox and does not allow authentication and others, but it was what worked for me and I have not had any problems so far.

UPDATE 1: SOLUTION

use the function mail() php

    
answered by 15.03.2017 в 02:45