izbryte Posted October 14, 2006 Share Posted October 14, 2006 I have an email form where the visitor can upload a file as an attachment that then gets emailed. If they choose not to upload a file the email still comes through with an attachment (txt file).How can I get rid of the attachment if there is no uploaded file?Here's the code I have for the attachment:[code] // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } }[/code] Link to comment https://forums.phpfreaks.com/topic/23922-upload-file-in-email-form/ Share on other sites More sharing options...
dymon Posted October 15, 2006 Share Posted October 15, 2006 Show please a bigger part of code from this email sender, in special where it sends the mail. Link to comment https://forums.phpfreaks.com/topic/23922-upload-file-in-email-form/#findComment-108993 Share on other sites More sharing options...
izbryte Posted October 16, 2006 Author Share Posted October 16, 2006 Ok. Here's the whole thing[code]<?php # Script 3.12 - register.php// Set the page title and include the HTML header.$page_title = 'Submit Your Resume!';include ('header.tpl');if (isset($_POST['submit'])) { // Handle the form. // Check for a first name. if (strlen($_POST['fname']) > 0) { $fn = TRUE; $fname = ($_POST['fname']); } else { $fn = FALSE; echo '<p class="red">You forgot to enter your first name.</p>'; } // Check for a last name. if (strlen($_POST['lname']) > 0) { $ln = TRUE; $lname = ($_POST['lname']); } else { $ln = FALSE; echo '<p class="red">You forgot to enter your last name.</p>'; } // Check for an email address. if (strlen($_POST['email']) > 0) { $e = TRUE; $email = ($_POST['email']); } else { $e = FALSE; echo '<p class="red">You forgot to enter your email address.</p>'; } // Check for phone 1. if (strlen($_POST['phone']) > 0) { $p1 = TRUE; $phone = ($_POST['phone']); } else { $p1 = FALSE; echo '<p class="red">You forgot to enter your phone number.</p>'; } // Check for address. if (strlen($_POST['phone2']) > 0) { $p2 = TRUE; $phone2 = ($_POST['phone2']); } else { $p2 = ' '; } // Check for address. if (strlen($_POST['address']) > 0) { $a1 = TRUE; $address = ($_POST['address']); } else { $a1 = FALSE; echo '<p class="red">You forgot to enter your address.</p>'; } // Check for address 2. if (strlen($_POST['address2']) > 0) { $a2 = TRUE; $address2 = ($_POST['address2']); } else { $address2 = 'None specified.'; } // Check for city. if (strlen($_POST['city']) > 0) { $c = TRUE; $city = ($_POST['city']); } else { $c = FALSE; echo '<p class="red">You forgot to enter your city.</p>'; } // Check for state. if (strlen($_POST['state']) > 0) { $s = TRUE; $state = ($_POST['state']); } else { $s = FALSE; echo '<p class="red">You forgot to enter your state.</p>'; } // Check for zip. if (strlen($_POST['zip']) > 0) { $z = TRUE; $zip = ($_POST['zip']); } else { $z = FALSE; echo '<p class="red">You forgot to enter your zip code.</p>'; } // Check for country. if (strlen($_POST['country']) > 0) { $co = TRUE; $country = ($_POST['country']); } else { $country = ' '; } // Check for undergrad school. if (strlen($_POST['undergrad_school']) > 0) { $us= TRUE; $undergrad_school = ($_POST['undergrad_school']); } else { $undergrad_school = ' '; } // Check for undergrad degree. if (strlen($_POST['undergrad_degree']) > 0) { $ud= TRUE; $undergrad_degree = ($_POST['undergrad_degree']); } else { $undergrad_degree = ' '; } // Check for grad school. if (strlen($_POST['grad_school']) > 0) { $gs= TRUE; $grad_school = ($_POST['grad_school']); } else { $grad_school = ' '; } // Check for grad degree. if (strlen($_POST['grad_degree']) > 0) { $gd= TRUE; $grad_degree = ($_POST['grad_degree']); } else { $grad_degree = ' '; } // Check for current position. if (strlen($_POST['position']) > 0) { $cp= TRUE; $position = ($_POST['position']); } else { $position = ' '; } // Check for market sector. if($_POST['market_sector'] == 'NULL') { $market_sector = ' '; } else { $ms= TRUE; $market_sector = ($_POST['market_sector']); } // Check for current employer. if (strlen($_POST['employer']) > 0) { $ce= TRUE; $employer = ($_POST['employer']); } else { $employer = ' '; } // Check for years experience. if (strlen($_POST['yrs_exp']) > 0) { $ye= TRUE; $yrs_exp = ($_POST['yrs_exp']); } else { $yrs_exp = ' '; } // Check for relocate. if($_POST['relocate'] == 'NULL') { $relocate = ' '; } else { $r= TRUE; $relocate = ($_POST['relocate']); } // Check for relocate area. if (strlen($_POST['relocate_area']) > 0) { $ra= TRUE; $relocate_area = ($_POST['relocate_area']); } else { $yrelocate_area = ' '; } if ($fn) { // If everything's okay. // we'll begin by assigning the To address and message subject $to="[email protected]"; $subject="Resume Submitted"; // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fname'])." ".stripslashes($_POST['lname'])."<".stripslashes($_POST['email']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Name: $fname $lname\n\nEmail: $email\n\nPhone: $phone\n\nAlternate Phone: $phone2\n\nAddress 1:\n$address\n$city, $state $zip $country\n\nAddress 2: \n$address2\n$city, $state $zip $country\n\nUndergraduate Institution: $undergrad_school\n\nUndergraduate Degree: $undergrad_degree\n\nGraduate School: $grad_school\n\nGraduate Degree: $grad_degree\n\nCurrent Position: $position\n\nMarket Sector: $market_sector\n\nCurrent Employer: $employer\n\nYears of Experience: $yrs_exp\n\nWilling to Relocate? $relocate\n\nRelocation Preference: $relocate_area"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } } // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; @mail($to, $subject, $message, $headers); echo "<br><p align='center' class='red'>Thank you $fname $lname. <br>Your resume has been sent successfully. <br>You will be contacted by us shortly.</p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p>"; } else { echo "<p align='center' class='red'>Please go back and try again.</p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p><p> </p>"; }} else { // Display the form.?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"><table width="760" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="95%" align="center" valign="top"><div align="left"> <p><img src="images/titles_resume.gif" alt="Submit Your Resume" width="221" height="47" hspace="10"></p> <table width="95%" border="0" align="center" cellpadding="4" cellspacing="2"> <tr> <td colspan="2" bgcolor="#329DD3" class="bold_white">Personal Information </td> <td width="1" rowspan="2" bgcolor="#FFFFFF"><img src="images/spacer.gif" width="1" height="1"></td> <td colspan="2" bgcolor="#329DD3" class="bold_white">Education</td> </tr> <tr bgcolor="#A5D9F1"> <td><b>First Name: </b></td> <td><input name="fname" type="text" id="fname" maxlength="25"></td> <td><b>Undergraduate Institution: </b></td> <td><input name="undergrad_school" type="text" id="undergrad_school"></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Last Name: </b></td> <td><input name="lname" type="text" id="lname" maxlength="30"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Undergraduate Degree: </b></td> <td><input name="undergrad_degree" type="text" id="undergrad_degree"></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Email Address: </b></td> <td><input name="email" type="text" id="email" maxlength="50"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Graduate Institution: </b></td> <td><input name="grad_school" type="text" id="grad_school"></td> </tr> <tr bgcolor="#A5D9F1"> <td bgcolor="#A5D9F1"><b>Phone:</b></td> <td><input name="phone" type="text" id="phone" maxlength="12"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Graduate Degree:</b></td> <td><input name="grad_degree" type="text" id="grad_degree"></td> </tr> <tr> <td bgcolor="#A5D9F1"><b>Alt. Phone </b></td> <td bgcolor="#A5D9F1"><input name="phone2" type="text" id="phone2" maxlength="12"></td> <td> </td> <td colspan="2" bgcolor="#329DD3" class="bold_white">Professional Information </td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Address 1:</b></td> <td><input name="address" type="text" id="address" maxlength="60"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Current Postition:</b></td> <td><input name="position" type="text" id="position"></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Address 2: </b></td> <td><input name="address2" type="text" id="address2" maxlength="60"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Market Sector:</b></td> <td><select name="market_sector" size="1" class="small_text style1" id="market_sector"> <option value="NULL">Choose one...</option> <option value="Hospitality/Hotel & Casino Projects">Hospitality/Hotel & Casino Projects</option> <option value="Healthcare/Hospital/Lab Projects">Healthcare/Hospital/Lab Projects</option> <option value="Retail, Mixed-use & Urban Planning Projects">Retail, Mixed-use & Urban Planning Projects</option> <option value="K-12 Public School & Higher Education Projects">K-12 Public School & Higher Education Projects</option> <option value="Single Family, Multi-family & Hi-rise Condo Projects">Single Family, Multi-family & Hi-rise Condo Projects</option> <option value="Commercial Office Buildings & Corporate Workplace Interiors">Commercial Office Buildings & Corporate Workplace Interiors</option> <option value="Industrial Parks & Tilt-up Projects">Industrial Parks & Tilt-up Projects</option> <option value="Government/Municipal Projects">Government/Municipal Projects</option> <option value="Theme Park/Entertainment Projects">Theme Park/Entertainment Projects</option> </select></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>City:</b></td> <td><input name="city" type="text" id="city" maxlength="30"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Current Employer: </b></td> <td><input name="employer" type="text" id="employer"></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>State:</b></td> <td><input name="state" type="text" id="state" maxlength="2"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Years of Experience: </b></td> <td><input name="yrs_exp" type="text" id="yrs_exp"></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Zip Code: </b></td> <td><input name="zip" type="text" id="zip" maxlength="5"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Are you willing to relocate? </b></td> <td><b> <select name="relocate" size="1" id="relocate"> <option value="NULL" selected>Choose one...</option> <option value="Yes">Yes</option> <option value="No">No</option> </select> </b></td> </tr> <tr bgcolor="#A5D9F1"> <td><b>Country:</b></td> <td><input name="country" type="text" id="country" maxlength="25"></td> <td bgcolor="#FFFFFF"> </td> <td><b>Relocation Preference:<br> (State/Region/Country) </b></td> <td><input name="relocate_area" type="text" id="relocate_area"></td> </tr> <tr align="center" valign="middle" bgcolor="#FFFFFF"> <td height="1" colspan="5"><img src="images/spacer.gif" width="1" height="1"></td> </tr> <tr align="center" valign="middle" bgcolor="#A5D9F1"> <td colspan="5"><b>Upload your resume: <input type="file" name="filename"> </b></td> </tr> <tr align="center" valign="middle" bgcolor="#A5D9F1"> <td colspan="5"><input type="submit" name="submit" value="Submit"></td> </tr> </table></form><!-- End of Form --> <?php}include ('footer.tpl'); // Include the HTML footer.?>[/code] Link to comment https://forums.phpfreaks.com/topic/23922-upload-file-in-email-form/#findComment-109246 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.