Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post

    attaching files from forms

    can i attach a $_FILES['filefield']['tmp_name'] to an email address as an attachment?

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    The Netherlands
    Posts
    252
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes it is!

    I just so happened to come accross a script that does email attachements today. You can find it here: http://www.phpfreakz.nl/library.php?sid=8785.

    Note that it is a dutch website, but the script has english comments, so you should be able to understand it if you are not a dutch native speaker.

    Furthermore I havn't tested the script, but I guess that it should work just fine!

    Good luck, cheers Michiel

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    where in that code does it show exactly how to just email the attachment?

  • #4
    New Coder
    Join Date
    Apr 2004
    Location
    NC
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's the script I use
    PHP Code:
    <?php
    //change/add to this line depending on what format of image you want.

    $mime_type = array(1=>"image/jpeg"2=>"image/gif"3=>"image/jpg"4=>"image/pjpeg"); //this part decides what type of mime type to put after the type of image is determined



    function mailcheck($emailadr){
    return 
    eregi("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$"$emailadr);
    }

    if (!isset(
    $_POST['submit'])){
    #if submit doesn't have a value

    ?>
       <form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);" enctype="multipart/form-data">
       <p>Name: <input type="text" name="name" style="background:#ccc;" /></p>
       <p>Subject: <input type="text" name="subject" style="background:#ccc;" /></p>
       <p>Email: <input type="text" name="email" style="background:#ccc;" /></p>
       Send this file: <input name="userfile" type="file" />
       <p>Comments/Questions: <br />
       <textarea name="comment" rows="10" cols="30" style="background:#ccc;"></textarea>
       </p>
       <p><input type="submit" name="submit" value="Submit" style="border:1px solid black; color: blue; font-family:arial; background:#ccc;" /></p>
       </form>
    <?php
    } else {
       if (
    is_uploaded_file($_FILES['userfile']['tmp_name'])) {
           
    // you can change this if you move the uploaded file from the tmp folder.
           
    $uploaddir '/the/directory/here/uploads/'// change this
           
    $uploadfile $uploaddir $_FILES['userfile']['name'];
       if(
    array_search($_FILES['userfile']['type'],$mime_type)){
           if (
    $_FILES['userfile']['size'] > 100000) {
                   echo 
    'file size too large';
                   exit;
               }
         
               if (!
    copy($_FILES['userfile']['tmp_name'], $uploadfile)) {
                   echo 
    "Possible file upload attack! Here's some debugging info:\n";
                   
    print_r($_FILES);
                   exit;
               }
               
    $new_mimetype $_FILES['userfile']['type'];
           } else {
               echo 
    "Wrong file type uploaded.";
               exit;
           }
       } else {
           echo 
    "No file Uploaded, or file was empty";
           exit;
       }

       
    $name $_POST['name'];
       
    $email $_POST['email'];
       
    $subject $_POST['subject'];
       
    $comment stripslashes($_POST['comment']);
       
    $to "bob@example.com";

       if ((
    mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL)||($subject!=NULL)){
           
    #check if a real email address, if it returns true it sends the email
           #also checks to make sure the comment and name fields aren't empty

           # subject
           #$subject = "mail from site" deleted

           # message
           
    $message '
           <html>
           <head>
           <title>$subject</title>
           <style type="text/css">
           body { font-family: arial; background: #ccc; }
           </style>
           </head>
           <body>

           <span style="font-size:12pt;font-weight:bold;color:blue;">Subject:</span> '
    .$subject.'<br /><br />
           <span style="font-size:12pt;font-weight:bold;color:blue;">Name of person:</span> '
    .$name.'<br /><br />
           <span style="font-size:12pt;font-weight:bold;color:blue;">Email of person:</span> '
    .$email.'<br /><br />
           <span style="font-size:12pt;font-weight:bold;color:blue;">Comment/Question:</span><br />
           <div style="font-size:10pt;">'
    .$comment.'</div>
           <br />

           </body>
           </html>'
    ;

           
    // create a unique boundry, can basically be anything.
           // generally people use time()
           
    $mail_boundary md5(uniqid(time()));
           
    // create header
           
    $mail_headers "From: $email\r\n"#The sender's email
           
    $mail_headers .= "MIME-Version: 1.0\r\n";
           
    $mail_headers .= "Content-type: multipart/mixed; boundary=\"$mail_boundary\"\r\n\r\n";
           if (
    file_exists($uploadfile)) {
               
    // must open the file in readonly and binary format
               
    $fp fopen($uploadfile"rb");
               
    $file fread($fpfilesize($uploadfile));
               
    // also you have to base64 it so the email can see it.
               
    $file chunk_split(base64_encode($file));
               
    // must get the name of the file
               
    $filename basename($uploadfile);
           }        
           
    // sets the email up so we can send the text and the attachment at the same time
           
    $new_mail_body .= "--{$mail_boundary}\r\n";
           
    $new_mail_body .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
           
    $new_mail_body .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
           
    $new_mail_body .= "{$message}\r\n";

           
    $new_mail_body .= "--{$mail_boundary}\r\n";
           
    $new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n";
           
    $new_mail_body .= "Content-Transfer-Encoding:base64\r\n ";
           
    $new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n";
           
    $new_mail_body .= $file "\r\n";
           
    $new_mail_body .= "--{$mail_boundary}--\r\n";

           
    // now the good stuff as we actually send the message and the attachment.
           
    mail($to,$subject,$new_mail_body,$mail_headers);

           echo 
    'Thank you for sending mail.';
       } else {
           echo 
    'Sorry, one of more of your fields is invalid.';
       }
    }

    ?>

  • #5
    Regular Coder
    Join Date
    Oct 2003
    Posts
    603
    Thanks
    2
    Thanked 1 Time in 1 Post
    i have the form mailer code all created, but i need to know where to put the attachment in the mail() function. i am sending a .XLS file from an upload form and i need it to be sent in the email as an attachment.

  • #6
    New to the CF scene
    Join Date
    May 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello Boeing747FP

    Did you end up finding an answer ?

    I have the same problem.
    Right now I'm working on the page and the code is driving me nuts, trying to get the form to accept a image so you can submit it with its application and send it to a email address with image.

    I'm yet to get this working as it's got me foxed big time .

    cheers karl

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You guys miss this part in Josh_'s post?
    PHP Code:
    $new_mail_body .= "--{$mail_boundary}\r\n"
    $new_mail_body .= "Content-Type: $new_mimetype; name=\"{$filename}\"\r\n"
    $new_mail_body .= "Content-Transfer-Encoding:base64\r\n "
    $new_mail_body .= "Content-Disposition: attachment; filename=\"{$filename}\"\r\n\r\n"
    $new_mail_body .= $file "\r\n"
    $new_mail_body .= "--{$mail_boundary}--\r\n"
    I've never done anything like this, but I'm assuming that's the code for it.. this is after the file has been uploaded of course.

    Hope that helps,
    Sadiq.


  •  

    Posting Permissions

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