View Full Version : PHP Help: Upload file to email

02-25-2010, 07:04 PM

I have been racking my brain trying to get my head around this php.
I have a simple form that asks (Name, Email, Directions, & Upload)

The issue I'm having is getting a PHP script that will email the form data to a desired email address. (Mainly the attachment)

I have the form data assembled as follows:

<form name="upload" method="post" action="uploademail.php" enctype="multipart/form-data">
<table cellpadding="3" cellspacing="0">
<td class="tablebody" align="right" valign="top"><label for="name">Name:</label></td>
<td><input name="name" class="textfield" id="name" size="30" type="text"><br />
<br />
<td class="tablebody" align="right" valign="top"><label for="email">Email:</label></td>

<td><input name="email" class="textfield" id="email" size="30" type="text"><br />
<br />
<td class="tablebody" align="right" valign="top"><label for="directions">Directions:</label></td>
<td><textarea name="directions" cols="30" rows="3" class="textfield" id="directions"></textarea><br />
<br />
<td class="tablebody" align="right" valign="top"><label for="uploaded_file">Filename:</label></td>

<td><input name="uploaded_file" id="uploaded_file" type="file"><br />
<br />
<tr height="65">
<td valign="top" height="65"><input name="KT_Insert1" id="KT_Insert1" value="UPLOAD" type="submit">
<td class="errortext" valign="top" height="65"><div id="upload_errorloc" class="error_strings">
<p class="errortext"></p>


I need help getting the PHP script.
I have looked around places on the net and I just don't fully understand them.

Any help in this matter would be GREATLY appreciated.

Here is what I have so far.

//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['questionscomments'];

$email_from = 'EMAIL@COMPANY.COM';//<== update the email address
$email_subject = "New Contact Form From $name.\n";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message".

$to = "EMAIL@COMPANY.COM";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
//done. redirect to thank-you page.
header('Location: thank-you.html');

// Function to validate against any email injection attempts
function IsInjected($str)
$injections = array('(\n+)',
$inject = join('|', $injections);
$inject = "/$inject/i";
return true;
return false;


02-25-2010, 08:17 PM
This is a little bit on the older side, but still perfectly valid for how to send attachments via email: http://www.webcheatsheet.com/PHP/send_email_text_html_attachment.php#attachment

The real big things here are the content-type is multipart/mixed and you'll see --PHRASE and --PHRASE-- around. Those are bounds marking for the email itself.
This should do exactly what you're looking for, you'll just need to edit the text set for the message (looks like 1 part plain, 1 part html), and the filename in use. You can get all of that from you're form I would presume.

Another article here: http://www.daniweb.com/forums/thread60795.html
This one is a little shorter, but its a little more difficult to read.
You can look through a few others on google as well, there are a couple good ones: http://www.google.com/search?hl=en&rls=com.microsoft%3A*&q=php+add+attachment+to+email&aq=f&aqi=g10&aql=&oq=

02-25-2010, 10:32 PM

Thank you for the response.
I feel I may have written it wrong...

What I am looking for is the user to upload a file via the form.
And when he hits the submit button it emails that file to me.

The two sources you linked are good, but they do the reverse.
They email a file to the user.

Any help in that area?

02-25-2010, 10:44 PM
You simply change the address for the email from the users to your own :)

02-26-2010, 02:37 PM

Ok. but I don't know how to get it from the submit field to a folder.. then the folder to me..

I will continue to browse.
Thanks for leading me in the correct direction tho.