1

I have this form that sends emails to the email.In localhost it works fine, when I upload it to host it gives me the following bug.

Could not access file: SMTP Error: Could not connect to SMTP host

email.php

$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$portal = $_POST['portal'];
$piso = $_POST['piso'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$incidencia = $_POST['incidencia'];
$message = $_POST['message'];
$archivo = $_FILES['adjunto'];

require("archivosformulario/class.phpmailer.php");

$mail = new PHPMailer();

$mail->From     = $email;
$mail->FromName = $first_name; 
$mail->AddAddress("micrreo@gmail.com"); // Dirección a la que llegaran los mensajes.


$mail->WordWrap = 50; 
$mail->IsHTML(true);    
$mail->Subject  =  "Incidencia ";

$mail->Body     =  
    "Esto es un correo generado desde la web, si quiere mas informacion contacte con: correo@gmail.com \n<br". 
    "Nombre: $first_name \n<br />".    
    "Apellido: $last_name \n<br />".  
    "Portal: $portal \n<br />".
    "Piso: $piso \n<br />".
    "Email: $email \n<br />".
    "Telefono: $phone \n<br />".  
    "Incidencia: $incidencia \n<br />".
    "Mensaje: $message \n<br />";

$mail->AddAttachment($archivo['tmp_name'], $archivo['name']);



$mail->IsSMTP(); 
$mail->Host = "ssl://smtp.gmail.com:465";  // Servidor de Salida.
$mail->SMTPAuth = true; 
$mail->Username = "micorreo@gmail.com";  // Correo Electrónico
$mail->Password = "mipasswd"; // Contraseña


if ($mail->Send()){
    echo "<script>alert('bien');location.href ='javascript:history.back()';</script>";
}else{
    echo "<script>alert('Error al enviar el formulario')</script>";
    var_dump($_POST);
    exit();
}

?>

sorry for my english, I'm spanish Thank you very much to anyone who helps me

1 Answers1

0

It may be due to a lack of SSL support in the PHP version of your host, as it's written here : PHPMailer: SMTP Error: Could not connect to SMTP host

So, check in your php.ini file if extension=php_openssl.dll is enabled.

If it's disabled, don't forget to restart the web server after enabling this extension.

Hope it will help you.

EDIT after your answer :

Although I'm not sure it can help, you can try to replace the following line :

$mail->Host = "ssl://smtp.gmail.com:465";

by those lines :

$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com"; 
$mail->Port = 465;

Also, if it doesn't work with SSL, you can try to use 'tls' instead of 'ssl', and to replace the port 465 by the port 587.

If it still doesn't work, you can add the following line that will give you more information on your error :

$mail->SMTPDebug = 2;