Forum Moderators: coopster

Message Too Old, No Replies

How to email a file to myself

processing a file then emailing it

         

Clark

3:56 am on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got files up to 5 megabytes that are uploaded via html to a php program and I do a bunch of processing on the file....but I realized that I'd love to have a copy of the original file emailed to me so that I can check for errors in processing. How can I do that? I know the mail command where you set the text of the message in a variable and use the mail() command, but never attached files. And I guess it would be nice to gzip it first too...
Thanks.

RonPK

8:27 am on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An e-mail with an attachment is basically a multipart-mime-message. The first part could be the message, the second will be the contents of the file, base64-encoded. The parts are separated by a unique boundary. You'll also have to add some content-type headers to make things work.

Let us know if you need a more detailed explanation. Google shows some nice examples if you search for 'php mail attachment', so maybe you don't even need WebmasterWorld...

Clark

9:58 am on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks. I found a tutorial for it here:
[sitepoint.com...]

but I'm wondering since I already opened the file and processed it line by line if I can (or should) "open" it again and then send it out, if there's a less processor intensive method of handling this so I won't need to process the file twice?

RonPK

12:38 pm on Jun 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess the script should do something like this:

1a. read the uploaded file line by line and do your parsing
1b. at the same time, concatenate a string containing all the data
2. it should be safe to close the file now
3. compress the datastring, with gzencode()
4. base64-encode the compressed datastring, et cetera

AFAIK this could work...

Clark

6:22 am on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK...thing is that in that page the code is

 // Read the file to be attached ('rb' = read binary) 
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

and in my code I'm using

while ($datafieldvalues = fgetcsv ($fp, 10000, "$separator")) {

which puts the whole thing into an array...so I'm not quite sure how to put that into another variable?

RonPK

3:31 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

OK... replace all that with

$data = implode($separator . "\n", $datafieldvalues);

That should create a string of lines with your separator and a new line character at the end of the line.

Small print: Please note I haven't tested any of this... let's hope some guru steps in if I'm erring away...

Clark

7:32 pm on Jun 23, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well in the meantime I tested it by reopening the file and reading it in again and it only cost a few seconds, but it sounds like your code looks like a great way to get the same result without rereading it. I'll try it tonight. Thanks very much!