View Full Version : attaching files from forms

04-13-2004, 11:29 PM
can i attach a $_FILES['filefield']['tmp_name'] to an email address as an attachment?

04-13-2004, 11:54 PM
Yes it is!

I just so happened to come accross a script that does email attachements today. You can find it here: http://www.phpfreakz.nl/library.php?sid=8785.

Note that it is a dutch website, but the script has english comments, so you should be able to understand it if you are not a dutch native speaker.

Furthermore I havn't tested the script, but I guess that it should work just fine!

Good luck, cheers Michiel

04-14-2004, 12:14 AM
where in that code does it show exactly how to just email the attachment?

04-14-2004, 01:35 AM
Here's the script I use

//change/add to this line depending on what format of image you want.

$mime_type = array(1=>"image/jpeg", 2=>"image/gif", 3=>"image/jpg", 4=>"image/pjpeg"); //this part decides what type of mime type to put after the type of image is determined

function mailcheck($emailadr){
return eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$", $emailadr);

if (!isset($_POST['submit'])){
#if submit doesn't have a value

<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);" enctype="multipart/form-data">
<p>Name: <input type="text" name="name" style="background:#ccc;" /></p>
<p>Subject: <input type="text" name="subject" style="background:#ccc;" /></p>
<p>Email: <input type="text" name="email" style="background:#ccc;" /></p>
Send this file: <input name="userfile" type="file" />
<p>Comments/Questions: <br />
<textarea name="comment" rows="10" cols="30" style="background:#ccc;"></textarea>
<p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
} else {
if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
// you can change this if you move the uploaded file from the tmp folder.
$uploaddir = '/the/directory/here/uploads/'; // change this
$uploadfile = $uploaddir . $_FILES['userfile']['name'];
if ($_FILES['userfile']['size'] > 100000) {
echo 'file size too large';

if (!copy($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "Possible file upload attack! Here's some debugging info:\n";
$new_mimetype = $_FILES['userfile']['type'];
} else {
echo "Wrong file type uploaded.";
} else {
echo "No file Uploaded, or file was empty";

$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comment = stripslashes($_POST['comment']);
$to = "bob@example.com";

if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL)){
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty

# subject
#$subject = "mail from site" deleted

# message
$message = '
<style type="text/css">
body { font-family: arial; background: #ccc; }

<span style="font-size:12pt;font-weight:bold;color:blue;">Subject:</span> '.$subject.'<br /><br />
<span style="font-size:12pt;font-weight:bold;color:blue;">Name of person:</span> '.$name.'<br /><br />
<span style="font-size:12pt;font-weight:bold;color:blue;">Email of person:</span> '.$email.'<br /><br />
<span style="font-size:12pt;font-weight:bold;color:blue;">Comment/Question:</span><br />
<div style="font-size:10pt;">'.$comment.'</div>
<br />


// create a unique boundry, can basically be anything.
// generally people use time()
$mail_boundary = md5(uniqid(time()));
// create header
$mail_headers = "From: $email\r\n"; #The sender's email
$mail_headers .= "MIME-Version: 1.0\r\n";
$mail_headers .= "Content-type: multipart/mixed; boundary=\"$mail_boundary\"\r\n\r\n";
if (file_exists($uploadfile)) {
// must open the file in readonly and binary format
$fp = fopen($uploadfile, "rb");
$file = fread($fp, filesize($uploadfile));
// also you have to base64 it so the email can see it.
$file = chunk_split(base64_encode($file));
// must get the name of the file
$filename = basename($uploadfile);
// sets the email up so we can send the text and the attachment at the same time
$new_mail_body .= "--{$mail_boundary}\r\n";
$new_mail_body .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
$new_mail_body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$new_mail_body .= "{$message}\r\n";

$new_mail_body .= "--{$mail_boundary}\r\n";
$new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n";
$new_mail_body .= "Content-Transfer-Encoding:base64\r\n ";
$new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
$new_mail_body .= $file . "\r\n";
$new_mail_body .= "--{$mail_boundary}--\r\n";

// now the good stuff as we actually send the message and the attachment.

echo 'Thank you for sending mail.';
} else {
echo 'Sorry, one of more of your fields is invalid.';


04-15-2004, 12:07 AM
i have the form mailer code all created, but i need to know where to put the attachment in the mail() function. i am sending a .XLS file from an upload form and i need it to be sent in the email as an attachment.

05-07-2004, 01:54 PM
Hello Boeing747FP

Did you end up finding an answer ?

I have the same problem.
Right now I'm working on the page and the code is driving me nuts, trying to get the form to accept a image so you can submit it with its application and send it to a email address with image.

I'm yet to get this working as it's got me foxed big time :eek: .

cheers karl

05-07-2004, 09:07 PM
You guys miss this part in Josh_'s post?

$new_mail_body .= "--{$mail_boundary}\r\n";
$new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n";
$new_mail_body .= "Content-Transfer-Encoding:base64\r\n ";
$new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
$new_mail_body .= $file . "\r\n";
$new_mail_body .= "--{$mail_boundary}--\r\n";

I've never done anything like this, but I'm assuming that's the code for it.. this is after the file has been uploaded of course.

Hope that helps,