SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mail form with file attachement

    Hello,

    I'm looking to make an email form for my site, with file attachement.
    I found this article: http://www.finalwebsites.com/snippets.php?id=36
    Now I'm trying to add a simple form to it.

    This is what I made of it (only the upload part so far, I think the rest would be easy)

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    body,td,th {
        color: #000000;
    }
    label{
    display: block;}
    -->
    </style></head>

    <body>

    <?php
    function mail_attachment($filename$path$mailto$from_mail$from_name$replyto$subject$message) {
        
    $file $path.$filename;
        
    $file_size filesize($file);
        
    $handle fopen($file"r");
        
    $content fread($handle$file_size);
        
    fclose($handle);
        
    $content chunk_split(base64_encode($content));
        
    $uid md5(uniqid(time()));
        
    $name basename($file);
        
    $header "From: ".$from_name." <".$from_mail.">\r\n";
        
    $header .= "Reply-To: ".$replyto."\r\n";
        
    $header .= "MIME-Version: 1.0\r\n";
        
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
        
    $header .= "This is a multi-part message in MIME format.\r\n";
        
    $header .= "--".$uid."\r\n";
        
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
        
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
        
    $header .= $message."\r\n\r\n";
        
    $header .= "--".$uid."\r\n";
        
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"// use diff. tyoes here
        
    $header .= "Content-Transfer-Encoding: base64\r\n";
        
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
        
    $header .= $content."\r\n\r\n";
        
    $header .= "--".$uid."--";
        if (
    mail($mailto$subject""$header)) {
            echo 
    "mail send ... OK"// or use booleans here
        
    } else {
            echo 
    "mail send ... ERROR!";
        }
    }

    if (!
    $_POST['submit']){ // show form
    ?>

            <form method="post" action="test.php">
            <label>Design file:</label>
            <input type="file" name="tfile" class="inputDefault" />
            </div>
            <div id="order-right">
            </div>

            <div class="clear">
            <input type="submit" name="submit" value="Submit" class="form-button" />
            </div>
            </form>


    <? }else{ // stuur form als gesubmit

    $tfile $_POST['tfile'];
    $my_path "/home/.noogies/ee13/www.ee13.com/";
    $my_name $_POST['name'];
    $my_mail "altin.t@gmail.com";
    $my_replyto $_POST['mail'];
    $my_subject $_POST['subject'];
    $my_message $_POST['msggs'];
    mail_attachment($tfile$my_path"recipient@mail.org"$my_mail$my_name$my_replyto$my_subject$my_message);

    }

    ?>




    <?
    // how to use example
    /*
    $my_file = "somefile.zip";
    $my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
    $my_name = "Olaf Lederer";
    $my_mail = "my@mail.com";
    $my_replyto = "my_reply_to@mail.net";
    $my_subject = "This is a mail with attachment.";
    $my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
    mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
    */
    ?>

    </body>
    </html>

    A live example can be found here:
    http://www.ee13.com/test.php

    When trying to upload, it gives me few warnings:
    Code:
    Warning: filesize() [function.filesize]: stat failed for /home/.noogies/ee13/www.ee13.com/20original.zip in /home/.noogies/ee13/www.ee13.com/test.php on line 21
    
    Warning: fopen(/home/.noogies/ee13/www.ee13.com/20original.zip) [function.fopen]: failed to open stream: No such file or directory in /home/.noogies/ee13/www.ee13.com/test.php on line 22
    
    Warning: fread(): supplied argument is not a valid stream resource in /home/.noogies/ee13/www.ee13.com/test.php on line 23
    
    Warning: fclose(): supplied argument is not a valid stream resource in /home/.noogies/ee13/www.ee13.com/test.php on line 24
    mail send ... OK
    But it also says "mail send ... OK" but I still don't recive anything to my email.


    Can someone please help me?

    Thanks!

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    <form method="post" action="test.php" enctype="multipart/form-data">

    http://www.w3.org/TR/html4/interact/...l#adef-enctype
    Saul

  3. #3
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Belgium
    Posts
    387
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ew, that was stupid of me ...
    But it still isn't working.
    But I don't get any warnings anymore.

    Thanks!

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, you need to handle the file upload first, then email it. See Chapter 38. Handling file uploads in PHP Manual.
    Saul


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
  •