I'm writing a mailform i php for placeing orders, and sense they have to get send a picture to me for the order to work properly, I'd like to be able to attach the file in the formmail. How shuld I do this? I have seen some different sulutions but non that I've complety understand.
You need to set the right mail-headers, and then attach the file by encoding it to whatever form you have declared in the header, like in this snippet:
All you need to do here, is read the file, and encode it (to base64 in this case)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$data = chunk_split(base64_encode($data));
first you'll need a boundary, like a rule to tell where one part stops, and the other begins
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
then set the headers right, to support attachement
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
then build up your message
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" . // start text block
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_content . "\n\n" .
"--{$mime_boundary}\n" . // start attachement
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" . // this is the file...
"--{$mime_boundary}\n";
and then... sent the message using mail ;-)
mail($email_to, $email_subject, $email_message, $headers)
I would also suggest php_mailer http://sourceforge.net/project/showfiles.php?group_id=26031
Has all of the options you could ever want and lets you build custom length forms without "TOO" much pain
There are also a bunch of tutorials and would gladly send along an example of a project I did recently if you want
$sl="select max(id)AS maxid from photos"; $res=mysql_query($sl); $rowl=@mysql_fetch_array($res);
$adid=$rowl['maxid']; $filedir="/photo_gallery/"; $file1=$filedir."img".$adid.$_FILES['myfile']['name'];
//echo $file1; @move_uploaded_file($_FILES['myfile']['tmp_name'],$file1);
$upd="update photos set photo='".$file1."',Added_date=now() where id=$adid";
//echo $upd; mysql_query($upd);
@unlink($file1);