Page 1 of 3 123 LastLast
Results 1 to 10 of 25

Thread: mail form with upload file

  1. #1
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default mail form with upload file

    Hey all,
    I have this mail form, with this as php code:
    Code:
    <?php
    if ($_POST["action"] == "send"){
    if ($_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") { 
    mail ("info@site.com", "via site", 
    "
    Naam: ".$_POST['naam']."
    E-mail: ".$_POST['email']."
    Bericht: ".$_POST['bericht']."
    ",
    "From: ".$_POST['naam']." <".$_POST['email'].">");
    $subject = "je bericht aan Ceci Casariego"; 
    $msg = "
    Dit is een automatisch verzonden mail. Gelieve niet te antwoorden.
    Beste $_POST[naam],
    Bedankt voor je bericht aan Ceci.
    Ze zal je zo snel mogelijk beantwoorden
    Dit was je bericht:
    $_POST[bericht] 
    ";  
    mail($_POST[email], $subject, $msg); 
    echo 'Bedankt.<br><br>Je bericht is verzonden, <br>en je zult een bevestiging ontvangen. <br><br>&nbsp;<br><br>';
    }
    else{
    echo 'Gelieve alle velden in te vullen<br><br>Je naam, email adres en bericht<br> zijn verplichte velden.<br><br><a href="contactNL.html"><font color="#565656;">[graag opnieuw]</font></a><br><br>&nbsp;<br>';
    }
    }
    ?>
    I wish to add an upload function (attachment), like this:
    Code:
    <script type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>
    <form name="form1" method="post" action="contactNL.php">
    <input type="hidden" name="action" value="send"> 
    <input type="text" name="naam" value=" je naam" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted #cc6600; border-right:0px; border-bottom: 1px dotted #cc6600; width:150px">
    <br>
    <input type="text" name="email" value=" je email adres" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted purple; border-right:0px; border-bottom: 1px dotted purple; width:150px;">
    <br>
    <input type="file" name="fieldname" style="border-top:0px; border-left: 1px dotted purple; border-right:0px; border-bottom: 1px dotted purple; width:150px;"><br>
    <textarea name="bericht" style="border-top:0px; border-left:1px dotted #0066cc; border-right:0px; border-bottom: 1px dotted #0066cc; width:150px; height:70px" onfocus="clearDefault(this)"> je bericht</textarea>
    <br> 
    &nbsp;&nbsp;&nbsp; <input type="image" src="images/sendarrow.gif" value="send"> 
    &nbsp;&nbsp;&nbsp; <img src="images/clear.gif" onclick="document.getElementById('form1').reset();" style="cursor:pointer">
    </form>
    But when I send the mail, the attached file isn't sent. What needs to be changed in the php ?
    The attachment is not oligatory; the other fields need to be filled in.
    Last edited by chechu; 06-08-2008 at 11:25 AM.

  2. #2
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

  3. #3
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    I might be a code monkey, but it seems none hears me roar !

  4. #4
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Yes, we are, and I'm hearing the roar

    Have you had tried gone through PHP Manual - Handling File Uploads
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  5. #5
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Excellent ears !
    I will explain you my problem: I am an artist, complexe personality, living in another world, so php and all its ' ; $ array ' < and these signs are trees into the huge forrest of webdesign to me. Html I can work with a bit, but no php, as that involves more logical thinking. So what I am trying to say is that I usually pay people that I admire doing things I cannot do (like the plumber). Webdesigners ask so much money for their work, that I now refuse to pay for it. Dynamic Drive actually built and rebuilt my site, and all the people on this platform can count on my greatest appreciation.
    To get to the point: I do not understand what is explained in your link. I honestly don't. Does that mean I want to get the scripts just like that, no questions asked ? Would be perfect to work with, but I'd rather see how it is created, from the very start. Unfortunately that cannot be done; therefore I'd need a private teacher. Or as John Scheuer ones told me on this forum: "teach a man how to fish, and he'll get drunk in the harbour all day." Or something like that ...
    This forum actually helped me in creating a new project about interaction of unknown people, the social help, not being selfish. I'm working on it, first project after two years of inactivity (due to motherhood), so check out the site you've built !

    If I add this in the html, should it work then ?
    Code:
    <script type="text/JavaScript">
    function clearDefault(el) {
    if (el.defaultValue==el.value) el.value = ""
    }
    </script>
    <form name="form1" method="post" enctype="multipart/form-data" action="contactNL.php">
    <input type="hidden" name="action" value="send"> 
    <input type="text" name="naam" value=" je naam" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted #cc6600; border-right:0px; border-bottom: 1px dotted #cc6600; width:150px">
    <br>
    <input type="text" name="email" value=" je email adres" onfocus="clearDefault(this)" style="border-top:0px; border-left: 1px dotted purple; border-right:0px; border-bottom: 1px dotted purple; width:150px;">
    <br>
    <input type="file" name="fieldname" style="border-top:0px; border-left: 1px dotted purple; border-right:0px; border-bottom: 1px dotted purple; width:150px;"><br>
    <textarea name="bericht" style="border-top:0px; border-left:1px dotted #0066cc; border-right:0px; border-bottom: 1px dotted #0066cc; width:150px; height:70px" onfocus="clearDefault(this)"> je bericht</textarea>
    <br> 
    &nbsp;&nbsp;&nbsp; <input type="image" src="images/sendarrow.gif" value="send"> 
    &nbsp;&nbsp;&nbsp; <img src="images/clear.gif" onclick="document.getElementById('form1').reset();" style="cursor:pointer">
    </form>
    Last edited by chechu; 06-13-2008 at 09:37 AM.

  6. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    I'm actually trying to give you a link since I'm not on my wits with regards to PHP.
    But since its a boring afternoon, and got nothing to do, I worked with the code. And here's what I've got:

    PHP Code:
    <?php
    if ($_POST["action"] == "send"){
    $uploaddir 'c:\\progra~1\\xampp\\htdocs\\'# This will be your web directory.
    $uploadfile $uploaddir.basename($_FILES['fieldname']['name']);
    if (
    move_uploaded_file($_FILES['fieldname']['tmp_name'],$uploadfile)) {
        echo 
    "File is valid, and was successfully uploaded.<br>\n<img src='".basename($_FILES['fieldname']['name'])."'> <br>\n";
    } else {
        echo 
    "Possible file upload attack!\n";
    }



    if (
    $_POST[naam] != " je naam" and $_POST[naam] != "" and $_POST[email] != " je e-mail adres" and $_POST[email] != "" and $_POST[bericht] != "") { 
    mail ("info@site.com""via site"
    "
    Naam: "
    .$_POST['naam']."
    E-mail: "
    .$_POST['email']."
    Bericht: "
    .$_POST['bericht']."
    "
    ,
    "From: ".$_POST['naam']." <".$_POST['email'].">");
    $subject "je bericht aan Ceci Casariego"
    $msg "
    Dit is een automatisch verzonden mail. Gelieve niet te antwoorden.
    Beste 
    $_POST[naam],
    Bedankt voor je bericht aan Ceci.
    Ze zal je zo snel mogelijk beantwoorden
    Dit was je bericht:
    $_POST[bericht] 
    "
    ;  
    mail($_POST[email], $subject$msg); 
    echo 
    'Bedankt.<br><br>Je bericht is verzonden, <br>en je zult een bevestiging ontvangen. <br><br>&nbsp;<br><br>';
    }
    else{
    echo 
    'Gelieve alle velden in te vullen<br><br>Je naam, email adres en bericht<br> zijn verplichte velden.<br><br><a href="contactNL.html"><font color="#565656;">[graag opnieuw]</font></a><br><br>&nbsp;<br>';
    }
    }
    ?>
    ...and have this in your form tag:
    Code:
    enctype="multipart/form-data"
    I'm having XAMPP, and it's working, just edit the value of $uploaddir.

    See if it helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. The Following User Says Thank You to rangana For This Useful Post:

    chechu (06-13-2008)

  8. #7
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    It looked like I'd have a boring day also, but that has changed.
    I'll have a look at it !

    What does this mean ?
    I'm having XAMPP, and it's working, just edit the value of $uploaddir.
    Is the upload directory where the file will be placed on my computer when sent ?
    I just want the file as an attachment in the email, nothing more.
    Also, if someone wants to send me like five pictures, how can they be uploaded ?

    Just tried the form, and the message is sent, but no email.
    Check here: http://www.cecicasariego.com/testcontact.html
    Also, when there is no file uploaded, there should not come up the message: 'possible file attack'.
    Last edited by chechu; 06-13-2008 at 10:22 AM.

  9. #8
    Join Date
    Jun 2007
    Posts
    72
    Thanks
    3
    Thanked 3 Times in 3 Posts

    Default

    I am no great PHP coder, but I had a quick look on google and found this tutorial this explains how to send a email with attachments I have also attached the zip file containing the sample PHP and HTLM files.

    Hope the helps

  10. The Following User Says Thank You to calumogg For This Useful Post:

    chechu (06-16-2008)

  11. #9
    Join Date
    Jul 2006
    Location
    Antwerp, Belgium (Europe)
    Posts
    927
    Thanks
    121
    Thanked 2 Times in 2 Posts

    Default

    Code:
    <?php
    // Read POST request params into global vars
    $to      = $_POST['to'];
    $from    = $_POST['from'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    
    // Obtain file upload vars
    $fileatt      = $_FILES['fileatt']['tmp_name'];
    $fileatt_type = $_FILES['fileatt']['type'];
    $fileatt_name = $_FILES['fileatt']['name'];
    
    $headers = "From: $from";
    
    if (is_uploaded_file($fileatt)) {
      // Read the file to be attached ('rb' = read binary)
      $file = fopen($fileatt,'rb');
      $data = fread($file,filesize($fileatt));
      fclose($file);
    
      // Generate a boundary string
      $semi_rand = md5(time());
      $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
      
      // Add the headers for a file attachment
      $headers .= "\nMIME-Version: 1.0\n" .
                  "Content-Type: multipart/mixed;\n" .
                  " boundary=\"{$mime_boundary}\"";
    
      // Add a multipart boundary above the plain message
      $message = "This is a multi-part message in MIME format.\n\n" .
                 "--{$mime_boundary}\n" .
                 "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
                 "Content-Transfer-Encoding: 7bit\n\n" .
                 $message . "\n\n";
    
      // Base64 encode the file data
      $data = chunk_split(base64_encode($data));
    
      // Add file attachment to the message
      $message .= "--{$mime_boundary}\n" .
                  "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" .
                  "--{$mime_boundary}--\n";
    }
    
    // Send the message
    $ok = @mail ($to, $subject, $message, $headers);
    if ($ok) {
      echo "<p>Mail sent! Yay PHP!</p>";
    } else {
      echo "<p>Mail could not be sent. Sorry!</p>";
    }
    ?>
    Where do I put the mail adress where it needs to be sent to ?
    I changed @mail into an existing adress, but the form didn't work then.
    You can check it here

  12. #10
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    $ok = @mail ($to, $subject, $message, $headers);
    take out the space

    Code:
    $ok = @mail($to, $subject, $message, $headers);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •