Foros del Web » Programando para Internet » PHP »

Enviar correo con tildes y ñ

Estas en el tema de Enviar correo con tildes y ñ en el foro de PHP en Foros del Web. Hola, estoy trabajando en una web de reparaciones a domicilio y contiene un formulario de contacto con varios campos a rellenar, el problema es que ...
  #1 (permalink)  
Antiguo 03/09/2012, 12:59
Avatar de legomolina  
Fecha de Ingreso: junio-2011
Ubicación: Valencia
Mensajes: 165
Antigüedad: 12 años, 10 meses
Puntos: 3
Enviar correo con tildes y ñ

Hola, estoy trabajando en una web de reparaciones a domicilio y contiene un formulario de contacto con varios campos a rellenar, el problema es que cuando alguien en el formulario pone caracteres especiales cuando los veo yo en mi Outlook se me ven como cosas raras. Se que es por la codificacion pero he probado con utf8_encode, htmlentities... pero haga lo que haga funciona igual de mal.
Aqui les dejo el formulario:
Código PHP:
<?php 
include("funciones.php");
session_start();
$borrar=false;

//damos valor inicial a las variables value
$nombre="";
$email="";
$provincia="Valencia";
$poblacion="";
$telefono="";
$apellido="";
$movil="";
$direccion="";
$averia="";
$codigo_postal="";
$dni="";
$marca="";
$modelo="";
$fecha_compra="DD/MM/AAAA";
$comercio="";
$garantia="Si";

if(isset(
$_SESSION['nombre'])) {
    
$borrar=true;
    
// si se han definido las sessions intercambiamos los nombres
    
$nombre=$_SESSION['nombre'];
    
$email=$_SESSION['email'];
    
$provincia=$_SESSION['provincia'];
    
$poblacion=$_SESSION['poblacion'];
    
$telefono=$_SESSION['telefono'];
    
$apellido=$_SESSION['apellido'];
    
$movil=$_SESSION['movil'];
    
$direccion=$_SESSION['direccion'];
    
$averia=$_SESSION['averia'];
    
$codigo_postal=$_SESSION['codigo_postal'];
    
$dni=$_SESSION['dni'];
    
$marca=$_SESSION['marca'];
    
$modelo=$_SESSION['modelo'];
    
$fecha_compra=$_SESSION['fecha_compra'];
    
$comercio=$_SESSION['comercio'];
    
$garantia=$_SESSION['garantia'];
}
?>
<!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" />
<link href="styles/estilo.css" rel="stylesheet" type="text/css" /><title>SAT BalMa - Contacta</title>
<script languaje="javascript">
function habilita(form)

    form.fecha_compra.disabled = false;
    form.comercio.disabled = false;
}

function deshabilita(form)

    form.fecha_compra.disabled = true;
    form.comercio.disabled = true;
}
</script>
</head>

