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 8 of 8
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Email attachment using web form

    Ok, I found this php script on the web, and I'm having trouble getting it to work. I'm trying to let a user attach a file (using a web form), then send that form to me...

    Here is the form...
    http://www.djgallagher.com/emailasanattachment1.php

    ###################
    ##### ERROR #####
    ###################

    Unfortunately, I keep getting an error:

    Warning: fopen("C:\\WINDOWS\\Desktop\\fileattachment.zip", "rb") - No such file or directory in /usr/home/v1/a0016225/html/emailasanattachment2.php on line 26

    ######################
    ##### PHP CODE #####
    ######################

    Here is the PHP on the form processing page...

    <?php

    if ($thefile)
    {

    // $thefile is the file attachment name

    $fileatt = $thefile; // Path to the file
    $fileatt_type = "application/octet-stream"; // File Type
    $fileatt_name = "attachment"; // Filename that will be used for the file as the attachment

    $email_from = "Me"; // Who the email is from
    $email_subject = "Attached File"; // The Subject of the email
    $email_message = "message here..."// Message that the email has in it

    $email_to = "me@djgallagher.com"; // Who the email is too

    $headers = "From: ".$email_from;

    // The next line is line 26, where the error is...
    $file = fopen($fileatt,'rb');
    $data = fread($file,filesize($fileatt));
    fclose($file);

    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

    $headers .= "\nMIME-Version: 1.0\n" .
    "Content-Type: multipart/mixed;\n" .
    " boundary=\"{$mime_boundary}\"";

    $email_message = "This is a multi-part message in MIME format.\n\n" .
    "--{$mime_boundary}\n" .
    "Content-Type:text/html; charset=\"iso-8859-1\"\n" .
    "Content-Transfer-Encoding: 7bit\n\n" .
    $email_message . "\n\n";

    $data = chunk_split(base64_encode($data));

    $email_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";

    $ok = @mail($email_to, $email_subject, $email_message, $headers);

    if($ok) {
    echo "The file was successfully sent!";
    } else {
    die("Sorry but the email could not be sent. Please go back and try again!");
    }

    } else {
    echo "No file chosen for download.";
    }
    ?>

    #####################
    ##### HELP!!! #####
    #####################

    It looks like the path is add a slash "\"...
    C:\\WINDOWS\\Desktop\\fileattachment.zip

    It should be...
    C:\WINDOWS\Desktop\fileattachment.zip

    How do I fix this? Please email me if you have the answer.

    Jabbamonkey

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Seems that you have magic_quotes enabled by your php-configuration. Set them to "0" or use

    stripslashes($thefile);

    to get rid of the extra backslashes.

    P.S: Answers to questions in a forum should go into the appropriate threads and not sent per email IMO. If everyone started sending answers per email, it becomes impossible for others to follow the discussion in the thread and the search function gets useless.

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hmmm... slashes gone, but still...

    Well, that got rid of extra slashes (thanks!), but I'm still getting a "file missing" error...

    Warning: fopen("C:\WINDOWS\Desktop\attachment.zip", "rb") - No such file or directory in /usr/home/v1234567/emailasanattachment2.php on line 27

    That is the location of the missing file ... but it's still not recognizing it. Is it because the file is on a local drive? If so, how do I tell ph to get the file from there? (just to reiterate, this is an attachment the user grabs from his harddrive and tries to send to me)

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Hmmm... slashes gone, but still...

    Originally posted by Jabbamonkey
    That is the location of the missing file ... but it's still not recognizing it. Is it because the file is on a local drive? If so, how do I tell ph to get the file from there? (just to reiterate, this is an attachment the user grabs from his harddrive and tries to send to me)
    Did I get that right, you are running this script on a server not on your local computer, and try to attach file from your local drive? If that's the case, then "no" is the answer. You can only open files that are accessible on the server or through HTTP-URLs. You'll have to make sure the user uploads the files previously, then you can send them.

    As to the reason why it doesn't work, you should read a little about the difference between client and server.

  • #5
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Ug...

    If you check out...

    http://www.djgallagher.com/emailasanattachment1.php

    ... you'll see that the user chooses the location of a file on his computer, then hits submit. Then I want the user's file to be sent to me.

    Do I need to upload the file to my server first? Isn't there a way to send it directly from the web form?

    Jabbamonkey

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Ug...

    Originally posted by Jabbamonkey
    Do I need to upload the file to my server first?
    Ehrm... basically yes... only that the user has to upload the file to your server by completing the web form and hitting the submit button. But seen technically, the file must first be uploaded to the server, and only after the upload has finished is your script able to send it as an attachment.

    http://se.php.net/manual/en/features.file-upload.php

    Isn't there a way to send it directly from the web form?
    The complete HTML code of the web form gets downloaded to the user's computer by help of his browser. The browser then sends data back to the server, containing everything the user entered in the form, also the data of a file if he chosed to do so.
    So I don't understand what you mean with "directly", but maybe I'm looking to technically at this matter.

  • #7
    New Coder
    Join Date
    Aug 2002
    Location
    New York, NY
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts

    deleting files from the server

    So, when I figure out how to upload a file to my server, I should be able to do the following...

    1. Have the user choose a file to upload.
    2. When they click "submit", they are taken to a confirmation page.
    3. When they click "confirm", the file (on the server) is sent to someone via email, and then it is deleted from the server.


    How do you delete a file from the server?


    Jabbamonkey

  • #8
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problem with Image attachment....

    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/related;\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";
    }


    i use above code with image attachment........ but i didn't give me any image in Email....it gives me 64 bit code.......shows below....

    Plzzzz help me ASAP...
    thanx

    --==Multipart_Boundary_x9558936c649ed70faae5683a654e6698x
    Content-Type: image/pjpeg;
    name="006.jpg"
    Content-Disposition: attachment;
    filename="006.jpg"
    Content-Transfer-Encoding: base64

    /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYF
    BgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoK
    CgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAFvAu4DASIA
    AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
    AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
    ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
    p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
    AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
    BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
    U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
    uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD9/KKK
    /Ib9gL9gL9g7xn+wd8E/F/i/9if4R6rq+q/CPw3eapqmpfDfS57m8uZdLt3lmlleAtJI7szM7Eli
    SSSTWtKk6raTMqtVUkm0frzRX5v/APDt3/gnd/0YT8Fv/DW6R/8AI9H/AA7d/wCCd3/RhPwW/wDD
    W6R/8j1v9Tl3MPrkex+kFFfm+P8Agm9/wTtB5/YJ+C3/AIa3SP8A5HpT/wAE3/8AgnYRx+wR8Fv/
    AA1mkf8AyPR9Tl3D65Hsfo/RX5vr/wAE3v8AgnaDz+wT8Fv/AA1ukf8AyPSt/wAE3v8Agnaen7BH


  •  

    Posting Permissions

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