defeated Posted February 13, 2008 Share Posted February 13, 2008 Hi, I've got a problem. I have a job website and need people to be able to apply for a job and send their cv. I CANT get anything to attach and send from a form. I will then be using mySQL data to insert selected job details into the email. I have lost count of how many scripts I have tried using. I don't know if i'm just not getting something. I am using <input type="file" name="attachment"> in the form and can't seem to get a script to get me from there to an e-mail in my inbox with a cv attached. The closest I got was a script that sent a jpeg from another site. It was the only thing that worked for me and was useless. I have little hair left and am pulling what remains out like a madman. I have worked on this for weeks trying every search I can think of. Did I mention that this stuff is WAY beyond me at the moment? HEEEELLLLLLPPPPP!! Cheers in advance Ian. P.S. Tried the sourceforge phpmailer. Couldn't get it to work like all the others but if somebody can babystep me through the attachment part from the form as described above I'd be happy to use it. pps. ok I'm rambling now so I'll just push post..... hard to see the button through the tears! Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/ Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 Have you got enctype="multipart/form-data" in your <form> tag? Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465819 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 Yup. It tells me the message is sent and then when it arrives there is no attachment. :-\ Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465840 Share on other sites More sharing options...
haku Posted February 13, 2008 Share Posted February 13, 2008 So lets see the code you are using to create the email. It sounds like you aren't attaching it correctly. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465852 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 <?php //access mailer require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php'); $mail = new PHPMailer(); $subject=$_POST['subject']; $attachment=(is_array($_FILES['attachment']))? implode($_FILES['attachment']):$_FILES['attachment']; $mail->From = "[email protected]"; $mail->FromName = "The Website"; $mail->AddAddress("[email protected]", "JBM.ie"); $mail->AddAddress("[email protected]"); // name is optional $mail->AddReplyTo("[email protected]", "Information"); $mail->WordWrap = 50; // set word wrap to 50 characters $mail->AddAttachment("$attachment"); // add attachments $mail->AddAttachment("$attachment"); // optional name $mail->IsHTML(true); // set email format to HTML $mail->Subject = "$subject"; $mail->Body = "This is the HTML message body <b>in bold!</b>"; $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } echo "Message has been sent"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465858 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 I'm guessing that $attachment=(is_array($_FILES['attachment']))? implode($_FILES['attachment']):$_FILES['attachment']; is where the problem.... or at least one of the problems is. I put the line in because $attachment was returning as "array" when I echoed it. Ian Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465978 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 yeah, it's an array of info about the file. here is move info on the arrays values: http://us3.php.net/manual/en/features.file-upload.php I assume AddAttachment takes the path to a file? If so, change that line to: $attachment = $_FILES['attachment']['tmp_name']; Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-465983 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 Ok... so I did that. It failed to materialise in my webmail but when I sent it to another account it came up as a dat file with a different name (php435363632) or something like that..... I can smell success. The original file was a mycv.doc, but it was created on an outdated mac not Word and saved as a word file so there may be issues there. So priority now is to get the original file name to send followed by recieving as a .doc, rtf and txt would be nice too since it will only be cv's that should be sent. Thank you so so much for the input so far... getting there. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466017 Share on other sites More sharing options...
nogray Posted February 13, 2008 Share Posted February 13, 2008 using the $attachment = $_FILES['attachment']['tmp_name']; in your attachment is a security risk since you exposing the temporary saved file name. If someone uploads a php file with malicious code, they can run it directly in your server. You'll need to move the uploaded file (after you varifiy it's safe) to another folder in your site, and than send that file. More details on how to upload a file can be found here http://us3.php.net/manual/en/features.file-upload.php Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466048 Share on other sites More sharing options...
rhodesa Posted February 13, 2008 Share Posted February 13, 2008 I just read up on the phpmailer script, you should use the following: <?php //access mailer require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php'); $mail = new PHPMailer(); $subject=$_POST['subject']; $mail->From = "[email protected]"; $mail->FromName = "The Website"; $mail->AddAddress("[email protected]", "JBM.ie"); $mail->AddAddress("[email protected]"); // name is optional $mail->AddReplyTo("[email protected]", "Information"); $mail->WordWrap = 50; // set word wrap to 50 characters //Attachment $tmp_file = '.tmpupload_'.getmypid(); if(!move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_name)) die("Failed to move uploaded file"); $mail->AddAttachment($tmp_name,$_FILES['attachment']['name']); // add attachments $mail->IsHTML(true); // set email format to HTML $mail->Subject = "$subject"; $mail->Body = "This is the HTML message body in bold!"; $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->Send()) { echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } //Delete temp attachment unlink($tmp_file); echo "Message has been sent"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466053 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 Nope..... getting colder. that returned "Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File() is not within the allowed path(s): (/home/j43012:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/j43012/public_html/postjob/mailer.php on line 35 Failed to move uploaded file" I'm still really appreciating all the help though Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466245 Share on other sites More sharing options...
defeated Posted February 13, 2008 Author Share Posted February 13, 2008 http://us3.php.net/manual/en/features.file-upload.php I tried looking at the above. I tried a couple of things but couldn't get anything to work. I'm afraid it's all a little beyond me. I don't really follow whats happening there..... maybe I will down the line but that's then and this is now. I'm so lost. 6 months ago I didn't know any HTML let alone anything else. I've bitten off more than I can chew I think. Still, now that I've bitten I WILL get there. Besides this stuff is great when it works. Even decided I'm going to college this year to learn software dev properly. Meanwhile.......... ??? Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466359 Share on other sites More sharing options...
defeated Posted February 14, 2008 Author Share Posted February 14, 2008 Anybody got any further ideas? Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466633 Share on other sites More sharing options...
GameYin Posted February 14, 2008 Share Posted February 14, 2008 Look at w3schools example of PHP email forms w3schools.com/php/default.asp I am DETERMINED to help you. I haven't been programming long with PHP, 2nd worst language, and I know how it feels to be hopeless. Also If your include isn't working. Try an absolute file path. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466642 Share on other sites More sharing options...
defeated Posted February 14, 2008 Author Share Posted February 14, 2008 Looked at that GameYin, No mention of attachments. Thanks though. The thought is appreciated. . . . .not as much as a solution..... but appreciated never the less! Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466651 Share on other sites More sharing options...
GameYin Posted February 14, 2008 Share Posted February 14, 2008 Oh, well I thought you were still having trouble with the actual form, woops just read the topic title. Lmao my bad... Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-466675 Share on other sites More sharing options...
defeated Posted February 15, 2008 Author Share Posted February 15, 2008 Anybody? Plllleeeeeaaaaassssseeeee!!!!!! I've just had a bad experience trying to implement a different mailer with SMTP. Got nowhere AGAIN and screwed up something. Now the inbox in outlook says there is a new message when there isn't. I really really need the phpmailer to work! I know I'm probably one of these people that should leave what I don't know about alone but I made a promise that I'd get the site up and running after the original (bought) product looked crap and did nothing. I can't let them down. Please someone help. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-467525 Share on other sites More sharing options...
rhodesa Posted February 15, 2008 Share Posted February 15, 2008 Ok....I wrote and tested this, so it should work for you. I tested it with phpMailer v2.0.0 rc3 <?php require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php'); $mail = new PHPMailer(); $mail->From = "[email protected]"; $mail->FromName = "The Website"; $mail->AddAddress("[email protected]"); $mail->AddReplyTo("[email protected]", "Information"); $mail->IsHTML(true); $mail->WordWrap = 50; //Attachment if($_FILES['attachment']){ $uploadErrors = array( UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', UPLOAD_ERR_NO_FILE => 'No file was uploaded.', UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.', ); //Check for error if($_FILES['attachment']['error']) die("Upload error: {$uploadErrors[$_FILES['attachment']['error']]}"); //Move uploaded file $tmp_file = '.tmp_upload_'.getmypid(); //Make unique filename if(!move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_file)) die("Failed to move uploaded file to $tmp_file"); $mail->AddAttachment($tmp_file,$_FILES['attachment']['name']); } $mail->Subject = $_POST['subject']; $mail->Body = "This is the HTML message body in bold!"; $mail->AltBody = "This is the body in plain text for non-HTML mail clients"; if(!$mail->Send()){ echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit; } //Delete temp attachment unlink($tmp_file); echo "Message has been sent"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-467637 Share on other sites More sharing options...
defeated Posted February 15, 2008 Author Share Posted February 15, 2008 Hi Rhodesa, Thanks once again for your input. unfortunately it is returning the following:- Warning: move_uploaded_file(.tmp_upload_24310) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/j43012/public_html/postjob/mailer.php on line 30 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpHsJwxw' to '.tmp_upload_24310' in /home/j43012/public_html/postjob/mailer.php on line 30 Failed to move uploaded file to .tmp_upload_24310 I don't know if that means anything to you.... it certainly doesn't to me! Tried moving it to the root folder too but no difference. (it's only in the postjob folder because that is where I put stuff I'm working on ..... it's password protected and keeps out googlebots. I know it wasn't the right way of doing things and maybe wasn't very secure but at least the first way worked.... appart from showing up with the tmp name instead of the file name. I would do or try anything at this stage. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-467678 Share on other sites More sharing options...
rhodesa Posted February 15, 2008 Share Posted February 15, 2008 I assume you are managing these files through an FTP client? There should be a way in there to add WRITE permission to the /home/j43012/public_html/postjob/ folder. If you have the ability to make a folder outside of public_html that is writable, you may want to store the temporary uploads there instead. Let me know if you want to go that route. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-467681 Share on other sites More sharing options...
defeated Posted February 15, 2008 Author Share Posted February 15, 2008 I've mailed my provider for permissions to be turned on. Will keep you posted. It should just work then shouldn't it? Thank you so much once again. Ian. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-467739 Share on other sites More sharing options...
defeated Posted February 15, 2008 Author Share Posted February 15, 2008 Rhodesa, You da man!!!! McGyver had nothin' on you. I feel a bit like this.... ;D ;D ;D ;D ;D ;D ;D ;D ;D I can't possibly thank you enough. I hope I can help out someone else the same way some day. Quote Link to comment https://forums.phpfreaks.com/topic/90896-mail-with-document-attachment-from-form/#findComment-468077 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.