Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCESAR SOTO
3,687 PointsSEND AN ATTACHMENT EMAIL WITH PHP
Hello everybody !
Can somebody tell me how can I send an email with attachment file with PHP code to gmail . I have tried but i cant see the file , its corrupt but in hotmail I can see it .
Thanks .
6 Answers
CESAR SOTO
3,687 PointsThanks Tom and ghost code forma tour comments , I could resolve the code bug , I checked my code line per line as gosth code told me until I understood was a sintax headers bug .
Sue Dough
35,800 PointsThat is strange. It works fine. Do you mind showing the code your sending?
CESAR SOTO
3,687 PointsOf course
<?php
class correo{
function correoConAdjunto($direccionDe,$direccionPara,$titulo,$mensajeEncabezado,$mensajeCuerpo,$mensajeFinal,$rutaArchivo,$nombreArchivo){
$archivoAdjunto_nombre = $nombreArchivo;
$archivoAdjunto_ruta = $rutaArchivo.$archivoAdjunto_nombre;
$archivoAdjunto_tipo = "application/octet-stream";
$correo_de = $direccionDe;
$correo_titulo = $titulo;
$correo_mensaje = $mensajeEncabezado."<br />";
$correo_mensaje .= $mensajeCuerpo."<br />";
$correo_mensaje .= $mensajeFinal;
$correo_para = $direccionPara;
$encabezados = "From: ".$correo_de;
$archivo = fopen($archivoAdjunto_ruta,"rb");
$dato = fread($archivo,filesize($archivoAdjunto_ruta));
fclose($archivo);
$aleatorio = md5(time());
$mime_boundary = "==Multipart_Boundary_x".$aleatorio."x";
$encabezados .= "\nMIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"$mime_boundary\"";
$correo_mensaje_head = "This is a multi-part message in MIME format.\n\n".
"--$mime_boundary\n".
"Content-Type: text/html; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n".
"$correo_mensaje\n\n";
$dato = chunk_split(base64_encode($dato));
$correo_mensaje_head .= "--$mime_boundary\n".
"Content-Type: $archivoAdjunto_tipo; name=\"$archivoAdjunto_nombre\"\n".
"Content-Description: $archivoAdjunto_nombre\n".
"Content-Disposition: attachment;\n".
"filename=\"$archivoAdjunto_nombre\"\n".
"Content-Transfer-Encoding: base64\n\n".
"$dato\n\n".
"--$mime_boundary--\n";
if(@mail($correo_para,$correo_titulo,$correo_mensaje_head,$encabezados)){
return true;
}
}
}
?>
Sue Dough
35,800 PointsIt may be because its something to do with mail. I would do this. Send a php file with hello world and see if you can receive that just so we can make sure its isolated to this file. Then I would take take out a few lines each time until you find the culprit.
CESAR SOTO
3,687 PointsBut Its strange because if i send it a hotmail account i can see it but a gmail account i cant .
thomascawthorn
22,986 PointsHave you tried implementing phpMailer? I notice you're reading your attachments from a file, so you should be able to let phpMailer do the heavy lifting for you.
CESAR SOTO
3,687 PointsNo i havent tried that option yet , Im trying to find the bug of my code , i dont know if can anybody show me a functional example code .
Thanks .