zinch33 Posted February 9, 2012 Share Posted February 9, 2012 Hello I'm completely new with php and need some help. Basically I'm at a level that I can tweak it but I'm still learning on how to write it. I have a form within an html page. This form has multiple text inputs and a upload button. Once the user hits the submit button it triggers the mailer.php. I've done tons of searches online today and in forums and people keep mentioning php mailer as a good tool. I looked into this and was confused on how to implement it. I don't need the info sent to a database, just emailed. The uploaded file just needs to be sent to a temp directory and then deleted after the file is emailed. The inputs would be in the body of the email and the uploaded file would be attached. This is the php code I'm working with for a starting point. Any help would be greatly appreciated. <?PHP $to = "[email protected]"; $subject = "New Employee Application"; $headers = "From:" . $from; $forward = 1; $location = ""; $date = date ("l, F jS, Y"); $redirect = "../thanks.html"; $msg = "New Employee Application. It was submitted on $date.\n\n"; if ($_SERVER['REQUEST_METHOD'] == "POST") { foreach ($_POST as $key => $value) { $msg .= ucfirst ($key) ." : ". $value . "\n"; } } else { foreach ($_GET as $key => $value) { $msg .= ucfirst ($key) ." : ". $value . "\n"; } } mail($to, $subject, $msg, $headers); if ($forward == 1) { header ("Location: $redirect"); } else { echo "Your application has been sent."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/ Share on other sites More sharing options...
zinch33 Posted February 9, 2012 Author Share Posted February 9, 2012 So I looked into php mailer. But I'm having trouble putting all the pieces together. The email comes through but I need help coding for the attachment. I have users uploading a resume, however I won't know the name or file type(pdf or doc). Plus I don't know what directory the file goes once it's uploaded. Do I need to tell it in my html where to go so I can use the specific code below? This is the new code. <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsMail(); $mail->From="[email protected]"; $mail->FromName="My site's mailer"; $mail->Sender="[email protected]"; $mail->AddAddress("[email protected]"); $mail->Subject = "Test 1"; $mail->IsHTML(true); $mail->AddAttachment('tmp/test.pdf'); $mail->Body = "<h1>Test 1 of PHPMailer html</h1><p>This is a test</p>"; $mail->AltBody="To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { echo "Letter sent"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316213 Share on other sites More sharing options...
darkfreaks Posted February 9, 2012 Share Posted February 9, 2012 http://stackoverflow.com/questions/6322610/phpmailer-attachment might come in handy for what you are asking Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316216 Share on other sites More sharing options...
zinch33 Posted February 9, 2012 Author Share Posted February 9, 2012 Checked your link. It was slightly confusing since I'm a novice at php. Will this help with finding the file that is uploaded by the user and attaching it to the sent email? Not sure where to go with this. for ($i = 0; $i <= 2; $i++) { if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) { $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']); } } Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316254 Share on other sites More sharing options...
darkfreaks Posted February 9, 2012 Share Posted February 9, 2012 that will upload the file to a folder then add it as a mail attachment is thhat not what you want Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316311 Share on other sites More sharing options...
zinch33 Posted February 9, 2012 Author Share Posted February 9, 2012 Yes it's what I'm looking for. I've been looking at php code too long and it's starting to really confuse me. I'm just getting into php and trying to learn it. I tried adding this code to my existing code and I'm still not getting an attachment. Am I placing the code in the correct spot in the script or am I missing something? I have a tmp directory that's 777 that's in the root folder. <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsMail(); $mail->From="[email protected]"; $mail->FromName="My site's mailer"; $mail->Sender="[email protected]"; $mail->AddAddress("[email protected]"); $mail->Subject = "Test 1"; $location = ""; $redirect = "../thanks.html"; $sender_firstname = $_POST['firstname']; $sender_middlename = $_POST['middlename']; $sender_lastname = $_POST['lastname']; $sender_streetaddress = $_POST['streetaddress']; $sender_apartment = $_POST['apartment']; $sender_city = $_POST['city']; $sender_state = $_POST['state']; $sender_zipcode = $_POST['zipcode']; $sender_homenumber = $_POST['homenumber']; $sender_cellnumber = $_POST['cellnumber']; $sender_email = $_POST['email']; $mail->IsHTML(true); for ($i = 0; $i <= 2; $i++) { if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) { $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']); } } $mail->Body = "New Employee Application <br />\r\n <br /> Personal Information <br />\r\n Name: $sender_firstname <br />\r\n Address: $sender_streetaddress <br />\r\n Apartment: $sender_apartment <br />\r\n City: $sender_city <br />\r\n State: $sender_state <br />\r\n Zip: $sender_zipcode <br />\r\n Home Phone: $sender_homenumber <br />\r\n Cell Phone: $sender_cellnumber <br />\r\n Email: $sender_email <br />\r\n <br />"; $mail->AltBody="To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { header ("Location: $redirect"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316337 Share on other sites More sharing options...
darkfreaks Posted February 9, 2012 Share Posted February 9, 2012 http://www.html-form-guide.com/email-form/php-email-form-attachment.html this tutorial explains more in depth how to use a temp folder to upload from a form and add it into the attachment. Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316353 Share on other sites More sharing options...
zinch33 Posted February 11, 2012 Author Share Posted February 11, 2012 Ok, so I have added code to move the file to the uploads folder. However I'm still getting no attachment. I ran print_r($mail->GetAttachments()); to check to see if it's getting attached but it's returning Array ( ) Warning: Cannot modify header information - headers already sent by (output started at /home/content/37/8916437/html/php/mailer.php:6) in /home/content/37/8916437/html/php/mailer.php on line 176. Here's my latest code. <?php require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsMail(); print_r($mail->GetAttachments()); //Settings $mail->From="[email protected]"; $mail->FromName="Company Site"; $mail->Sender="[email protected]"; $mail->AddAddress("[email protected]"); $mail->Subject = "Test"; $location = ""; $redirect = "../thanks.html"; $allowed_extensions = array("pdf", "doc", "docx"); $upload_folder = "../uploads"; //Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The uploaded file is not supported file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); } //copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } } //Settings For Inputs $sender_firstname = $_POST['firstname']; $sender_middlename = $_POST['middlename']; $sender_lastname = $_POST['lastname']; $sender_streetaddress = $_POST['streetaddress']; $mail->IsHTML(true); $mail->addAttachment($path_of_uploaded_file); $mail->Body = "<h1>Form</h1> <br />\r\n <u>Personal Information</u> <br />\r\n <b>Full Name:</b> $sender_firstname $sender_middlename $sender_lastname <br />\r\n <b>Address:</b> $sender_streetaddress <br />\r\n <br />"; $mail->AltBody="To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { header ("Location: $redirect"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316898 Share on other sites More sharing options...
darkfreaks Posted February 11, 2012 Share Posted February 11, 2012 else { //other otpion if this does not work is to put header(location:'../thanks.html'); header ('Location:'.$redirect); } Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316900 Share on other sites More sharing options...
zinch33 Posted February 11, 2012 Author Share Posted February 11, 2012 tried using code below but the same error comes up header ("Location: ../thanks.html"); Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316902 Share on other sites More sharing options...
darkfreaks Posted February 11, 2012 Share Posted February 11, 2012 that is because you are echoing out stuff before you call the header. make sure you call the header before you output anything else HTML or TEXT wise. Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316903 Share on other sites More sharing options...
darkfreaks Posted February 11, 2012 Share Posted February 11, 2012 else { //other otpion if this does not work is to put header(location:'../thanks.html'); header ('Location:'.$redirect); //exit to stop any further execution exit; } Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316905 Share on other sites More sharing options...
darkfreaks Posted February 11, 2012 Share Posted February 11, 2012 else { //if headers are not outputted send header if(!headers_sent()){ header ('Location:'.$redirect); //exit to stop any further execution exit; } } Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316912 Share on other sites More sharing options...
darkfreaks Posted February 11, 2012 Share Posted February 11, 2012 i believe this will solve through PHP and javascript your output buffering problems with the headers and still alllow you to redirect. <?php function redirect($pURL) { if (strlen($pURL) > 0) { if (headers_sent()) { echo "document.location.href='".$pURL."';\n"; } else { header("Location: " . $pURL); } exit(); } } require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsMail(); $mail->From="[email protected]"; $mail->FromName="My site's mailer"; $mail->Sender="[email protected]"; $mail->AddAddress("[email protected]"); $mail->Subject = "Test 1"; $location = ""; $sender_firstname = $_POST['firstname']; $sender_middlename = $_POST['middlename']; $sender_lastname = $_POST['lastname']; $sender_streetaddress = $_POST['streetaddress']; $sender_apartment = $_POST['apartment']; $sender_city = $_POST['city']; $sender_state = $_POST['state']; $sender_zipcode = $_POST['zipcode']; $sender_homenumber = $_POST['homenumber']; $sender_cellnumber = $_POST['cellnumber']; $sender_email = $_POST['email']; $mail->IsHTML(true); for ($i = 0; $i <= 2; $i++) { if (file_exists($locatie.$_FILES['uploaded'.$i]['tmp_name'])) { $mail->AddAttachment($locatie.$_FILES['uploaded'.$i]['tmp_name'], $_FILES['uploaded'.$i]['name']); } } $mail->Body = "New Employee Application <br />\r\n <br /> Personal Information <br />\r\n Name: $sender_firstname <br />\r\n Address: $sender_streetaddress <br />\r\n Apartment: $sender_apartment <br />\r\n City: $sender_city <br />\r\n State: $sender_state <br />\r\n Zip: $sender_zipcode <br />\r\n Home Phone: $sender_homenumber <br />\r\n Cell Phone: $sender_cellnumber <br />\r\n Email: $sender_email <br />\r\n <br />"; $mail->AltBody="To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { redirect('../thanks.html'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1316950 Share on other sites More sharing options...
zinch33 Posted February 12, 2012 Author Share Posted February 12, 2012 Still banging my head on this one. I looked through the link you supplied as well as the code and tweaked my code. I still can't get anything attached to the email. When I submit via form I've checked my uploads folder and nothing is there. There must be a problem pulling it from the temp folder but I'm not sure what I'm dong wrong. Too new with PHP to troubleshoot this on my own. I also moved the print to after I the file is attached. But now I'm getting this Array ( ) document.location.href='../thanks.html'; Here is my latest code: <?php function redirect($pURL) { if (strlen($pURL) > 0) { if (headers_sent()) { echo "document.location.href='".$pURL."';\n"; } else { header("Location: " . $pURL); } exit(); } } require("class.phpmailer.php"); $mail = new PHPMailer(); $mail->IsMail(); //Settings $mail->From="[email protected]"; $mail->FromName="Company Site"; $mail->Sender="[email protected]"; $mail->AddAddress("[email protected]"); $mail->Subject = "Test"; $location = ""; $allowed_extensions = array("pdf", "doc", "docx"); $upload_folder = "../uploads"; //<-- this folder must be writeable by the script //Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The uploaded file is not supported file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); } //copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } } //Settings For Inputs $sender_firstname = $_POST['firstname']; $sender_middlename = $_POST['middlename']; $sender_lastname = $_POST['lastname']; $sender_streetaddress = $_POST['streetaddress']; $sender_apartment = $_POST['apartment']; $sender_city = $_POST['city']; $sender_state = $_POST['state']; $sender_zipcode = $_POST['zipcode']; $sender_homenumber = $_POST['homenumber']; $mail->IsHTML(true); $mail->addAttachment($path_of_uploaded_file); print_r($mail->GetAttachments()); $mail->Body = "<h1>Test</h1> <br />\r\n <u>Personal Information</u> <br />\r\n <b>Full Name:</b> $sender_firstname $sender_middlename $sender_lastname <br />\r\n <b>Address:</b> $sender_streetaddress <br />\r\n <b>Apartment:</b> $sender_apartment <br />\r\n <b>City:</b> $sender_city <br />\r\n <b>State:</b> $sender_state <br />\r\n <b>Zip:</b> $sender_zipcode <br />\r\n <b>Home Phone:</b> $sender_homenumber <br />\r\n <br />"; $mail->AltBody="To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()) { echo "Error sending: " . $mail->ErrorInfo;; } else { redirect('../thanks.html'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317148 Share on other sites More sharing options...
darkfreaks Posted February 12, 2012 Share Posted February 12, 2012 turn error_reporting on in your script i bet it gives you mroe detailed errors Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317158 Share on other sites More sharing options...
zinch33 Posted February 12, 2012 Author Share Posted February 12, 2012 I turned on errors and this is what I get: Notice: Undefined index: uploaded_file in /home/content/37/8916437/html/php/mailer_employee.php on line 33 line 33 in my script is: $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); Notice: Undefined variable: errors in /home/content/37/8916437/html/php/mailer_employee.php on line 50 line 50 is: " Only the following file types are supported: ".implode(',',$allowed_extensions); Notice: Undefined index: uploaded_file in /home/content/37/8916437/html/php/mailer_employee.php on line 55 document.location.href='../thanks.html'; line 55 is: $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317232 Share on other sites More sharing options...
darkfreaks Posted February 12, 2012 Share Posted February 12, 2012 have those variables been checked to see if there isset otherwise it is going to through undefined notices if(isset($_FILES['uploadedfile'])) { //code here } else { // echo error here } Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317290 Share on other sites More sharing options...
zinch33 Posted February 13, 2012 Author Share Posted February 13, 2012 I figured out the problem. I had the naming wrong on my form. I had it uploadedfile not uploaded_file like in the php code. Once I changed the name all errors went away and the file attaches. Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317388 Share on other sites More sharing options...
darkfreaks Posted February 13, 2012 Share Posted February 13, 2012 don’t forget to hit topic solved then Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317459 Share on other sites More sharing options...
zinch33 Posted February 13, 2012 Author Share Posted February 13, 2012 Thanks for helping me out. I appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/256718-code-help-for-attaching-uploaded-file-to-email/#findComment-1317463 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.