Jump to content

[SOLVED] Email with attachment corrupts file


kristo5747

Recommended Posts

Hello!

 

I am a newbie who's trying to code in my app a way to send email with file attachments. Here's the code I put together (thanks to Google!).

 

<?php
$fileatt = "/somedir"; 
$fileatt_type = "application/zip";
$fileatt_name = "logos.zip"; 

//$fileatt_type = "image/gip";
//$fileatt_name = "logo.gif"; 


$email_from = "joe@work_email.com"; 
$email_subject = "attachment test-subject"; 
$email_txt = "attachment test-body"; 

$email_to = "[email protected]";

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "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" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

mail($email_to, $email_subject, $email_message, $headers);

?>

 

My dev box runs Linux and sendmail. The recipient ("joe@work_email.com") runs MS Windows and Outlook.

 

The code works and sends an email to the recipient. The problem is that the attached file is corrupted (read, empty).  I sent gif and zip files as attachments: MS Outlook `sees` the files as gif or zip but the size is 0/zero bytes.

 

The weird part is that, using mutt on my dev box, I was able to successfully send GIF & ZIP as attachements.

 

Is it because the header is mal-formed in my code? What am I doing wrong here?

 

Thanks for helping.

 

Al.

Your fopen() is failing (and your code has no error checking logic in it to tell you or to prevent the remainder of the code from executing) because the file name parameter you passed it is not a filename, but a folder. You must always check if a function works before blindly attempting to use data from that function.

 

Add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

Thanks for your reply. I figured out what I was doing wrong.

 

a) I was passing a directory name ($fileatt) to fopen()

b) I was not trapping any errors

c) I was not using relative paths so the file could not be found. As soon as I moved the file to the directory where my code is, all worked as expected.

 

Php newbie + new dad + zero sleep for the next 6 months = bad combination.

 

Thanks for kicking me back awake. ;D

 

FYI. this is the corrected code. Not perfect but gets the job done.

 

<?php

$fileatt = "upload/";
$fileatt_type =  "image/gif";
$fileatt_name = $fileatt. "dtv.gif";

$email_from = "joe@work_email.com";
$email_subject = "attachment test-subject";
$email_txt = "attachment test-body";

$email_to = "[email protected]";

$headers = "From: ".$email_from;

$file = fopen($fileatt_name,'rb');
$data = fread($file,filesize($fileatt_name));
fclose($file);

$fileatt_name = substr($fileatt_name, strlen($fileatt));

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "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" .
$email_message . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if ($ok) {
    echo "<p>mail sent!</p>";
} else {
    echo "<p>mail could not be sent!</p>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.