PHP_rookie Posted October 16, 2007 Share Posted October 16, 2007 Hi I've managed to put bit's and pieces of code together to create an online email form with attachment. When I click on send the email gets sent and my email client doesn't tagg it as a spam, which is good, BUT the attached file size is 0.0 Kb. The name of the attached file is correct and the format, but it's empty. If someone could help me with the missing code or faulty code I would be greatful. This is HTML --------------------------------------------------------------------------------------------------- <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Mail</title> </head> <body style="padding:3px; margin:0px;" bgcolor="#FFFFFF"> <table cellpadding="0" cellspacing="0" border="0" width="440"> <tr><td style="height:10px"></td></tr> <tr> <td colspan="2" style="text-align:justify; line-height:15px;" class="body"> <form name="frm" method="POST" action="sendmail.php" enctype="multipart/form-data"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td width="25%" class="body"> Your name</td> <td width="75%"><input type="text" name="myname" class="textfield"></td> </tr> <tr><td style="height:3px"></td></tr> <tr> <td width="25%" class="body"> Your e-mail</td> <td width="75%"><input type="text" name="myemail" class="textfield"></td> </tr> <tr><td style="height:3px"></td></tr> <tr> <td width="25%" class="body"> To e-mail</td> <td width="75%"><input type="text" name="toemail" class="textfield"></td> </tr> <tr><td style="height:3px"></td></tr> <tr> <td width="25%" class="body"> File</td> <td width="75%"><input type="file" name="myfile"></td> </tr> <tr><td style="height:10px"></td></tr> <tr> <td colspan="3" align="center"> <input type="submit" value="Send" name="submit" onClick="return validate();"> <input type="reset" value="Reset" name="reset"> </td> </tr> </table> </form> </td> </tr> <tr> <td colspan="2" align="center"> </td> </tr> </table> </body> </html> --------------------------------------------------------------------------------------------------- This is the PHP code --------------------------------------------------------------------------------------------------- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Mail sender</title> </head> <body> <? $eol="\r\n"; $myname=ucfirst($_REQUEST["myname"]); $myemail=ucfirst($_REQUEST["myemail"]); $toemail=ucfirst($_REQUEST["toemail"]); $filename=$_FILES["myfile"]["name"]; $filetype=$_FILES["myfile"]["type"]; $filesize=$_FILES["myfile"]["size"]; $filetemp=$_FILES["myfile"]["tmp_name"]; if($filetype=="application/acad" or $filetype=="image/jpeg" or $filetype=="application/pdf") { $message= $myname.$myemail; // MAIL SUBJECT $subject = "Testing PHP Mail"; // TO MAIL ADDRESS $to = $toemail; // MAIL HEADERS with attachment $fp = fopen($myfile, "rb"); $file = fread($fp, $myfile_size); $file = chunk_split(base64_encode($file)); $num = md5(time()); //Normal headers $headers = "From: ".$myname." <".$myemail.">\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: multipart/related; "; $headers .= "boundary=".$num."\r\n"; $headers .= "--$num\r\n"; // This two steps to help avoid spam $headers .= "Message-ID: <".$now." 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".$eol.$eol; $headers .= "".$message."\n"; $headers .= "--".$num."\n"; // Attachment headers $headers .= "Content-Type:".$filetype." "; $headers .= "name=\"".$filename."\"".$eol; $headers .= "Content-Transfer-Encoding: base64".$eol; $headers .= "Content-Description: ".$filename.$eol; $headers .= "Content-Disposition: attachment; "; $headers .= "filename=\"".$filename."\"".$eol.$eol; $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>'; } ?> </body> </html> --------------------------------------------------------------------------------------------------- Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/ Share on other sites More sharing options...
BlueSkyIS Posted October 16, 2007 Share Posted October 16, 2007 is $myfile_size ever defined before this line? $file = fread($fp, $myfile_size); Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/#findComment-371136 Share on other sites More sharing options...
PHP_rookie Posted October 18, 2007 Author Share Posted October 18, 2007 I've posted all the code. So that's it So I'm guess that's missing. How should I define it? Sorry totally new to PHP. Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/#findComment-372605 Share on other sites More sharing options...
BlueSkyIS Posted October 18, 2007 Share Posted October 18, 2007 it looks like you already got it, just need to change a variable name. update the fread like so: $file = fread($fp, $filesize); Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/#findComment-372610 Share on other sites More sharing options...
PHP_rookie Posted October 18, 2007 Author Share Posted October 18, 2007 Thanks! I did update. But it still doesn't work. The email gets sent and everything works except the file size. It's almost like it creates an empty attached file. Do you have any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/#findComment-372668 Share on other sites More sharing options...
freakstyle Posted October 18, 2007 Share Posted October 18, 2007 add an extra end of line here: // from : $headers .= "".$file."\r\n"; // to: $headers .= "".$file."\r\n\r\n"; you also need to change the following: // from : $headers .= "Content-Type:".$filetype." "; $headers .= "name=\"".$filename."\"".$eol; $headers .= "Content-Transfer-Encoding: base64".$eol; $headers .= "Content-Description: ".$filename.$eol; $headers .= "Content-Disposition: attachment; "; $headers .= "filename=\"".$filename."\"".$eol.$eol; $headers .= "".$file."\r\n"; $headers .= "--".$num."--"; // to: $message .= "--".$num."--"; $message .= "Content-Type:".$filetype." "; $message .= "name=\"".$filename."\"".$eol; $message .= "Content-Transfer-Encoding: base64".$eol; $message .= "Content-Description: ".$filename.$eol; $message .= "Content-Disposition: attachment; "; $message .= "filename=\"".$filename."\"".$eol.$eol; $message .= "".$file."\r\n\r\n"; $message .= "--".$num."--"; line breaks are usually the cause for incorrect sends. the trick is to find a solution that works for you, write a function / class and only enhance it. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/73553-htmlphp-mail-attachment-issue/#findComment-372736 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.