<body>
<table width="96%" height="691" border="0">
  <tr>
    <td width="70%" height="77"><img src="ima/contacto.png" alt="" width="687" height="103" /></td>
    <td width="30%" rowspan="2" valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td rowspan="2" valign="top">
      <form action="enviar_form.php" method="post" name="formulario_contacto">
        <fieldset>
        <table width="100%" border="0">
            <legend>DATOS DEL USUARIO</legend>
            <tr>
              <td colspan="2"><input type="text" value="<?php echo fechaActual(); ?>" size="10" maxlength="10" name="fecha" disabled="disabled" /></td>
            </tr>
            <tr>
                <td width="120">Nombre:</td> <td><input type="text" value="<?php echo $nombre?>" name="nombre" size="60" /> *</td>
             </tr>
            <tr>
                <td>Apellidos:</td> <td><input value="<?php echo $apellido?>" type="text" name="apellidos" size="60" /></td>
            </tr>
            <tr>
                <td>Tel&eacute;fono:</td> <td colspan="3"><input value="<?php echo $telefono?>" type="text" name="telefono" size="9" maxlength="9" /> 
                *  &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;M&oacute;vil: 
                <input value="<?php echo $movil?>" type="text" name="movil" size="9" maxlength="9" /></td> 
            </tr>
            <tr>
                <td>E-Mail:</td> <td><input value="<?php echo $email?>" type="text" name="email" size="60" /> 
                *</td>
               </tr>
            <tr>
                <td>Provincia:</td> <td><input type="text" value="<?php echo $provincia?>" name="provincia" size="60" /> 
                *</td>
            </tr>
            <tr>
                <td>Poblaci&oacute;n:</td> <td><input type="text" name="poblacion" value="<?php echo $poblacion?>" size="60" /> 
                * </td>
            </tr>
            <tr>
                <td>Direcci&oacute;n:</td> <td><input value="<?php echo $direccion?>" type="text" name="direccion" size="60" /></td>
            </tr>
            <tr>
                <td>C&oacute;digo Postal:</td> <td><input value="<?php echo $codigo_postal?>" type="text" name="c_postal" size="5" maxlength="5" /></td>
            </tr>
            <tr>
                  <td>DNI/CIF</td> <td><input name="dni_num" type="text" value="<?php echo $dni?>" size="15" /></td>
            </tr>
       </table>
        </fieldset>
        <fieldset>
           <table width="100%" border="0">
           <legend>DATOS DEL APARATO</legend>
               <tr>
                <td width="120">
                Marca: </td>
              <td><select name="marca_campana">
                    <option value="pando" <?php if($marca=='pando') echo 'selected="selected"'; else echo 'selected="selected"'?> id="pando">Pando</option>
                    <option <?php if($marca=='orpan') echo 'selected="selected"'?> value="orpan" id="orpan">Orpan</option>
                    <option value="thermex" <?php if($marca=='thermex') echo 'selected="selected"'?> id="thermex">Thermex</option>
                 </select></td>
              </tr>
            <tr>
                <td width="120">Modelo:</td> <td><input value="<?php echo $modelo?>" type="text" name="modelo" size="60" /></td>
               </tr>
            <tr>
                 <td>Aver&iacute;a o consulta: </td><td><textarea name="averia" cols="60" rows="10"><?php echo $averia?></textarea> 
                   </td>
            </tr>
            <tr>
                <td>Garant&iacute;a: </td>
                       <td><label for="s">Sí:</label> 
                    <input type="radio" id="s" name="garantia" value="Si" <?php if($garantia=='Si') echo'checked="checked"'?> onClick="habilita(this.form)" />
                    <label for="n">No:</label> <input id="n" name="garantia" <?php if($garantia=='No') echo'checked="checked"'; else echo 'checked="checked"'?> type="radio" onClick="deshabilita(this.form)" value="No" /></td>
               </tr>
            <tr>         
                   <td>Fecha de compra:</td> <td><input disabled="disabled" type="text" value="<?php echo $fecha_compra?>" name="fecha_compra" size="12" maxlength="10" /></td>
            </tr>
            <tr>          
                  <td>Comercio de la compra: </td> <td><input disabled="disabled" value="<?php echo $comercio?>" type="text" name="comercio" size="30" /></td>
               </tr>
           </table> 
         </fieldset>
        <p>(*) Campos Obligatorios.<br /><br />
          <input type="hidden" name="fecha" value="<?php echo fechaActual(); ?>" />
          <input type="hidden" name="borrar" value="<?php echo $borrar?>" />
          <input align="middle" type="image" src="ima/botones/enviar.png" name="enviar"  />
        </p>
</form>
    </td>
  </tr>
  
</table></div></div>
</div>
</body>
</html>
  #2 (permalink)  
Antiguo 03/09/2012, 13:00
Avatar de legomolina  
Fecha de Ingreso: junio-2011
Ubicación: Valencia
Mensajes: 165
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Enviar correo con tildes y ñ

y la pagina que envia el correo (no me dejaba todo en uno :)):
Código PHP:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="../styles/estiloprincipal.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php
//si el campo 'borrar' tiene un valor 'true' las variables de session se destruiran 
if($_POST['borrar']=='true') {
unset(
$_SESSION['nombre'],$_SESSION['apellido'],$_SESSION['email'],$_SESSION['provincia'],$_SESSION['poblacion'],$_SESSION['telefono'],$_SESSION['movil'],$_SESSION['averia'],$_SESSION['codigo_postal'],$_SESSION['dni'],$_SESSION['marca'],$_SESSION['modelo'],$_SESSION['fecha_compra'],$_SESSION['comercio'],$_SESSION['garantia']);
}

//definimos las variables necesarias
$fecha=$_POST['fecha'];//obligado
$nombre=$_POST['nombre'];//obligado
$apellido="";
$email=$_POST['email'];//obligado
$provincia=$_POST['provincia'];//obligado
$direccion="";
$poblacion=$_POST['poblacion'];//obligado
$telefono=$_POST['telefono'];//obligado
$movil="";
$averia="";
$codigo_postal="";
$dni="";
$marca="";
$modelo="";
$fecha_compra="";
$comercio="";
$garantia="";
$vinculo='<br /><a href="contacta.php" target="principal">Volver a contactar</a>';

