...

View Full Version : attaching files from forms



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

Michiel
04-13-2004, 10: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

boeing747fp
04-13-2004, 11:14 PM
where in that code does it show exactly how to just email the attachment?

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


<?php
//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>
<p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
</form>
<?php
} 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(array_search($_FILES['userfile']['type'],$mime_type)){
if ($_FILES['userfile']['size'] > 100000) {
echo 'file size too large';
exit;
}

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

$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 = '
<html>
<head>
<title>$subject</title>
<style type="text/css">
body { font-family: arial; background: #ccc; }
</style>
</head>
<body>

<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 />

</body>
</html>';

// 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.
mail($to,$subject,$new_mail_body,$mail_headers);

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

?>

boeing747fp
04-14-2004, 11:07 PM
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.

karl38
05-07-2004, 12: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

sad69
05-07-2004, 08: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,
Sadiq.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum