473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

corrupt file sending email attachment

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
3 7011
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: AspDotNetDeveloper | last post by:
I'm trying to troubleshoot why my ASP application can no longer send file attachments larger than 100K, and I have eliminated many possibilities already, but I'm running out of ideas. I think I...
5
by: Nick Z. | last post by:
When using SmtpClient and sending a message with an attached file using SendAsync(...), if I add File.Delete(pathToAttachedFile) in the SendCompleted event handler I get a file in use exception. Is...
14
by: Manish | last post by:
The project I am developing doesn't involves database. I want to parse the mailbox file (.mbx) and store the summary in the text file for fast retrieval and display of information in the Inbox...
1
by: kierenj | last post by:
Hi, I'm trying to send a zipfile to a client using Content-Disposition: attachment. Done it before and it works fine. My code is below: context.Response.Buffer = false;...
7
by: iporter | last post by:
I use the code below to authorise the download of certain files. Thus, instead of linking to the file in a wwwroot directory, I link to this code with the filename as a parameter, and the script...
1
by: krishan123456 | last post by:
i have tried to send email with attached doc file but when i receive the mail i find the attached file in encoded text instead of actuall attachment.the code that i used to send an email is given...
0
by: prasenjit2007 | last post by:
I have a main form for inputing the (to/from/mesg/file) with the following code:- <html> <body> <table> <tr> <td>To:</td> <td><input type="text" name="to" size="50" ...
4
by: Seguros Catatumbo | last post by:
Hi guys, i am having trouble deleting a file after sending an email. The file is in use. Here's the code: String texto = "Test"; System.Net.Mail.SmtpClient smtp = new...
7
by: Paridevi | last post by:
Hai , i want to send email in .Net Using OutLook Express,My Project is Web Application using vb.Net 2003 with SQL Server 2000.I have searched a lot in Google, but ican't get any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.