//Definimos las sessions
$_SESSION['nombre']=$nombre;
$_SESSION['email']=$email;
$_SESSION['provincia']=$provincia;
$_SESSION['poblacion']=$poblacion;
$_SESSION['telefono']=$telefono;
$_SESSION['apellido']=$apellido;
$_SESSION['movil']=$movil;
$_SESSION['direccion']=$direccion;
$_SESSION['averia']=$averia;
$_SESSION['codigo_postal']=$codigo_postal;
$_SESSION['dni']=$dni;
$_SESSION['marca']=$marca;
$_SESSION['modelo']=$modelo;
$_SESSION['fecha_compra']=$fecha_compra;
$_SESSION['comercio']=$comercio;
$_SESSION['garantia']=$garantia;

//
//CONDICIONES QUE SE TIENE QUE DAR PARA QUE SE ENVIE EL E-MAIL
//

//El campo nombre debe ser obligatorio
if($nombre=="") {
    echo 
"El campo 'Nombre' no puede estar vac&iacute;o";
    echo 
$vinculo;
    exit;
}

//El campo telefono debe ser obligatorio
if($telefono=="") {
    echo 
"El campo 'Tel&eacute;fono' no puede estar vac&iacute;o";
    echo 
$vinculo;
    exit;
}

// El telefono debe contener 9 d�gitos
if((strlen($telefono) < 9)) {
    echo 
"El tel&eacute;fono tiene que contener 9 cifras";
    echo 
$vinculo;
    exit;
}

//El campo email debe ser obligatorio
if($email=="") {
    echo 
"El campo'E-mail' no puede estar vac&iacute;o";
    echo 
$vinculo;
    exit;
}

//El campo provincia debe ser obligatorio
if($provincia=="") {
    echo 
"El campo 'Provincia' no puede estar vac&iacute;o";
    echo 
$vinculo;
    exit;
}

//El campo poblacion debe ser obligatorio
if($poblacion=="") {
    echo 
"El campo 'Poblaci&oacute;n' no puede estar vac&iacute;o";
    echo 
$vinculo;
    exit;
}

//
//SI SE DEFINEN LAS NO-OBLIGADAS VAMOS ACTUALIZANDO LAS SESSIONS Y LAS DEMAS VARIABLES
//

//si se define apellidos
if(isset($_POST['apellidos']) && $_POST['apellidos']!="") {
    
$apellido=$_POST['apellidos'];
    
$_SESSION['apellido']=$apellido;
}

//si se define movil
if(isset($_POST['movil']) && $_POST['movil']!="") {
    
$movil=$_POST['movil'];
    
$_SESSION['movil']=$movil;
    
// El movil debe contener 9 d�gitos
    
if (strlen($movil) < 9){
        echo 
"El m�vil debe contener 9 cifras";
        echo 
$vinculo;
        exit;
    }
}    

//si se define direccion
if(isset($_POST['direccion']) && $_POST['direccion']!="") {
    
$direccion=$_POST['direccion'];
    
$_SESSION['direccion']=$direccion;
}

//si se define codigo postal
if(isset($_POST['c_postal']) && $_POST['c_postal']!="") {
    
$codigo_postal=$_POST['c_postal'];
    
$_SESSION['codigo_postal']=$codigo_postal;
    if(
strlen($codigo_postal)<5) {
        echo 
"El codigo postal tiene que contener 5 cifras";
        echo 
$vinculo;
        exit;
    }
}

//si se define dni
if(isset($_POST['dni_num']) && $_POST['dni_num']!="") {
    
$dni=$_POST['dni_num'];
    
$_SESSION['dni']=$dni;
}

//si se define marca
if(isset($_POST['marca_campana']) && $_POST['marca_campana']!="") {
    
$marca=$_POST['marca_campana'];
    
$_SESSION['marca']=$marca;
}

//si se define modelo
if(isset($_POST['modelo']) && $_POST['modelo']!="") {
    
$modelo=$_POST['modelo'];
    
$_SESSION['modelo']=$modelo;
}

//si se define averia
if(isset($_POST['averia']) && $_POST['averia']!="") {        
    
$averia=$_POST['averia'];    
    
$_SESSION['averia']=$averia;
}

//si se define garantia
if(isset($_POST['garantia']) && $_POST['garantia']!="") {
    
$garantia=$_POST['garantia'];
    
$_SESSION['garantia']=$garantia;
}

//si se define fecha de compra
if(isset($_POST['fecha_compra']) && $_POST['fecha_compra']!="") {
    
$fecha_compra=$_POST['fecha_compra'];
    
$_SESSION['fecha_compra']=$fecha_compra;
    
//La fecha de compra esta mal introducida
    
if(strlen($fecha_compra) < 10){
        echo 
"La fecha de compra debe tener el siguiente formato: DD/MM/AAAA";
        echo 
$vinculo;
        exit;
    }
}

