FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a 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.
Reply to topic    Frihost Forum Index -> Scripting -> Others

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