By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,844 Members | 1,918 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,844 IT Pros & Developers. It's quick & easy.

corrupt file sending email attachment

P: n/a
Hi,

I am sending a file from the server as an email attachment. The file
is being attached no problem and sending the email, but I get an error
when I try to open it saying it is corrupt. Obviuosly, the file is
fine on the server, so the attachment code I am using must be
corrupting it, but I dont know what it is:

// send email with attachment
function emailAttachment($to, $subject, $message, $name, $email,
$file) {
$fp = fopen($file, "rb");
$fcontent = fread($fp, filesize($file));
fclose($fp);
$data = chunk_split(base64_encode($fcontent));
$mime_boundary = md5(uniqid(time()));
$file_name = basename($file);
$header = "From: " . $name . "<" . $email . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"" . $mime_boundary . "\"\r\n";
$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content .= $message . "\r\n\r\n";
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Type: image/jpeg; name=\"" . $file_name .
"\"\r\n";
$content .= "Content-Transfer-Encoding: base64\r\n";
$content .= "Content-Disposition: attachment; filename=\"" .
$file_name . "\"\r\n\r\n";
$content .= $data . "\r\n";
$content .= "--" . $mime_boundary . "--";
if (mail($to, $subject, $content, $header)) {
return true;
} else {
return false;
}
}

I am receiving the email on a Mac, will this cause any errors?
I look forward to any help.

Paul
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello,

On 05/26/2004 12:31 PM, Paul Lamonby wrote:
Hi,

I am sending a file from the server as an email attachment. The file
is being attached no problem and sending the email, but I get an error
when I try to open it saying it is corrupt. Obviuosly, the file is
fine on the server, so the attachment code I am using must be
corrupting it, but I dont know what it is:


It is hard to tell. It seems correct.

You may want to try this throughly tested class that also supports
sending messages with attachments to see if you experience the same
problem. Just let me know if your problem is not solved:

http://www.phpclasses.org/mimemessage

--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #2

P: n/a
pa**@popimages.com (Paul Lamonby) wrote in message news:<86**************************@posting.google. com>...
Hi,

I am sending a file from the server as an email attachment. The file
is being attached no problem and sending the email, but I get an error
when I try to open it saying it is corrupt.

<snip>

Perhaps you should try with <http://phpmailer.sourceforge.net/>

--
| Just another PHP saint |
Email: rrjanbiah-at-Y!com
Jul 17 '05 #3

P: n/a
Dunno much about sending email attachments, but maybe it is a character
encoding problem. What type of character encoding does base64_encode()
return? In your header, you seem to be assuming it's western latin.
Maybe it's not.

have you tried removing chunk_split()?

Paul Lamonby wrote:
Hi,

I am sending a file from the server as an email attachment. The file
is being attached no problem and sending the email, but I get an error
when I try to open it saying it is corrupt. Obviuosly, the file is
fine on the server, so the attachment code I am using must be
corrupting it, but I dont know what it is:

// send email with attachment
function emailAttachment($to, $subject, $message, $name, $email,
$file) {
$fp = fopen($file, "rb");
$fcontent = fread($fp, filesize($file));
fclose($fp);
$data = chunk_split(base64_encode($fcontent));
$mime_boundary = md5(uniqid(time()));
$file_name = basename($file);
$header = "From: " . $name . "<" . $email . ">\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed;\r\n";
$header .= " boundary=\"" . $mime_boundary . "\"\r\n";
$content = "This is a multi-part message in MIME format.\r\n\r\n";
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$content .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$content .= $message . "\r\n\r\n";
$content .= "--" . $mime_boundary . "\r\n";
$content .= "Content-Type: image/jpeg; name=\"" . $file_name .
"\"\r\n";
$content .= "Content-Transfer-Encoding: base64\r\n";
$content .= "Content-Disposition: attachment; filename=\"" .
$file_name . "\"\r\n\r\n";
$content .= $data . "\r\n";
$content .= "--" . $mime_boundary . "--";
if (mail($to, $subject, $content, $header)) {
return true;
} else {
return false;
}
}

I am receiving the email on a Mac, will this cause any errors?
I look forward to any help.

Paul

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.