Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Attaching a File to a Form

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #27444
    dcp3450
    Participant

    Should be pretty simple. I need to create a form that allows the user to attach a doc and send it to a specified email address. I know how to make a mail form. However, I’ve never had the pleasure of allowing people to add attachments. Looking for some "how to" or tutorials on this. Any point in the right direction would be appreciated. Thanks!

    #69276
    lyleyboy
    Member

    To give you some pointers you would have to upload the document first as the web server have no access to the local machine to be able to pick it up.
    Then you need to attach it to the email and send it.
    Now you need to clear down the uploaded file.

    To upload a file http://www.tizag.com/phpT/fileupload.php

    To send the attachment http://www.webcheatsheet.com/php/send_email_text_html_attachment.php

    Hope this helps

    #69325
    dcp3450
    Participant

    I got the form to accept an attachment and send the attachment but if it’s a doc it’s blank:

    Code:
    $fileatt = $_FILES[‘file’][‘tmp_name’];
    $fileattType = $_FILES[‘file’][‘type’];
    $fileattName = $_FILES[‘file’][‘name’];
    $file = fopen($fileatt,’rb’);
    $data = fread($file,filesize($fileatt));
    fclose($file);

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

    $headers = “from: $email”;

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

    $content = “Info about user”;

    $content = “This is a multi-part message in MIME format.nn” .
    “–{$mime_boundary}n” .
    “Content-Type: text/plain; charset=”iso-8859-1″n” .
    “Content-Transfer-Encoding: 7bitnn” .
    $content . “nn”;

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

    $content .= “–{$mime_boundary}n” .
    “Content-Type: {$fileattType};n” .
    ” name=”{$fileattName}”n” .
    “Content-Disposition: attachment;n” .
    ” filename=”{$fileattName}”n” .
    “Content-Transfer-Encoding: base64nn” .
    $data . “nn” .
    “–{$mime_boundary}–n”;

    $send = mail( “us[email protected]”, “Form”, $content, $headers);
    if($send)
    header(‘Location: success page’);
    else
    header(‘Location: fail page’);

    The email sends correctly, there is an attachment with the correct name and file type. when it opens it is empty. An image will send with no problem.

    I got the tutorial from sitepoint: http://articles.sitepoint.com/article/a … mail-php/5

    #69331
    dcp3450
    Participant

    I downloaded the test files from Sitepoint and ran them, their doc files are blank too. Problem is I’ve compared it to other examples and they are virtually identical.

    #69157
    lyleyboy
    Member

    Well. Just goes to show you can’t trust anyone.

    I have some code but I’m not in a position to get at it and won’t be for a few hours.
    I’ll post it when I can.

    #69158
    dcp3450
    Participant

    That would be great! I want to learn how to do this so I can just hammer it out in the future since I know I’ll be asked to do this again. Thanks for your help

    #69341
    lyleyboy
    Member

    Hi, Here to post my code as requested.

    The form looks like this. Nothing special I’m sure you’ll agree.

    Code:

    Click browse to find the image you wish to upload.
    The file size cannot exceed 1 MB

    The processing PHP bit looks like this

    Code:
    1000000) {
    echo “File size is bigger than 1 MB. It must be rezised in order to upload.”;
    exit;
    }
    @copy(“$img2”, “/THIS IS MY SERVER PATH/$img2_name” )
    or die(“Upload Failed!
    Back“);
    } else {
    die(“No input file!.
    Back“);
    }

    echo “Image Uploded”;
    ?>

    Where I have "THIS IS MY SERVER PATH" you need to add in the actual path to your upload folder on your server. I’m sure you already know this but here we are in any case.
    I have found the best way to find it is to force a PHP error say by removing a ; and the running the script. The path is output on the error page.

    Incidentally I have just tested this and it worked fine for a .doc file. Uploaded it then I FTP’d it back to my machine and opened it without a problem.

    If this doesn’t work then you may have to start looking at your server setup. I’m not expert with the setup but that may be where the problem lies.

    #69242
    dcp3450
    Participant

    Is there anyway to send an attachment without uploading to the server? I’ve tried PHPMailer and I can’t get anywhere with it. I tried SwiftMailer and our mail server doesn’t like it.

    #69356
    dcp3450
    Participant

    I tested the script on an offsite location outside of our web server and it works. docs, images, etc all send and have content. 100% perfect.

    In the offsite script i left the

    ini_set("SMTP","smtp.server.com");
    ini_set("smtp_port","25");
    ini_set("sendmail_from","$email");

    is there a setting on the webserver that would prevent the data from being in the file?

    #69358
    dcp3450
    Participant

    The headers are night and day. The offsite server has all the header data. Our server only shows:

    Received: from ctc03 (10.48.8.13) by CTC01.xxxxx.com (xx.xx.x.xx)
    with Microsoft SMTP Server id x.x.xxx.x; Wed, 13 Jan 2010 12:05:25 -0500
    Date: Wed, 13 Jan 2010 12:05:24 -0500
    Subject: Test
    To: [email protected]
    From: <[email protected]>
    MIME-Version: 1.0
    Content-Type: multipart/mixed;
    boundary="==Multipart_Boundary_xf0aer9962d85709d7e9aecceac75d871x"
    Message-ID: <[email protected]>
    Return-Path: [email protected]

    #69362
    lyleyboy
    Member
    Quote:
    Is there anyway to send an attachment without uploading to the server? I’ve tried PHPMailer and I can’t get anywhere with it. I tried SwiftMailer and our mail server doesn’t like it.

    Not as far as I know. The problem with that concept is that the browser, and therefore the internet, is sandboxed from the rest of your computer. THis basically means that the browser has no access to the file to be able to attach it to the email.

    Just imagine it in a larger scale. Effectively you’re running a program (you PHP) on a computer in a different location, over the web. Compare this to trying to email a file that is on the computer upstairs from the computer downstairs. It’s not there is it?

    I hope that’s clear and if it’s too simplified I apologize.

    So it’s all working then?

    Don’t forget to clear down the temp folder that you’re uploading to.

    #69361
    dcp3450
    Participant

    I have it working now from an offsite server. If I send using the companies server it strips the header and only the file name and type come through. Offsite the whole file sends, no issues. I never had to upload it to the server.

    #69728
    zorro
    Member

    For mail forms there is a special tutorials resource – http://phpforms.net/tutorial.html. On it you can find examples of usage of forms and other information.

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘Back End’ is closed to new topics and replies.