/    Sign up×
Community /Pin to ProfileBookmark

File upload, send as Attachment

I’ve made a webform, where a user can enter his/her email, upload his/her resume and write a cover letter if wanted. Now I made a php, and ist sending me the cover letter and the email, but not the uploaded file! Is there a way, that an uploaded file can be send as attachment? If yes, does anyone know how to do this?

Fanx

to post a comment
PHP

43 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 07.2007 — http://www.php.net/mail

http://www.php.net/function.mail

press ctr+F, then type in "attachment", you need to set appropriate mail headers and such.
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 07.2007 — [code=php]

<?

$to="[email protected]";

$message=" The following Resume was posted:nn

$covernn";



//open the file
$fp = fopen( $upload_name, "r");

//read the file into variables
$file = fread( $fp, $upload_size );


$file = chunk_split(base64_encode($file));


//get a random 32-character hexadecimal number
$num = md5( time() );

//define the main headers
$hdr = "From:$emailrn";
$hdr .= "MIME-Version: 1.0rn";
$hdr .= "Content-Type: multipart/mixedrn ";
$hdr .= "boundary=$numrn";
$hdr .= "--$numrn";

//define the message body
$hdr .= "Content-Type: text/htmlrn";
$hdr .= "Content-Transfer-Encoding: 64bitrnn";
$hdr .= "$messagern";
$hdr .= "--$numrn";

//define the attachment section
$hdr .= "Content-Type: $upload_type; ";
$hdr .= "name="$upload_name"rnn";
$hdr .= "$filern";
$hdr .= "--$num--rn";




If(mail($to,"Resume",$email,$hdr)){


echo "Thank you for your resume.";

}

else {

echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

}

?>

[/code]


This is my attempt. I get an email, but no attachment, and there are just some random numbers and text in the email! Any help?
Copy linkTweet thisAlerts:
@scragarNov 07.2007 — I know your supposed to put quotes around the mpboundry, but I doubt that's the problem... I'll run though it line for line and see if I can spot the problem.

you should also put attachments in the message, not the header.
Copy linkTweet thisAlerts:
@scragarNov 07.2007 — [code=php]<?
$to="[email protected]";
$message=" The following Resume was posted:nn
$covernn";
//read the file into a variable
$file = file_get_contents($upload_name);
$file = chunk_split(base64_encode($file));
//get a hopefully unique boundary.
$num = md5(time());
//define the main headers
$hdr = "From:$emailrn";
$hdr .= "MIME-Version: 1.0rn";
$hdr .= "Content-Type: multipart/mixedrn ";
$hdr .= "boundary=$numrn";


$body .= "$messagern--$numrn";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/htmlrn";
$body .= "Content-Transfer-Encoding: 64bitrnn";
$body .= "$messagern";
$body .= "--$numrn";
//define the attachment section
$body .= "Content-Type: $upload_type; ";
$body .= "name="$upload_name"rnn";
$body .= "$filern";
$body .= "--$num--rn";

If(mail($to,"Resume",$body,$hdr)){


echo "Thank you for your resume.";

}

else {

echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

}

?>

[/code]
try that edit. ?
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 08.2007 — Nope, still doesnt work, I still get this email:


[COLOR="RoyalBlue"]

Content-Type: multipart/mixed

boundary=c86bc1761736679c30e0098a4d3ff54f





The following Resume was posted:











--c86bc1761736679c30e0098a4d3ff54f

Content-Type: text/html

Content-Transfer-Encoding: 64bit



The following Resume was posted:











--c86bc1761736679c30e0098a4d3ff54f

Content-Type: application/msword; name="Report on Vincent in India.doc"





--c86bc1761736679c30e0098a4d3ff54f[/COLOR]
--

U have any idea wat that means, or wat is wrong?
Copy linkTweet thisAlerts:
@scragarNov 08.2007 — have you tried checking you you are really capable of opening the file to read it it, test it with something like:

