PHP question...

Live forum:


29-08-2007 18:48:27

For a basic mail() function, where there's a few variables from input boxes and such, and then it gets mailed to a specified email address...I have that all working and all, but how do I go about sending an attachment along with that email. So if a user's filling out a form, and they can insert a file attachment by clicking browse, how do I make it in PHP so that it sends the file attachment. I included below the code I have.


$name = stripslashes($name);
$address = stripslashes($address);
$city = stripslashes($city);
$state = stripslashes($state);
$zip = stripslashes($zip);
$homep = stripslashes($homep);
$workp = stripslashes($workp);
$cellp = stripslashes($cellp);
$email = stripslashes($email);
$lastc = stripslashes($lastc);
$comments = stripslashes($comments);
$subject = "Inquiry";

$headers = "From: $name <$email>\r\n";
$headers .= "Content-type: text/html\r\n";

$tmptext = "<html><head></head><body><font size=3><b>From:</b> " . $name . " [<a href=mailto:" . $email . ">" . $email . "</a>]<br><br><b>Address: </b> " . $address . "<br><br><b>City: </b>" . $city . "<br><br><b>State: </b>" . $state . "<br><br><b>Zip: </b>" . $zip . "<br><br><b>Home Phone: </b>" . $homep. "<br><br><b>Work Phone: </b>" . $workp. "<br><br><b>Cell Phone: </b>" . $cellp. "<br><br><b>Name of last company employed at: </b>" . $lastc . "<br><br><b>Additional Information/Comments: </b>" . $comments . "</font></body></html>";




29-08-2007 19:11:29

[code1dcedf16b59]$file = fopen('file/directory/path', 'rb');

$file_type = mime_content_type('file/directory/path');

$file_data = fread($FILE, filesize('file/directory/path');


$file_data = chunk_split(base64_encode($file_data));

$tmptext .= "--{$mime_boundary}\r\n".

"Content-Transfer-Encoding: base64\r\n".

"Content-Disposition: attachment; filename="File_Name"\r\n".



Then just make sure to add a title in the mail function for the new attachment. Obviously you will replace the file/directory/path with the $_POST global with the file name. The file will be sent somewhat like this -

mail('', 'File Name', $tmptext, $headers)

Something like that I guess

[bdcedf16b59]EDIT[/bdcedf16b59] I guess I should explain. Well, I decided to encode the file in base64 because its the generic encryption method for emails. This script is by no means safe of being hacked. There are many file content checks that should be gone through to make sure it eliminates all executable programs and what not. but this is just what you asked for.


29-08-2007 19:16:53

file/directory/path or whatever would be the input box name? $file or whatever I decide to name it?

And also, in the mail function, I put "File Name" exactly like that? That part confuses me. And do I put it before $subject or after $subject.


I also notice there's a File_Name also. That part confuses me. P


29-08-2007 19:21:21

File Name just refers to the file name after directory paths have been cut. FOr example when you recieve an email you dont recieve a file titled Windows/Users/Joseph/DocummentsandSettings/file.txt , you just recieve, file.txt . Anywhere it says File Name or File_name or w/e is where you put in the variable regarding the file name. file/directory/path is also a variable from your other page (ex. the Browse... button)


29-08-2007 19:28:10

So the variable from the other page, is the variable I use for all of those things? Also, can you edit the code so that it excludes script files, and executables? .vb,url==http://=http:///url .exe,url==http://=http:///url .html,url==http://=http:///url .php,url==http://=http:///url .js,url==http://=http:///url etc. Thanks!


29-08-2007 20:19:51

well, that would take a while - try looking at hotscripts - they have pre-made ones (just source code), that are easy to use and don't make you look like a pre-made geocities site.


29-08-2007 20:23:21

Ahh geocities. The good old days. I remember using Yahoo PageBuilder when I was 8. D


31-08-2007 12:23:05

Yeah um. This is urgent. Jams. I tried your code, and it's not working there's like something wrong with $file_data = fread($FILE, filesize('file/directory/path');

it's not liking the semicolon. I don't know why it's working and I'm becoming frustrated. The variable for the browse box in the form is $resume. So if you could please help me with that as your code is extremely confusing. Thanks!

I replaced all those path things with $resume, and everything just kept on exploding. (


31-08-2007 16:32:17

[code1364ba31be5]if(strlen($resume) >= 1){
$file = fopen($resume, 'rb');

//$file_type = mime_content_type($resume);

$file_data = fread($file, filesize($resume));


$file_data = chunk_split(base64_encode($file_data));

$tmptext .= "--{$mime_boundary}\r\n".

"Content-Transfer-Encoding: base64\r\n".

"Content-Disposition: attachment; filename='Resume'\r\n".



$ok = mail('', $subject, $tmptext, $headers);
$ok = mail('', $subject, $tmptext, $headers);


I have that code...and when I get an email, apparently the file comes through...but it shows in the message the base64 of the file. It does NOT attach the file. How do I fix this?

Comes like this...


01-09-2007 20:22:08

First, you need to retrieve the file contents uploaded by the user.


The important thing is to define your HTML form as having the [bc8dae3b53d]enctype="multipart/form-data"[/bc8dae3b53d] and [bc8dae3b53d]method="POST"[/bc8dae3b53d]. Also don't forget the extra hidden input MAX_FILE_SIZE, which is used by PHP to ignore files that are too large.

PHP will store the uploaded file to a temp folder on the server, and populate the _FILES array variable with all the information about the file(s) uploaded. You can retrieve the temp filename from _FILES['myfile']['tmp_name'] (where 'myfile' is the name value you assigned to the file input). With the filename, you can then use the file functions as described earlier to open and read the file contents.

Read the contents into a variable. Then you have to add the variable contents as an attachment in the outgoing e-mail. You can use the built-in PHP mail() function, but you have to MIME encode it yourself. If you have PEAR handy, check out the Mail_Mime package. You might also refer to this article



01-09-2007 20:27:25

That's what I need help doing. I need the code, lol. I have no clue how to do that. I'm a noob at PHP.


02-09-2007 00:25:05

if you're intending to put that code on a live site, you need to make sure you protect yourself