Estoy tratando de enviar un email con un adjunto, pero necesito que el adjunto solo sea doc o pdf.. Como lo puedo hacer??
Código HTML :
<!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>Ejemplo Enviar Adjunto Email - 1</title> </head> <body> <form name='formulario' id='formulario' method='post' action='enviar_mail.php' enctype="multipart/form-data"> <p>Nombre <input type='text' name='Nombre' id='Nombre'></p> <p>Apellidos <input type='text' name='Apellidos' id='Apellidos'></p> <p>E-mail <input type='text' name='E-mail' id='E-mail'></p> <p><input type='radio' value='Hombre' name='Sexo' id='Sexo'>Hombre</p> <p><input type='radio' value='Mujer' name='Sexo' id='Sexo'>Mujer</p> <p>Adjuntar archivo1: <input type='file' name='archivo1' id='archivo1'></p> <p>Adjuntar archivo2: <input type='file' name='archivo2' id='archivo2'></p> <p align='center'> <input type='submit' value='Enviar formulario'> <input type='reset' value='resetear formulario'> </p> </form> </body> </html>
Código PHP :
<?php
function form_mail($sPara, $sAsunto, $sTexto, $sDe)
{
$bHayFicheros = 0;
$sCabeceraTexto = "";
$sAdjuntos = "";
$sCuerpo = $sTexto;
$sSeparador = uniqid("_Separador-de-datos_");
$sCabeceras = "MIME-version: 1.0\n";
// Recogemos los campos del formulario
foreach ($_POST as $sNombre => $sValor)
$sCuerpo = $sCuerpo."\n".$sNombre." = ".$sValor;
// Recorremos los Ficheros
foreach ($_FILES as $vAdjunto)
{
if ($bHayFicheros == 0)
{
// Hay ficheros
$bHayFicheros = 1;
// Cabeceras generales del mail
$sCabeceras .= "Content-type: multipart/mixed;";
$sCabeceras .= "boundary=\"".$sSeparador."\"\n";
// Cabeceras del texto
$sCabeceraTexto = "--".$sSeparador."\n";
$sCabeceraTexto .= "Content-type: text/plain;charset=iso-8859-1\n";
$sCabeceraTexto .= "Content-transfer-encoding: 7BIT\n\n";
$sCuerpo = $sCabeceraTexto.$sCuerpo;
}
// Se añade el fichero
if ($vAdjunto["size"] > 0)
{
$sAdjuntos .= "\n\n--".$sSeparador."\n";
$sAdjuntos .= "Content-type: ".$vAdjunto["type"].";name=\"".$vAdjunto["name"]."\"\n";
$sAdjuntos .= "Content-Transfer-Encoding: BASE64\n";
$sAdjuntos .= "Content-disposition: attachment;filename=\"".$vAdjunto["name"]."\"\n\n";
$oFichero = fopen($vAdjunto["tmp_name"], 'rb');
$sContenido = fread($oFichero, filesize($vAdjunto["tmp_name"]));
$sAdjuntos .= chunk_split(base64_encode($sContenido));
fclose($oFichero);
}
}
// Si hay ficheros se añaden al cuerpo
if ($bHayFicheros)
$sCuerpo .= $sAdjuntos."\n\n--".$sSeparador."--\n";
// Se añade la cabecera de destinatario
if ($sDe)$sCabeceras .= "From:".$sDe."\n";
// Por último se envia el mail
return(mail($sPara, $sAsunto, $sCuerpo, $sCabeceras));
}
//Ejemplo de como usar:
if (form_mail("[email protected]",
"Test Attach - v1",
"Los datos introducidos en el formulario son:\n",
"[email protected]"))
echo "Su formulario ha sido enviado con exito";
?>
Desde ya muchas gracias!!!
Saludos