[code=php]if(!is_file($upload_name)){
echo "$upload_name cannot be found.";
};[/code]
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 08.2007 — I mean I get the name of the document, so I guess I am able to read it. Is there maybe a problem that the file is not uploaded properly?
Copy linkTweet thisAlerts:
@scragarNov 08.2007 — you may say so, but if your not putting the correct file path, or the file doesn't get given the correct permitions when uploaded you could be prevented from reading it, even though you can upload. That's why I think you should check anyway, small errors are always the ones that cause the most problems ?
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 08.2007 — ok it says file can not be found! So I guess u were right, any suggestions on wat to do now? I'm really new to this all so, I cant *** up wit much myself!
Copy linkTweet thisAlerts:
@scragarNov 08.2007 — you'll have to show me your code relating to the upload for help on that problem.
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 08.2007 — Ohh, thats the problem, I dont hav a code for the upload:$ Any suggestions on how to write one? The the problem shud be fixed right? Is there also a way to put both of them in one script? thanx
Copy linkTweet thisAlerts:
@scragarNov 08.2007 — thereticaly you could make the whole thing work with a few lines, but that's very unsecure.

[code=php]// get unique file name.
$xname = basename($_FILES['uploadedfile']['name'])
$xpath =uniqid() .basename($_FILES['uploadedfile']['name']);

if(!move_uploaded_file($_FILES['UPLOADFORMFEILDNAME']['tmp_name'], $xpath)){
die("There was an error uploading the file, please try again!");
};
/// mail script goes here as before,
/// the file to open with file_get_contents is $xpath,
/// and the file name is $xname.

// after your mail script remove the file from the server to save space.
unlink($xpath);[/code]
die is better than exit, and I spelt exit wrong anyway ?
Copy linkTweet thisAlerts:
@mrdhimselfauthorNov 08.2007 — [code=php]
<?


// get unique file name.
$xname = basename($_FILES['uploadedfile']['name'])
$xpath =uniqid() .basename($_FILES['uploadedfile']['name']);

if(!move_uploaded_file($_FILES['upload']['tmp_name'], $xpath)){
ecit("There was an error uploading the file, please try again!");
};




$to="[email protected]";
$message=" The following Resume was posted:nn
$covernn";
//read the file into a variable
$file = file_get_contents($xpath);
$file = chunk_split(base64_encode($file));
//get a hopefully unique boundary.
$num = md5(time());
//define the main headers
$hdr = "From:$emailrn";
$hdr .= "MIME-Version: 1.0rn";
$hdr .= "Content-Type: multipart/mixedrn ";
$hdr .= "boundary=$numrn";


$body .= "$messagern--$numrn";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/htmlrn";
$body .= "Content-Transfer-Encoding: 64bitrnn";
$body .= "$messagern";
$body .= "--$numrn";
//define the attachment section
$body .= "Content-Type: $xpath; ";
$body .= "name="$xname"rnn";
$body .= "$filern";
$body .= "--$num--rn";

If(mail($to,"Resume",$body,$hdr)){


echo "Thank you for your resume.";

}

else {

echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

}

// after your mail script remove the file from the server to save space.
unlink($xpath);

?>
[/code]


Wats wrong, now it doesnt work anymore,I dont get an email at all!
Copy linkTweet thisAlerts:
@scragarNov 08.2007 — I spot 3 things immediatly, proberly more once I take a real look.

