PHP: Enviar emails con archivos adjuntos

Hola,

Una de las cosas por las que me preguntan de vez en cuando es cómo enviar desde PHP un email con ficheros adjuntos.

Pues os he preparado un ejemplo muy simple con formulario y validación de tipo de ficheros incluída.


<?

/*
Script para enviar un fichero adjunto con formulario y validación de tipo de fichero

URL: http://www.gabrielcuesta.com
Blog: http://gabicuesta.blogspot.com
*/

if($_POST[submited]){

$emailDestination = "gabicuesta@hotmail.com";
$emailOrigin="gabicuesta@ono.com";
$nameEmailOrigin = "Gabriel";
$subject=$_POST[subject];
$message=$_POST[message];

$docList = 'image/gif,image/jpeg,image/jpg,image/pjpeg,image/x-png,application/msword,application/rtf,text/plain,application/octet-stream';

if (is_uploaded_file($_FILES['file1']['tmp_name']) && eregi($_FILES['file1']['type'],$docList)){
$nameFile = $_FILES['file1']['tmp_name'];
$file = fopen($nameFile, "r");
$content = fread($file, filesize($nameFile));
$encoded_attach = chunk_split(base64_encode($content));
fclose($file);
}

$headers = "From: ". $nameEmailOrigin . " <". $emailOrigin .">\n";
$headers .= "Reply-To: $emailDestination\n";
$headers .= "MIME-version: 1.0\n";
$headers .= "Content-type: multipart/mixed; ";
$headers .= "boundary=\"Message-Boundary\"\n";
$headers .= "Content-transfer-encoding: 7BIT\n";
$headers .= "X-attachments: ". $_FILES['file1']['name'];

$body_top = "--Message-Boundary\n";
$body_top .= "Content-type: text/plain; charset=US-ASCII\n";
$body_top .= "Content-transfer-encoding: 7BIT\n";
$body_top .= "Content-description: Mail message body\n\n";

$body = $body_top.$message;

if (is_uploaded_file($_FILES['file1']['tmp_name']) && eregi($_FILES['file1']['type'],$docList)){
$body .= "\n\n--Message-Boundary\n";
$body .= "Content-type: Binary; name=\"". $_FILES['file1']['name'] ."\"\n";
$body .= "Content-Transfer-Encoding: BASE64\n";
$body .= "Content-disposition: attachment; filename=\"". $_FILES['file1']['name'] ."\"\n\n";
$body .= "$encoded_attach\n";
$body .= "--Message-Boundary--\n";
}

if(mail($emailDestination,$subject,$body,$headers)){
echo("Your email has been correclty submited.<br/><br/>");
if(!eregi($_FILES['file1']['type'],$docList)){
echo("<br/><br/>Your attachment isn't an allowed file type, it hasn't been sent.<br/><br/>");
echo("<hr><br/><br/>");
}
}else{
echo("There have been problems sending your email, please, try later.<br/><br/>");
}

}
?>

Send and email attaching a file:
<br />
<br />
<form action="testEmail.php" method="post" enctype="multipart/form-data">
Subject: <input type="text" name="subject" />
<br />
<br />
Message:
<textarea name="message"></textarea>
<br />
<br />
File to attach: <input type="file" name="file1" />
<br />
<br />
<input type="hidden" name="submited" value="true" />
<input type="submit" />


</form>



Blogger no respeta las tabulaciones así que siento que el código sea en apariencia tan desordenado :(

Que os aproveche y cualquier duda o modificación que queráis me dáis un toque por email (gabicuesta@ono.com) o por messenger (gabicuesta@hotmail.com).


Comparte este Post:
Menéame Digg!

Comentarios

Unknown ha dicho que…
Gracias
Anónimo ha dicho que…
Excelente, muchas gracias!
Unknown ha dicho que…
t pasast man probe con varios scripts pero este me funko d maravilla no se si aun sigas usando tu mismo mail podrias avisar si lo cambiast el mio es aniquilacion_2004@hotmail.com