2

Jul


Formulario de envío de correos con Archivos Adjuntos

Autor: David

Hace poco tuve que hacer un sistema que me permitiera enviar archivos adjuntos en un correo enviado desde la web. Hay muchas webs donde hay ejemplos para hacer esto, yo encontré este, no sé exactamente donde pero ahí va.

Primero la función de envío.

PHP:
  1. function form_mail($sPara, $sAsunto, $sTexto, $sDe)
  2. {
  3.     $bHayFicheros = 0;
  4.     $sCabeceraTexto = "";
  5.     $sAdjuntos = "";
  6.    
  7.     if ($sDe)$sCabeceras = "From:".$sDe."\n";
  8.     else $sCabeceras = "";
  9.     $sCabeceras .= "MIME-version: 1.0\n";
  10.    
  11.     foreach ($_FILES as $vAdjunto)
  12.     {
  13.         if ($bHayFicheros == 0)
  14.         {
  15.         $bHayFicheros = 1;
  16.         $sCabeceras .= "Content-type: multipart/mixed;";
  17.         $sCabeceras .= "boundary=\"--_Separador-de-mensajes_--\"\n";
  18.        
  19.         $sCabeceraTexto = "----_Separador-de-mensajes_--\n";
  20.         $sCabeceraTexto .= "Content-type: text/plain;charset=iso-8859-1\n";
  21.         $sCabeceraTexto .= "Content-transfer-encoding: 7BIT\n";
  22.        
  23.         $sTexto = $sCabeceraTexto.$sTexto;
  24.         }
  25.        
  26.         if($vAdjunto["size"]> 1048576){
  27.         return false;
  28.         }
  29.        
  30.         if ($vAdjunto["size"]> 0)
  31.         {
  32.         $sAdjuntos .= "\n\n----_Separador-de-mensajes_--\n";
  33.         $sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";;
  34.         $sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
  35.         $sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";
  36.        
  37.         $oFichero = fopen($vAdjunto["tmp_name"], 'r');
  38.         $sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
  39.         $sAdjuntos .= chunk_split(base64_encode($sContenido));
  40.         fclose($oFichero);
  41.         }
  42.     }
  43.    
  44.     if ($bHayFicheros)
  45.     $sTexto .= $sAdjuntos."\n\n----_Separador-de-mensajes_----\n";
  46.     return(mail($sPara, $sAsunto, $sTexto, $sCabeceras));
  47. }

Luego el formulario que recoge los datos

HTML:
  1. <form name="formulario" action="enviar.php" method="post" enctype="multipart/form-data">
  2. <input type="text" name="nombre"> Nombre<br />
  3. <input type="text" name="email"> E-mail<br />
  4. <input type="text" name="asunto"> Asunto<br />
  5. <br /><br />
  6. Comentarios<br />
  7. <textarea name="comentarios"></textarea><br /><br />
  8. <input type="file" name="adjunto"> Archivo adjunto<br />
  9. <input type="submit" name="submit" value="Enviar">
  10. </form>

Por último el código que procesa el formulario.

PHP:
  1. if($_POST['submit']){
  2.     $namefrom=$_POST['nombre'];
  3.     $emailfrom='admin@dominio.com';
  4.     $emailto=$_POST['email'];
  5.     $subject=$_POST['asunto'];
  6.     $mensaje = "\nNombre: ".$namefrom."\n\n".$_POST['comentarios'];
  7.     //llamamos a la funcion de envio
  8.     form_mail($emailto,$subject, $mensaje, $emailfrom);
  9. }

Salu2



1 Comment Send your comment

  1.   wow gold

    I know some wow gold in wow.


Leave a Reply