FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Can I have attachments added to forms?





woundedhealer
I want to have a form on my website as I think it would be better than getting people to email me information. With a form, I can make sure I'm sent the info I need.
I also want them to send me a photo. Is it possible for people to attach a photo to a form?
TomS
I think you know how to create a form in HTML.

Code:
<form action="sendmail.php" method="POST">
<!-- some Inputs // -->

<!-- Here the input to upload a file // -->
<input name="thefile" type="file"> 
</form>


Here the code of the phpfile "sendmail.php" which finally sends the mail:

Code:

mail($_POST["sender"], $_POST["subject"], $_POST["message"],"From: My Webformular \nContent-Disposition: inline; filename=\"$_POST["thefile"]\")



I think that works (not tested), you only have to adapt the post-variables and your html-form.
woundedhealer
I've only used cgi to send forms. I don't know anything about php. If php is the only way to do it, I'll have to have a rethink. Maybe get the information by email for now and start learning php.
TomS
I found these two sites which are said to have content about attaching a file to an email sent with cgi/perl:

http://www.sendmail.org/

http://search.cpan.org/dist/CGI.pm/

Maybe that helps. If not, I can write you the whole script in php (with comments to explain functions, ofcourse).
woundedhealer
TomS
I read the article and tested the code.

Very useful. (ironic)

The code runs the standard-mailprogram (e.g. outlook) and attaches the file.

I think this is not what you want.
woundedhealer
It seems images can't be attached to forms. They sort of get scrambled in the sending. Can images be sent ok with php?
TomS
So. I found a useful code in a german php-forum.
Hope it's helpful for anyone.

Code:
# Maximal filesize of attachment in bytes:
$max_attach_size = 100000;
$filesize        = false;
$int_filesize    = $_FILES['upload1']['size'];
$str_filename    = $_FILES['upload1']['name'];

if ($int_filesize > $max_attach_size)
 {$filesize = false;
   echo "<h3>FormMailer caused an error:</h3>";
   echo "Your attachmentfilesize <b>".$str_filename."</b> is too big
(".number_format($_FILES['upload1']['size']/1000,0,",","")." KB)
 - Maximal filesize: .number_format($max_attach_size/1000,0,",","")."
KB<br>";
     echo "<br><a href="java script:history:back()\">Back to the form.</a>";
  }
else
  {// If attacments exists and have right size, create MIME-Mail:
   $filesize = true;
   if ($_FILES['upload1']['name'] && $filesize == true)
     {
      // Read files an encode them:
      $datei_content = fread(fopen($_FILES['upload1']['tmp_name'],"r"),
filesize($_FILES['upload1']['tmp_name']));
      $datei_content = chunk_split(base64_encode($datei_content),76,"\n");

      // Declare Boundary:
      $boundary = md5(uniqid(rand()));

      // Mail-Header:
      $header = "From: FormMailer<".$strFrom.">\r\n";
      $header .= "X-Sender-IP: ".$ip."\n";
      $header .= "MIME-Version: 1.0\n";
      $header .= "Content-Type: multipart/mixed; boundary=\"".$boundary."\"\n";
      $header .= "This is a multi-part message in MIME format.\n";
      // Mail-Text:
      $header .= "--".$boundary;
      $header .= "\nContent-Type: text/plain";
      $header .= "\nContent-Transfer-Encoding: 8bit";
      $header .= "\n\n".$text."\n\n(".number_format($int_filesize/1000,0,",","")." KB)";
      // Attachment:
      $header .= "\n--".$boundary;
      $header .= "\nContent-Type: ".$_FILES['upload1']['type'].";
name=\"".$_FILES['upload1']['name']."\"";
      $header .= "\nContent-Transfer-Encoding: base64";
      $header .= "\nContent-Disposition: attachment;
filename=\"".$_FILES['upload1']['name']."\"";
      $header .= "\n\n".$datei_content;

      // Ende:
      $header .= "\n--".$boundary."--";
      // Send mail and print Error / Succeed message
      mail($email,$betreff,"",$header) or die ("Transmission error.");


I just translated it into english. Have Fun.
woundedhealer
Thank you Toms for taking the time to do this. I'm going to study it to see if I can make some sense of it. I don't know any php and as I'm dyslexic it just looks like one big jumble of meaningless random letters. Hopefully I can eventually understand it and make use of it.
take3
Does anybody know how to change this code to add a second attachment?

that be a great help!

tnx.
jmraker
To add a 2nd file, add a 2nd file upload field like <input type="file" name="upload2">

and repeat the attachment in the email by adding a 3rd MIME section, copy the 2nd section so it puts the image contents in the 3rd section, or better replace the 1st attachment code to use a loop like
Code:
// The part before the attachments
foreach($_FILES as $key=>$file){
  if($key == 'upload1' || $key == 'upload2'){
      $header .= "\n--".$boundary;
      $header .= "\nContent-Type: ".$file['type']."; name=\"".$file['name']."\"";
      $header .= "\nContent-Transfer-Encoding: base64";
      $header .= "\nContent-Disposition: attachment; filename=\"".$file['name']."\"";

      $datei_content = fread(fopen($file['tmp_name'],"r"), filesize($file['tmp_name']));
      $datei_content = chunk_split(base64_encode($datei_content),76,"\n");
      $header .= "\n\n".$datei_content;
  }
}
// rest of mail code from TomS


Be aware that the code doesn't use the mail function correctly. The mail contents should not be in the header. If it doesn't work, the string "This is a multi-part message in MIME format" and below should be in a separate variable like $message and use
Code:
mail($email,$betreff,$message,$header) or die ("Transmission error.");

(including the email attachments)
Related topics
Microsoft gunning for Adobe's PDF format?
E-Cards with PHP & MySQL
HTTP AUTH with PHP and mySQL
How To : Secure Your PHP Website
751 Useful Windows XP Files
The downfall of american society
The IT Career Builder's Toolkit
attachments...
Bluetooth
Co myslicie o mojej stronce??
World of Warcraft
News and Comment system!
Syndicate Rich Media
How does facebook do those forms?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.