staples27 Posted October 5, 2009 Share Posted October 5, 2009 hey guys, i've recently used a code on a tutorial i've found on creating a php form that allows user to attach a file to the email...the form does attach the file and sends it to my email address but the problem is when i download and open the attachment it's all gibberish...example: ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ the file that i attach is a .doc how could i get it so the contents of the sent file is readable? the code is as follows: <?php $strname=ucfirst($_REQUEST["strname"]); $straddress=ucfirst($_REQUEST["straddress"]); $strcity=ucfirst($_REQUEST["strcity"]); $strstate=ucfirst($_REQUEST["strstate"]); $phone=$_REQUEST["strno"]; if($phone != ""){ $strno=$phone; } else { $strno="-"; } $stremail=$_REQUEST["stremail"]; $strcomments=ucfirst($_REQUEST["strcomments"]); $strresume_name=$_FILES["strresume"]["name"]; $strresume_type=$_FILES["strresume"]["type"]; $strresume_size=$_FILES["strresume"]["size"]; $strresume_temp=$_FILES["strresume"]["tmp_name"]; if($strresume_type=="application/octet-stream" or $strresume_type=="text/plain" or $strresume_type=="application/msword") { $message= ' <table cellspacing="0" cellpadding="8" border="0" width="400"> <tr> <td colspan="2"></td> </tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Name</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strname.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Address</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$straddress.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>City</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcity.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>State</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strstate.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Contact No.</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strno.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Email</strong></td> <td style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$stremail.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> <tr bgcolor="#eeeeee"> <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;"><strong>Comments</strong></td> </tr> <tr bgcolor="#eeeeee"> <td colspan="2" style="font-family:Verdana, Arial; font-size:11px; color:#333333;">'.$strcomments.'</td> </tr> <tr><td colspan="2" style="padding:0px;"><img src="images/whitespace.gif" alt="" width="100%" height="1" /></td></tr> </table> '; // MAIL SUBJECT $subject = "Mail with doc file attachment"; // TO MAIL ADDRESS $to="some_mail@some_domain.com"; /* // MAIL HEADERS $headers = "MIME-Version: 1.0\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\n"; $headers .= "From: Name <[email protected]>\n"; */ // MAIL HEADERS with attachment $fp = fopen($strresume_temp, "rb"); $file = fread($fp, $strresume_size); $file = chunk_split(base64_encode($file)); $num = md5(time()); //Normal headers $headers = "From: yourname<[email protected]>\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/mixed; "; $headers .= "boundary=".$num."\r\n"; $headers .= "--$num\r\n"; // This two steps to help avoid spam $headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n"; $headers .= "X-Mailer: PHP v".phpversion()."\r\n"; // With message $headers .= "Content-Type: text/html; charset=iso-8859-1\r\n"; $headers .= "Content-Transfer-Encoding: 8bit\r\n"; $headers .= "".$message."\n"; $headers .= "--".$num."\n"; // Attachment headers $headers .= "Content-Type:".$strresume_type." "; $headers .= "name=\"".$strresume_name."\"r\n"; $headers .= "Content-Transfer-Encoding: base64\r\n"; $headers .= "Content-Disposition: attachment; "; $headers .= "filename=\"".$strresume_name."\"\r\n\n"; $headers .= "".$file."\r\n"; $headers .= "--".$num."--"; // SEND MAIL @mail($to, $subject, $message, $headers); fclose($fp); echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#333333; font-weight:bold">Attachment has been sent Successfully.<br /></font>'; } else { echo '<font style="font-family:Verdana, Arial; font-size:11px; color:#F3363F; font-weight:bold">Wrong file format. Mail was not sent.</font>'; //echo "<script>window.location.href='careers.html';</script>"; } ?> note: the code is taken straight from the website i got it from but my code is pretty much similar to his.. thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/176555-php-email-attachment-problem/ Share on other sites More sharing options...
gr1zzly Posted October 5, 2009 Share Posted October 5, 2009 Don't know if this will help but are you using enctype="multipart/form-data" in the form declaration? This ensures that each form part is encoded relevant to it's content type and is essential for file uploads. Quote Link to comment https://forums.phpfreaks.com/topic/176555-php-email-attachment-problem/#findComment-930800 Share on other sites More sharing options...
staples27 Posted October 5, 2009 Author Share Posted October 5, 2009 yeah i've done that, my form looks like this: <form method="post" action="attach.php" enctype="multipart/form-data"> <div class='field'> <div class='fn'>*Name:</div> <input name='name' type='text' class='finput' /></div> <div class='field'> <div class='fn'>Company Name:</div> <input name='cname' type='text' class='finput' /></div> <div class='field'> <div class='fn'>Contact Number:</div> <input name='cnumber' type='text' class='finput' /></div> <div class='field'> <div class='fn'>*Contact Email:</div> <input name='email' type='text' class='finput' /></div> <div class='field'> <div class='fn'>Budget:</div> <input name='budget' type='text' class='finput' /></div> <div class="fieldb"> <div class='fn'>*Brief:</div> <textarea name='msg' class='finputb' /></textarea></div> <div class="field"> <div class="fn">Attach your brief</div><input type="file" name="attach"></div> <div class="field"> <div class='fn'>*What is 2 + 2?</div><input type="text" name="spam"> (Anti-spam question)</div> <input name='submit' type='submit' value='Submit' class='btn' /> </form> note: the code i got from the website the variable "$strresume" is changed to "attach" on my version Quote Link to comment https://forums.phpfreaks.com/topic/176555-php-email-attachment-problem/#findComment-930819 Share on other sites More sharing options...
gr1zzly Posted October 5, 2009 Share Posted October 5, 2009 Sorry this isn't really my area of experise, but you should check out http://www.webcheatsheet.com/php/send_email_text_html_attachment.php and http://www.hollowearth.co.uk/tech/php/email_attachments.php for guidance, paying particular attention to the "boundary" declaration. Also for future reference, annotating your code would really help, as would using a pre-formated include for the html, dropping the php vars in where you need - all that table declaration is messy and distracting! Quote Link to comment https://forums.phpfreaks.com/topic/176555-php-email-attachment-problem/#findComment-930854 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.