Como puedo mandar un mail con archivos adjuntos
Estoy desarrollando una aplicaci贸n y estoy teniendo algunos problemillas con mandar mails en php con archivos adjuntos (pdf, zip...)
Alguien me podria ayudar??
Alguien me podria ayudar??
Primero hay q subir el fichero al servidor:
//subir fichero adjunto al servidor
if ($archivo != "none" AND $archivo_size != 0){
if (copy ($archivo, $archivo_name)) {
echo "Se ha transferido el archivo $archivo_name";
}
}else{
echo "No ha podido transferirse el fichero";
}
Lueg enviarlo:
$asunto = "Mandar adjuntos";
$mensaje='<html>...</html> ';
$remite="[email protected]";
$separador = "_separador_de_trozos_".md5 (uniqid (rand()));
//para el env铆o en formato HTML
$headers = "Date: ".date("l j F Y, G:i").$UN_SALTO;
$headers .= "MIME-Version: 1.0".$UN_SALTO;
//direcci贸n del remitente
$headers .= "From: Jorge <[email protected]>".$UN_SALTO;
//direcciones que recibir谩n copia
$headers .= "Cc: [email protected]".$UN_SALTO;
//direcciones que recibir谩n copia oculta
$headers .= "Bcc: [email protected]".$UN_SALTO;
$headers .= "Return-path: ". $remite.$UN_SALTO;
$headers .= "Reply-To: ".$remite.$UN_SALTO;
$headers .= "X-MAILER:PHP/".phpversion().$UN_SALTO;
$headers .= "Content-Type: multipart/mixed;".$UN_SALTO;
$headers .= " boundary=$separador".$DOS_SALTOS;
//Mensaje en formato HTML
# Separador inicial
$cuerpo ="--$separador".$UN_SALTO;
# Encabezado parcial
$cuerpo .="Content-Type: text/html; charset="ISO-8859-1"".$UN_SALTO;
$cuerpo .="Content-Transfer-Encoding: 7bit".$DOS_SALTOS;
# Contenido de esta parte del mensaje
$cuerpo .= $mensaje;
# Separador de partes
$adj1 = $UN_SALTO."--$separador".$UN_SALTO;
// Fichero adjunto
# Encabezado parcial
$nombref=$HTTP_POST_FILES['archivo']['name'];
$adj1 .="Content-Type: application/octet-stream; name="$nombref"".$UN_SALTO;
$adj1 .="Content-Disposition: attachment; filename="$nombref"".$UN_SALTO;
$adj1 .="Content-Transfer-Encoding: base64".$DOS_SALTOS;
# lectura del fichero adjunto
$fp = fopen("$nombref", "r");
$buff = fread($fp, filesize("$nombref"));
fclose($fp);
# codificaci贸n del fichero adjunto
$adj1 .=chunk_split(base64_encode($buff));
// separador final del mensaje
$adj1 .=$UN_SALTO."--$separador".$UN_SALTO;
y q se ejecute:
// Uni贸n de las diferentes partes para crear el cuerpo del mensaje
$mensaje=$cuerpo.$adj1.$adj2.$adj3.$adj4;
mail($destinatario,$asunto,$mensaje,$headers);
echo "<br>Mensaje enviado.";
}
//subir fichero adjunto al servidor
if ($archivo != "none" AND $archivo_size != 0){
if (copy ($archivo, $archivo_name)) {
echo "Se ha transferido el archivo $archivo_name";
}
}else{
echo "No ha podido transferirse el fichero";
}
Lueg enviarlo:
$asunto = "Mandar adjuntos";
$mensaje='<html>...</html> ';
$remite="[email protected]";
$separador = "_separador_de_trozos_".md5 (uniqid (rand()));
//para el env铆o en formato HTML
$headers = "Date: ".date("l j F Y, G:i").$UN_SALTO;
$headers .= "MIME-Version: 1.0".$UN_SALTO;
//direcci贸n del remitente
$headers .= "From: Jorge <[email protected]>".$UN_SALTO;
//direcciones que recibir谩n copia
$headers .= "Cc: [email protected]".$UN_SALTO;
//direcciones que recibir谩n copia oculta
$headers .= "Bcc: [email protected]".$UN_SALTO;
$headers .= "Return-path: ". $remite.$UN_SALTO;
$headers .= "Reply-To: ".$remite.$UN_SALTO;
$headers .= "X-MAILER:PHP/".phpversion().$UN_SALTO;
$headers .= "Content-Type: multipart/mixed;".$UN_SALTO;
$headers .= " boundary=$separador".$DOS_SALTOS;
//Mensaje en formato HTML
# Separador inicial
$cuerpo ="--$separador".$UN_SALTO;
# Encabezado parcial
$cuerpo .="Content-Type: text/html; charset="ISO-8859-1"".$UN_SALTO;
$cuerpo .="Content-Transfer-Encoding: 7bit".$DOS_SALTOS;
# Contenido de esta parte del mensaje
$cuerpo .= $mensaje;
# Separador de partes
$adj1 = $UN_SALTO."--$separador".$UN_SALTO;
// Fichero adjunto
# Encabezado parcial
$nombref=$HTTP_POST_FILES['archivo']['name'];
$adj1 .="Content-Type: application/octet-stream; name="$nombref"".$UN_SALTO;
$adj1 .="Content-Disposition: attachment; filename="$nombref"".$UN_SALTO;
$adj1 .="Content-Transfer-Encoding: base64".$DOS_SALTOS;
# lectura del fichero adjunto
$fp = fopen("$nombref", "r");
$buff = fread($fp, filesize("$nombref"));
fclose($fp);
# codificaci贸n del fichero adjunto
$adj1 .=chunk_split(base64_encode($buff));
// separador final del mensaje
$adj1 .=$UN_SALTO."--$separador".$UN_SALTO;
y q se ejecute:
// Uni贸n de las diferentes partes para crear el cuerpo del mensaje
$mensaje=$cuerpo.$adj1.$adj2.$adj3.$adj4;
mail($destinatario,$asunto,$mensaje,$headers);
echo "<br>Mensaje enviado.";
}