PDA

View Full Version : Attach Text as a Text File Using Mail() ?


mark87
07-20-2005, 10:57 AM
Is this possible? And how? lol, thanks. :)

eshban
07-20-2005, 11:39 AM
download phpmailer 1.72, its a class used for sending attachments with email

download it form google

or
goto

www.phpclasses.org

bye

mark87
07-20-2005, 12:05 PM
It seems a bit bloated for my liking, I only really want to insert some code into a script I've made already. Thanks anyway.

ivy
07-20-2005, 12:36 PM
Hi mark87

Take a look at this tutorial (http://codewalkers.com/tutorials/72/3.html) at codewalkers.

It is a really simple tutorial, well commented and should show you exactly what you need.

Hope this helps

mark87
07-20-2005, 01:26 PM
Thanks Ivy, looks like it will do the trick. I'm not actually wanting to upload a file though, the file needs to be made with some text... is there a way to do this?

ivy
07-20-2005, 02:30 PM
Have I got this right?...

You have a form with a textarea (among other fields) that is submitted by a user.
You want the data in the textarea to be attached to the email sent by the form as a text file.

Is that it?

If so, you need to first save the textarea data as a text file on the server (I think this can be done as atemp file) then attach the temp file to the email as per the tutorial mentioned above.

mark87
07-20-2005, 02:48 PM
Oh ok thanks, well each field is added into a string so that when the string is in a text file it can be imported into Access. eg.

$thefile = $name . "|" . $age . "|" . $otherstuff;

I'll have a play with making the file, but wouldn't there be a problem if two users submit data at the same time? Or is that highly unlikely?

EDIT - Actually that was a stupid question, I'll just make the filename the users name or something, then delete it after it's sent.

delinear
07-20-2005, 02:55 PM
It probably would be unlikely, but it's simple enough to get around anyway by generating a random name for the file then checking that the file doesn't already exist, then just mail and unlink() the file if everything goes through okay.

function random_name() {
$charset = "0123456789abcdefghijklmnopqrstuvwxyz";
$filename = '';
for ($i=0; $i < $size; $i++) {
$filename .= substr($charset, rand(0, strlen($charset)-1), 1);
}
$filename .= '.txt';
return $filename;
}

$filename = random_name();
while(file_exists($filename)) {
$filename = random_name();
}
// you now have your random name that doesn't exist, you can use it to create the file

mark87
07-20-2005, 03:00 PM
Thanks, but I've decided to use an ID which was created for each submission anyway. :)

mark87
07-20-2005, 03:15 PM
Hum, problem.

Text file is being created and the data is written into it (I can see on my FTP), so that's good.

But the email is not sending properly... the attachment file is 0bytes and just has the name 'ATT###' where # is some numbers. Any ideas? :confused:

mark87
07-21-2005, 11:25 AM
Sorry to bump but need to get this sorted ASAP. Can anyone help? :o

EDIT - No worries, decided to use a script here http://www.4wordsystems.com/php_mail_attachment.php , which works. Well it attaches the file - not with the correct file name, but it works. :)