Ver Mensaje Individual
  #4 (permalink)  
Antiguo 29/10/2008, 03:35
Avatar de mgusdl
mgusdl
 
Fecha de Ingreso: abril-2007
Ubicación: Malaga, España
Mensajes: 190
Antigüedad: 17 años, 10 meses
Puntos: 5
Respuesta: Adjuntar archivo en mail PHP

Yo me he currado un formulario que envia un adjunto por email, con ayuda de los comentarios en php.net:

Código HTML:
<script type="text/javascript">
<!--
function envia() {
	var frm = document.forms.contacta;
	var emfilter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var telfilter = /^([689]{1})+([0-9]{8})+$/;
	if (frm.dept.value == '') { alert('Elija un departamento'); return false; }
	if (frm.nombre.value == '' || frm.nombre.value == 'Introduzca su nombre') { alert('Introduzca su nombre'); return false; }
	if (!emfilter.test(frm.mail.value )) { alert('Introduzca una dirección de correo electrónico'); return false; }
	if (frm.tel.value != '' && !telfilter.test(frm.tel.value )) { alert('Introduzca un teléfono correcto'); return false; }
	if (frm.mensaje.value == '' || frm.mensaje.value == 'Escriba su consulta') { alert('Escriba el mensaje'); return false; }
	return true;
	}
//-->
</script>
<form method="post" name="contacta" action="mailpost.php" enctype="multipart/form-data" onSubmit="return envia()">
<table summary="Formulario de contacto">
  <tr>
    <td colspan="2"><strong>Env&iacute;anos un mensaje</strong> a trav&eacute;s del siguiente formulario de contacto:</td>
  <tr>
    <td><label for="dept">Departamento al que<br>dirige su consulta<sup>*</sup>:</label></td>
    <td>
      <select name="dept" id="dept">
        <option value="">&nbsp;</option>
        <option value="1">Departamento 1</option>
        <option value="2">Departamento 2</option>
        <option value="3">Departamento 3</option>
      </select>
    </td>
  </tr><tr>
    <td><label for="nombre">Nombre<sup>*</sup>:</label></td>
    <td><input type="text" name="nombre" id="nombre"></td>
  </tr><tr>
    <td><label for="mail">e-mail:</label></td>
    <td><input type="text" name="mail" id="mail"></td>
  </tr><tr>
    <td ><label for="tel">Tel&eacute;fono de contacto:</label></td>
    <td><input type="text" name="tel" id="tel" size="10" maxlength="9"></td>
  </tr><tr>
    <td><label for="mensaje">Mensaje:</label></td>
    <td><textarea rows="5" cols="30" name="mensaje" id="mensaje"></textarea></td>
  </tr><tr>
    <td><label for="attach">Adjuntar archivo:</label></td>
    <td><input type="file" name="attach" id="attach"> (m&aacute;ximo 2MB)</td>
  </tr><tr>
    <td colspan="2">
      <input type="submit" value="ENVIAR"> -
      <input type="reset" value="LIMPIAR"></td>
  </tr>
