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?
echo "There was a problem trying to send your resume. Please try again and make sure, everything is filled in correctly!";
}
?>
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?
scragar
11-07-2007, 04:45 AM
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.
scragar
11-07-2007, 04:57 AM
<?
$to="myemail@myemail.com";
$message=" The following Resume was posted:\n\n
$cover\n\n";
//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:$email\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed\r\n ";
$hdr .= "boundary=$num\r\n";
$body .= "$message\r\n--$num\r\n";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/html\r\n";
$body .= "Content-Transfer-Encoding: 64bit\r\n\n";
$body .= "$message\r\n";
$body .= "--$num\r\n";
//define the attachment section
$body .= "Content-Type: $upload_type; ";
$body .= "name=\"$upload_name\"\r\n\n";
$body .= "$file\r\n";
$body .= "--$num--\r\n";
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!";
--c86bc1761736679c30e0098a4d3ff54f
Content-Type: application/msword; name="Report on Vincent in India.doc"
--c86bc1761736679c30e0098a4d3ff54f--
U have any idea wat that means, or wat is wrong?
scragar
11-07-2007, 11:02 PM
have you tried checking you you are really capable of opening the file to read it it, test it with something like:
if(!is_file($upload_name)){
echo "$upload_name cannot be found.";
};
mrdhimself
11-08-2007, 01:06 AM
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?
scragar
11-08-2007, 01:21 AM
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 :P
mrdhimself
11-08-2007, 01:35 AM
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!
scragar
11-08-2007, 01:46 AM
you'll have to show me your code relating to the upload for help on that problem.
mrdhimself
11-08-2007, 02:01 AM
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
scragar
11-08-2007, 02:58 AM
thereticaly you could make the whole thing work with a few lines, but that's very unsecure.
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); die is better than exit, and I spelt exit wrong anyway :P
if(!move_uploaded_file($_FILES['upload']['tmp_name'], $xpath)){
ecit("There was an error uploading the file, please try again!");
};
$to="my@email.com";
$message=" The following Resume was posted:\n\n
$cover\n\n";
//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:$email\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed\r\n ";
$hdr .= "boundary=$num\r\n";
$body .= "$message\r\n--$num\r\n";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/html\r\n";
$body .= "Content-Transfer-Encoding: 64bit\r\n\n";
$body .= "$message\r\n";
$body .= "--$num\r\n";
//define the attachment section
$body .= "Content-Type: $xpath; ";
$body .= "name=\"$xname\"\r\n\n";
$body .= "$file\r\n";
$body .= "--$num--\r\n";
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);
?>
Wats wrong, now it doesnt work anymore,I dont get an email at all!
scragar
11-08-2007, 03:17 AM
I spot 3 things immediatly, proberly more once I take a real look.
replace "ecit" with "die", it was a bad miss-spelling of exit, but die's faster anyway.
no \r\n in this line: "Content-Type: $xpath;"; add it like so:"Content-Type: $xpath;\r\n";
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.
mrdhimself
11-08-2007, 03:26 AM
im using text/html now, that is a mime type, right, the line looks like this
$body .= "Content-Type: text/html\r\n ";
scragar
11-08-2007, 03:32 AM
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.
mrdhimself
11-08-2007, 03:39 AM
Still not working, no email, no message saying uplaod unsuccessful...
scragar
11-08-2007, 03:44 AM
change your first few lines(before the preperation for the mail, just the upload bits) to:
$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!");
};final edit there. sorry for the 2 botched edits.
mrdhimself
11-08-2007, 03:50 AM
Still nothing, i just get a blank page...
<?
$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="myemail@me.com";
$message=" The following Resume was posted:\n\n
$cover\n\n";
//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:$email\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed\r\n ";
$hdr .= "boundary=$num\r\n";
$body .= "$message\r\n--$num\r\n";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/html\r\n";
$body .= "Content-Transfer-Encoding: 64bit\r\n\n";
$body .= "$message\r\n";
$body .= "--$num\r\n";
//define the attachment section
$body .= "Content-Type: application/msword\r\n ";
$body .= "name=\"$xname\"\r\n\n";
$body .= "$file\r\n";
$body .= "--$num--\r\n";
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);
?>
scragar
11-08-2007, 03:56 AM
you did fill in the name for you upload where it says:
YOUR FORM FEILD NAME GOES HERE
right?
mrdhimself
11-08-2007, 04:01 AM
yeah i did, does it need a '$'? cuz it still just gives me a blank page!
scragar
11-08-2007, 04:05 AM
what do you mean blank page? you talking about a blank email, or is the script exiting early or what?
mrdhimself
11-08-2007, 04:09 AM
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"!
mrdhimself
11-08-2007, 04:12 AM
is this maybe, cuz theer is no '$body' defined, only '$body .' and in the mail() it say's $body???
scragar
11-08-2007, 04:15 AM
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).
mrdhimself
11-08-2007, 04:18 AM
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?
scragar
11-08-2007, 04:23 AM
if you could zip the upload/mail page and the form and upload it here that'd be helpful.
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="guest01@inoxindia.com";
$message=" The following Resume was posted:\n\n
$cover\n\n";
//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:$email\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed\r\n ";
$hdr .= "boundary=$num\r\n";
echo "pass line X";
$body .= "$message\r\n--$num\r\n";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/html\r\n";
$body .= "Content-Transfer-Encoding: 64bit\r\n\n";
$body .= "$message\r\n";
$body .= "--$num\r\n";
echo "pass line X";
//define the attachment section
$body .= "Content-Type: application/msword\r\n ";
$body .= "name=\"$xpath\"\r\n\n";
$body .= "$file\r\n";
$body .= "--$num--\r\n";
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";
?>
mrdhimself
11-12-2007, 11:11 PM
Sorry, didnt know how to upload the zip:o
scragar
11-13-2007, 12:51 AM
$fname = "$upload";
$xname = basename($_FILES[$fname]['name'])
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.
$fname = "upload";
$xname = basename($_FILES[$fname]['name'])
removing the $ will give you the input name which is required by the rest of the script.
mrdhimself
11-13-2007, 01:27 AM
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
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="guest01@inoxindia.com";
$message=" The following Resume was posted:\n\n
${_POST['cover']}\n\n";
//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:$email\r\n";
$hdr .= "MIME-Version: 1.0\r\n";
$hdr .= "Content-Type: multipart/mixed\r\n ";
$hdr .= "boundary=$num\r\n";
$body = "$message\r\n--$num\r\n";
// body goes here incase someone can't use multipart emails, rare but can happen.
$body .= "Content-Type: text/html\r\n";
$body .= "Content-Transfer-Encoding: 64bit\r\n\n";
$body .= "$message\r\n";
$body .= "--$num\r\n";
//define the attachment section
$body .= "Content-Type: application/msword\r\n ";
$body .= "name=\"$xpath\"\r\n\n";
$body .= "$file\r\n";
$body .= "--$num--\r\n";
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);
?>
mrdhimself
11-13-2007, 02:11 AM
Now I get a message saying there was an error uploading ur file. No Email as of yet
scragar
11-13-2007, 09:28 AM
try checking for one of the 4 most common errors.
<?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!");
};
then see what message it tells you.
mrdhimself
11-13-2007, 10:58 PM
message is still the same, there was an error uploading the file, please try again!
scragar
11-14-2007, 12:10 AM
$xpath = "./".uniqid().$xname;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...
mrdhimself
11-14-2007, 12:21 AM
nope still not working... Is it maybe a problem with the definition of the server and the folder in the html? Im clueless
mrdhimself
11-14-2007, 01:09 AM
ok, i've found the error that was causing the "there was an error trying to upload ur file".
if(!move_uploaded_file($_FILES[$fname]['tmp_name'], $xpath)){
die("There was an error uploading the file, please try again!");
};
The problem is the '!' after the 'if(' Now I just get that weird email again, wit a lot of numbers and the file name!
mrdhimself
11-15-2007, 01:14 AM
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!
scragar
11-15-2007, 01:34 AM
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.
mrdhimself
11-16-2007, 03:13 AM
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!
scragar
11-16-2007, 03:47 AM
$xpath ="./upload_".uniqid().basename($_FILES[$upload]['name']);
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.
mrdhimself
11-16-2007, 10:46 PM
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...
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.