airwolfer Posted November 30, 2014 Share Posted November 30, 2014 Hi Friends , I'm new to this forum and to the PHP world , please help me with the folowing code as it drives me creazy . I'm making a project for my studies a webpage to send email with attachement , if I send it without attachement , the mail goes through . if I attach any attachement it wont send the mail this is the code , please help : <?php $to = $_POST['toemail']; $subject = $_POST['subject']; $message = $_POST['fieldDescription']; $fromemail = $_POST['fromemail']; $fromname = $_POST['fromname']; $lt= '<'; $gt= '>'; $sp= ' '; $from= 'From:'; $headers = $from.$fromname.$sp.$lt.$fromemail.$gt.$data; /* GET File Variables */ $tmpName = $_FILES['attachment']['tmp_name']; $fileType = $_FILES['attachment']['type']; $fileName = $_FILES['attachment']['name']; /* Start of headers */ /* $headers = "From: $fromname"; */ if (file($tmpName)) { /* Reading file ('rb' = read binary) */ $file = fopen($tmpName,'rb'); $data = fread($file,filesize($tmpName)); fclose($file); /* a boundary string */ $randomVal = md5(time()); $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; /* Header for File Attachment */ $headers .= "\nMIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed;\n" ; $headers .= " boundary=\"{$mimeBoundary}\""; /* Multipart Boundary above message */ $message = "This is a multi-part message in MIME format.\n\n" . "--{$mimeBoundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; /* Encoding file data */ $data = chunk_split(base64_encode($data)); /* Adding attchment-file to message*/ $message .= "--{$mimeBoundary}\n" . "Content-Type: {$fileType};\n" . " name=\"{$fileName}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mimeBoundary}--\n"; } mail($to,$subject,$message,$headers,$data); ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Email Attachment Without Upload - Excellent Web World</title> <style> body{ font-family:Arial, Helvetica, sans-serif; font-size:13px;} th{ background:#999999; text-align:right; vertical-align:top;} input{ width:181px;} </style> </head> <body> <form action="sendmail.php" method="post" name="mainform" enctype="multipart/form-data"> </h3><br> <p style="margin-left:15px"> <form action="sendmail.php" method="POST" name="mainform" enctype="multipart/form-data"> <b>From Name:</b><br> <input type="text" name="fromname" size="50"><br> <br><b>From Email:</b><br> <input type="text" name="fromemail" size="50"><br> <br><b>To Email:</b><br> <input type="text" name="toemail" size="50"><br> <br><b>Subject:</b><br> <input type="text" name="subject" size="74"><br> <tr> <th>Comments</th> <td><textarea name="fieldDescription" cols="20" rows="4" id="fieldDescription"></textarea></td> </tr> </textarea><br> <br><b>Attach Your File</b><br> <input name="attachment" type="file"> <tr> <td colspan="2" style="text-align:center;"><input type="submit" name="Submit" value="Send"><input type="reset" name="Reset" value="Reset"></td> </tr> </table> </form> </body> </html> sendmail (3).php Quote Link to comment 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.