</table>
</form> 
Código PHP:
if ($_POST) {
  
$para "[email protected]";
  switch (
$_POST['dept']) {
    case 
1$para .= ", [email protected]"; break;
    case 
2$para .= ", [email protected]"; break;
    case 
3$para .= ", [email protected]"; break;
    }
  
$asunto "Mensaje desde la Web [example.com]";

  
$mensaje "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n"
  
"<html>\n<head>\n<style type=\"text/css\">td { font-family:Verdana; font-size:10px;}</style>\n</head>\n"
  
"<body>\n<div style=\"width:780px;\"><table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\" align=\"left\">\n"
  
"<tr>\n<td width=\"20%\">Nombre:</td>\n<td><span style=\"font-weight:bold;\">{$_POST['nombre']}</span></td>\n</tr>\n"
  
"<tr><td>Tel&eacute;fono:</td><td><span style=\"font-weight:bold;\">{$_POST['tel']}</span></td></tr>\n"
  
"<tr><td>e-mail:</td><td><span style=\"font-weight:bold;\">{$_POST['mail']}</span></td></tr>\n"
  
"<tr><td valign=\"top\">Mensaje:</td><td>".str_replace("\n""<br>"$_POST['mensaje'])."</td></tr>\n"
  
"<tr><td colspan=\"2\"><hr></td></tr>\n"
  
"<tr><td>IP:</td><td><span style=\"font-weight:bold;\">{$_SERVER['REMOTE_ADDR']}</span></td></tr>\n"
  
"<tr><td>Fecha/Hora:</td><td><span style=\"font-weight:bold;\">".date("d/m/Y H:i"time())."</span></td></tr>\n"
  
"<tr><td colspan=\"2\"><hr></td></tr>\n"
  
"<tr><td colspan=\"2\" style=\"font-size:xx-small\">No responda a este mensaje ya que es enviado por un servicio autom&aacute;tico</td></tr>\n"
  
"</table>\n</div>\n</body>\n</html>";
  
$hd "From: Formulario Web <[email protected]>\r\n"
  
"Reply-To: Formulario Web <[email protected]>\r\n"
  
"Return-Path: Formulario Web <[email protected]>\r\n"
  
"Message-ID: <".time()."[email protected]>\r\n"
  
"X-Mailer: PHP v".phpversion()."\r\n"
  
"MIME-Version: 1.0\n"
  
"Content-type: text/html; charset=iso-8859-1\r\n";

  if (
$_FILES && is_uploaded_file($_FILES['attach']['tmp_name']))
    {
    
$filename $_FILES['attach']['name'];
    
$filemime $_FILES['attach']['type'];
    
$filsize =  $_FILES['attach']['size'];
    
$fileatt $_FILES['attach']['tmp_name'];
    
$file fopen($fileatt,'rb');
    
$data fread($filefilesize($fileatt));
    
fclose($file);
    
$data chunk_split(base64_encode($data));
    
$uid md5(uniqid(time()));

    
$hd "From: Formulario Web <[email protected]>\r\n"
    
"Reply-To: Formulario Web <[email protected]>\r\n"
    
"Return-Path: Formulario Web <[email protected]>\r\n"
    
"Message-ID: <".time()."[email protected]>\r\n"
    
"X-Mailer: PHP v".phpversion()."\r\n";

    
$hd .= "MIME-Version: 1.0\r\n\r\n"
    
"Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n";

    
$msg "--".$uid."\r\n"
    
"Content-Type: multipart/alternative; boundary=\"".$uid."_alt_\"\r\n\r\n";

    
$msg .= "--".$uid."_alt_\r\n"
    
"Content-Type: text/plain; charset=iso-8859-1\r\n"
    
"Content-Transfer-Encoding: 8bit\r\n\r\n"
    
strip_tags(str_replace("<br>""\n"substr($mensaje, (strpos($mensaje"<body>")+6))))."\r\n\r\n";

    
$msg .= "--".$uid."_alt_\r\n"
    
"Content-Type: text/html; charset=iso-8859-1\r\n"
    
"Content-Transfer-Encoding: 8bit\r\n\r\n"
    
$mensaje."\r\n\r\n";

    
$msg .= "--".$uid."_alt_--\r\n\r\n";

    
$msg .= "--".$uid."\r\n"
    
"Content-Type:".$filemime."; name=\"".$filename."\"\r\n"
    
"Content-Transfer-Encoding: base64\r\n"
    
"Content-Description: ".$filename."\r\n"
    
"Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
    
$data."\r\n\r\n"
    
"--".$uid."--\r\n\r\n";

    
$mensaje $msg;
    }

  if(
mail($para$asunto$mensaje $hd)) echo "Mensaje enviado";
  else echo 
"Mensaje NO enviado";

Espero que sirva de ayuda