//si se define comercio
if(isset($_POST['comercio']) && $_POST['comercio']!="") {
    
$comercio=$_POST['comercio'];
    
$_SESSION['comercio']=$comercio;
}

//
//ENVIAR EL CORREO
//

ini_set("SMTP","smtp.1and1.es");
$para="[email protected]";
$asunto="Consulta/Aviso";
$header='MIME-Version: 1.0' "\r\n";
$header='From: '.$email.'' "\r\n";
$header.='Content-type: text/html; charset=iso-8859-1' "\r\n";
$mensaje='
<html>
<head>
<style type="text/css">
body{ background-color:black; color:#ff3e3e }
</style>
</head>

<body>
<table width="100%" border="0">
  <tr>
    <td colspan="2"><div align="center">DATOS DEL USUARIO</div></td>
  </tr>
  <tr>
    <td width="12%">Nombre:</td>
    <td width="88%">'
.$nombre.'</td>
  </tr>
  <tr>
    <td>Apellidos:</td>
    <td>'
.$apellido.'</td>
  </tr>
  <tr>
    <td>Tel&eacute;fono:</td>
    <td>'
.$telefono.'</td>
  </tr>
  <tr>
    <td>M&oacute;vil:</td>
    <td>'
.$movil.'</td>
  </tr>
  <tr>
    <td>E-Mail:</td>
    <td>'
.$email.'</td>
  </tr>
  <tr>
    <td>Provincia:</td>
    <td>'
.$provincia.'</td>
  </tr>
  <tr>
    <td>Poblaci&oacute;n:</td>
    <td>'
.$poblacion.'</td>
  </tr>
  <tr>
    <td>Direcci&oacute;n:</td>
    <td>'
.$direccion.'</td>
  </tr>
  <tr>
    <td>C&oacute;digo Postal:</td>
    <td>'
.$codigo_postal.'</td>
  </tr>
  <tr>
    <td>DNI:</td>
    <td>'
.$dni.'</td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">DATOS DEL APARATO</div></td>
  </tr>
  <tr>
    <td>Marca:</td>
    <td>'
.$marca.'</td>
  </tr>
  <tr>
    <td>Modelo:</td>
    <td>'
.$modelo.'</td>
  </tr>
  <tr>
    <td><p>Aver&iacute;a:</p></td>
    <td>'
.$averia.'</td>
  </tr>
  <tr>
    <td>Garant&iacute;a:</td>
    <td>'
.$garantia.'</td>
  </tr>
  <tr>
    <td>Fecha de Compra:</td>
    <td>'
.$fecha_compra.'</td>
  </tr>
  <tr>
    <td>Comercio:</td>
    <td>'
.$comercio.'</td>
  </tr>
</table>
</body>
</html>
'
;
try
{
    
mail($para,$asunto,$mensaje,$header)or die("\n\nError al intentar enviar el mensaje\n\n");
}
catch(
Exception $e)
{
    echo 
'Error de ejecucion: '$e->getMessage(), "\n";
}
?>
<body>
�Tu mensaje ha sido enviado con &eacute;xito!<br /> Haz click <a href="index.php" target="principal">aqu&iacute;</a> para volver a la p&aacute;gina principal.
<?php unset($_SESSION['nombre'],$_SESSION['apellido'],$_SESSION['email'],$_SESSION['provincia'],$_SESSION['poblacion'],$_SESSION['telefono'],$_SESSION['movil'],$_SESSION['averia'],$_SESSION['codigo_postal'],$_SESSION['dni'],$_SESSION['marca'],$_SESSION['modelo'],$_SESSION['fecha_compra'],$_SESSION['comercio'],$_SESSION['garantia']); ?>
</body>
</html>
Espero que si tienen alguna solucion puedan decirmela porque me lleva de cabeza esto y quiero solucionarlo.
Gracias de antemano.
  #3 (permalink)  
Antiguo 03/09/2012, 13:21
 
Fecha de Ingreso: mayo-2006
Mensajes: 475
Antigüedad: 17 años, 11 meses
Puntos: 58
Respuesta: Enviar correo con tildes y ñ

El form y la pagina de envio estan en utf_8 pero el mail lo estas enviando en ISO 8859-1.
Siempre puedes usar tambien utf8_decode();
  #4 (permalink)  
Antiguo 03/09/2012, 13:58
Avatar de legomolina  
Fecha de Ingreso: junio-2011
Ubicación: Valencia
Mensajes: 165
Antigüedad: 12 años, 10 meses
Puntos: 3
Respuesta: Enviar correo con tildes y ñ

Muchisimas gracias de verdad, como no me he fijado en lo del formulario?
Eternamente agradecido.

Etiquetas: correo, formulario, html, tildes, variables, usuarios
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 04:36.