[LIST=1]
  • [*]replace "ecit" with "die", it was a bad miss-spelling of exit, but die's faster anyway.

  • [*] no rn in this line: "Content-Type: $xpath;"; add it like so:"Content-Type: $xpath;rn";

  • [*]content type requires a mimetype(instead of the file path like your using) for the contents of your email, since I have no idea what your sending I can't help too much, but if you google mimetype's there are plenty of results to help.

  • [/LIST]
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — im using text/html now, that is a mime type, right, the line looks like this
    [code=php]
    $body .= "Content-Type: text/htmlrn ";
    [/code]
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — your uploads are properly going to be in "application/msword", but you might get other formats...

    I find it highly unlikly that someone will be sending you a HTML webpage.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — Still not working, no email, no message saying uplaod unsuccessful...
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — change your first few lines(before the preperation for the mail, just the upload bits) to:
    [code=php]
    $only_allow_word = true;
    $fname = "YOUR FORM FEILD NAME GOES HERE";
    $xname = basename($_FILES[$fname]['name'])
    $xpath =uniqid() .basename($_FILES[$fname]['name']);

    if($only_allow_word && !preg_match("/.doc$/i", $xname)){
    die("sorry, microsoft word format only please.");
    };

    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    };[/code]
    final edit there. sorry for the 2 botched edits.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — Still nothing, i just get a blank page...

    [code=php]
    <?


    $only_allow_word = true;
    $fname = "YOUR FORM FEILD NAME GOES HERE";
    $xname = basename($_FILES[$upload]['name'])
    $xpath =uniqid() .basename($_FILES[$upload]['name']);

    if($only_allow_word && preg_match("/.doc$/i", $xname)){
    die("sorry, microsoft word format only please.");
    };

    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    };




    $to="[email protected]";
    $message=" The following Resume was posted:nn
    $covernn";
    //read the file into a variable
    $file = file_get_contents($xpath);
    $file = chunk_split(base64_encode($file));
    //get a hopefully unique boundary.
    $num = md5(time());
    //define the main headers
    $hdr = "From:$emailrn";
    $hdr .= "MIME-Version: 1.0rn";
    $hdr .= "Content-Type: multipart/mixedrn ";
    $hdr .= "boundary=$numrn";


    $body .= "$messagern--$numrn";
    // body goes here incase someone can't use multipart emails, rare but can happen.
    $body .= "Content-Type: text/htmlrn";
    $body .= "Content-Transfer-Encoding: 64bitrnn";
    $body .= "$messagern";
    $body .= "--$numrn";
    //define the attachment section
    $body .= "Content-Type: application/mswordrn ";
    $body .= "name="$xname"rnn";
    $body .= "$filern";
    $body .= "--$num--rn";

    If(mail($to,"Resume",$body,$hdr)){


    echo "Thank you for your resume.";

    }

    else {

    echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

    }

    // after your mail script remove the file from the server to save space.
    unlink($xpath);

    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — you did fill in the name for you upload where it says:

    YOUR FORM FEILD NAME GOES HERE

    right?
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — yeah i did, does it need a '$'? cuz it still just gives me a blank page!
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — what do you mean blank page? you talking about a blank email, or is the script exiting early or what?
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — the script is executed, at least it says the script name in my addressbar, but then the webpage stays blank, instead of saying "Thank you for your resume"!
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — is this maybe, cuz theer is no '$body' defined, only '$body .' and in the mail() it say's $body???
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — try putting a few lines like:

    echo "pass line X";

    in between a few peices of code, see if you can find the errorline.(it'll be the one where everything after it stops)

    body should default to a blank string, so you should have to worry about that(change it if you feel uncomfortable about it though).
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 08.2007 — it does absolutely nothing, ur command for checking. It just stays blank. Shud I upload all my files, so u cud maybe hav a better look at the whole?
    Copy linkTweet thisAlerts:
    @scragarNov 08.2007 — if you could zip the upload/mail page and the form and upload it here that'd be helpful.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 13.2007 — [code=html]
    <html>

    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <meta name="GENERATOR" content="Microsoft FrontPage 5.0">
    <meta name="ProgId" content="FrontPage.Editor.Document">
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
    <title>Upload your résumé</title>
    </head>



    <body>

    <form action="upload.php" method="POST" enctype="multipart/form-data">
    <!--webbot bot="FileUpload" U-File="_private/form_results.csv" S-Format="TEXT/CSV" S-Label-Fields="TRUE" B-Reverse-Chronology="FALSE" S-Destination="Uploads/" S-Category S-Assignedto S-ReviewStatus B-Process-MetaInfo="FALSE" S-Builtin-Fields startspan --><input TYPE="hidden" NAME="VTI-GROUP" VALUE="0"><input TYPE="hidden" NAME="_charset_" VALUE="windows-1252"><!--webbot bot="FileUpload" i-checksum="58985" endspan --><p>&nbsp;</p>
    <blockquote>
    <p>Email:
    <input name="email" type="text" id="email">
    </p>
    <p> Upload your résumé:&nbsp;&nbsp;&nbsp;
    <input name="upload" type="file" id="upload" size="20">
    </p>
    </blockquote>
    <p>&nbsp;</p>
    <p>If you want to add a cover letter, or any additional information (max. 1000 char.):</p>
    <p>
    <textarea name="cover" cols="100" rows="10" id="cover"></textarea>
    </p>
    <p>&nbsp;</p>
    <p><input type="submit" value="Upload" name="B1">&nbsp; <input type="reset" value="Reset" name="B2"></p>
    </form>

    </body>

    </html>
    [/code]



    [code=php]
    <?


    $only_allow_word = true;
    $fname = "$upload";
    $xname = basename($_FILES[$fname]['name'])
    $xpath =uniqid() .basename($_FILES[$fname]['name']);
    echo "pass line X";

    if($only_allow_word && preg_match("/.doc$/i", $xname)){
    die("sorry, microsoft word format only please.");
    };

    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    };
    echo "pass line X";




    $to="[email protected]";
    $message=" The following Resume was posted:nn
    $covernn";
    //read the file into a variable
    $file = file_get_contents($xpath);
    $file = chunk_split(base64_encode($file));
    //get a hopefully unique boundary.
    echo "pass line X";
    $num = md5(time());
    //define the main headers
    $hdr = "From:$emailrn";
    $hdr .= "MIME-Version: 1.0rn";
    $hdr .= "Content-Type: multipart/mixedrn ";
    $hdr .= "boundary=$numrn";
    echo "pass line X";


    $body .= "$messagern--$numrn";
    // body goes here incase someone can't use multipart emails, rare but can happen.
    $body .= "Content-Type: text/htmlrn";
    $body .= "Content-Transfer-Encoding: 64bitrnn";
    $body .= "$messagern";
    $body .= "--$numrn";
    echo "pass line X";
    //define the attachment section
    $body .= "Content-Type: application/mswordrn ";
    $body .= "name="$xpath"rnn";
    $body .= "$filern";
    $body .= "--$num--rn";
    echo "pass line X";

    If(mail($to,"Resume",$body,$hdr)){


    echo "Thank you for your resume.";

    }

    else {

    echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

    } echo "pass line X";


    // after your mail script remove the file from the server to save space.
    unlink($xpath);
    echo "pass line X";

    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 13.2007 — Sorry, didnt know how to upload the zip:o
    Copy linkTweet thisAlerts:
    @scragarNov 13.2007 — [code=php]$fname = "$upload";
    $xname = basename($_FILES[$fname]['name'])
    [/code]
    I'm sure that's an error, 2 things will happen with that script, firstly if you have auto variables active it will default to the files name, then be unable to fine an upload feild with that name, the second is that you don't have them active, $upload will be empty and the whole thing fails terribly.
    [code=php]$fname = "upload";
    $xname = basename($_FILES[$fname]['name'])
    [/code]
    removing the $ will give you the input name which is required by the rest of the script.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 13.2007 — Thanks, but there still must be something else wrong, cuz I still just get the blank page, and not the "Thank you" that I am suposed to get...:S
    Copy linkTweet thisAlerts:
    @scragarNov 13.2007 — try this.[code=php]<?php

    $only_allow_word = true;
    $fname = "upload";
    $xname = basename($_FILES[$fname]['name']);
    $xpath = uniqid().$xname;

    if($only_allow_word && !preg_match("/.doc$/i", $xname)){
    die("sorry, microsoft word format only please.");
    };

    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    };


    $to="[email protected]";
    $message=" The following Resume was posted:nn
    ${_POST['cover']}nn";
    //read the file into a variable
    $file = file_get_contents($xpath);
    $file = chunk_split(base64_encode($file));
    //get a hopefully unique boundary.
    $num = md5(time());
    //define the main headers
    $hdr = "From:$emailrn";
    $hdr .= "MIME-Version: 1.0rn";
    $hdr .= "Content-Type: multipart/mixedrn ";
    $hdr .= "boundary=$numrn";

    $body = "$messagern--$numrn";
    // body goes here incase someone can't use multipart emails, rare but can happen.
    $body .= "Content-Type: text/htmlrn";
    $body .= "Content-Transfer-Encoding: 64bitrnn";
    $body .= "$messagern";
    $body .= "--$numrn";
    //define the attachment section
    $body .= "Content-Type: application/mswordrn ";
    $body .= "name="$xpath"rnn";
    $body .= "$filern";
    $body .= "--$num--rn";

    If(mail($to,"Resume",$body,$hdr)){


    echo "Thank you for your resume.";

    }

    else {

    echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";

    }


    // after your mail script remove the file from the server to save space.
    unlink($xpath);

    ?>[/code]
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 13.2007 — Now I get a message saying there was an error uploading ur file. No Email as of yet
    Copy linkTweet thisAlerts:
    @scragarNov 13.2007 — try checking for one of the 4 most common errors.[code=php]

    <?php

    $only_allow_word = true;
    $fname = "upload";
    $xname = basename($_FILES[$fname]['name']);
    $xpath = uniqid().$xname;
    // here's the bit to add:
    $errors = array(1 => 'php.ini max file size exceeded',
    2 => 'html form max file size exceeded',
    3 => 'file upload was only partial',
    4 => 'no file was attached');
    ($_FILES[$fname]['error'] == 0)
    or die($errors[$_FILES[$fname]['error']]);
    @is_uploaded_file($_FILES[$fname]['tmp_name'])
    or die('not an HTTP upload');
    // resume:
    if($only_allow_word && !preg_match("/.doc$/i", $xname)){
    die("sorry, microsoft word format only please.");
    };

    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    };
    [/code]
    then see what message it tells you.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 14.2007 — message is still the same, there was an error uploading the file, please try again!
    Copy linkTweet thisAlerts:
    @scragarNov 14.2007 — [code=php]$xpath = "./".uniqid().$xname;[/code]try just editing that line, for some reason it's unable to move the file, so maybe it's just a problem with the path, if that's not it you may have to look into the permitions of your folder...
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 14.2007 — nope still not working... Is it maybe a problem with the definition of the server and the folder in the html? Im clueless
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 14.2007 — ok, i've found the error that was causing the "there was an error trying to upload ur file".
    [code=php]
    if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
    die("There was an error uploading the file, please try again!");
    }; [/code]

    The problem is the '!' after the 'if(' Now I just get that weird email again, wit a lot of numbers and the file name!
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 15.2007 — D u hav any idea how to change that, I know i just undid the function by removing the'!' sign, but now at least I know where the problem lies. I've checked my folder permissions, and changed it to full access, but somehow it stil doesnt work!
    Copy linkTweet thisAlerts:
    @scragarNov 15.2007 — the ! means not, so if it could not move the file end on the message saying "there was an error uploading the file"...

    there is an easy way to confirm this, simply comment out the line at the bottom to unlink the file, try to upload and send, then check the folder. if the file could be uploaded you should have a new file in the directory with a stupidly long name.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 16.2007 — Ok, i've done that, but i don't get a file at all in my folder! where do I have to define, in which folder the uploaded file has to go, cuz I dont think i did that yet... maybe that's were the problem lies!
    Copy linkTweet thisAlerts:
    @scragarNov 16.2007 — [code=php]$xpath ="./upload_".uniqid().basename($_FILES[$upload]['name']);[/code]
    should be enough to fix the uploads problem if it's a problem with the file name, if it's a directory permitions problem then you chould check the permitions on the directory.
    Copy linkTweet thisAlerts:
    @mrdhimselfauthorNov 17.2007 — Still doesnt fix the upload problem. Wats the name I hav to give the folder, where the uploads are supposed to go to? With permissios, do u mean who is aloud to write, read, etc. the folder, in that case I've changed that to everyone! So the problem shudnt lie there...
    ×

    Success!

    Help @mrdhimself spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...