newtophp2 Posted February 8, 2010 Share Posted February 8, 2010 I don't know php. I'm just a mom who has been "recruited" to work on our elementary school website, usually pretty basic stuff which I'm ok with. I do have a background in database design/programming, I'm just getting into web stuff in my spare time while raising my kids. i have been asked to create a form where people can email pictures to someone... multiple pictures. i found and copied some code online and it worked - for one picture. I added in the other fields and modified the code a bit to what I thought would work, but it doesn't. can someone look at this and tell me how to attach the multiple fields fileatt1, fileatt2, fileatt3 etc?? first i have a form.php file: <!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>Form Sample</title> </head> <body> <form id="form1" name="form1" method="post" action="form_mailer.php"> <table width="455" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="175" height="44" align="center"><label for"name">name</label></td> <td width="280"><input name="name" type="text" id="name" size="30" /> </td> </tr> <tr> <td height="45" align="center"><label for="email">email</label></td> <td><input name="email" type="text" id="email" size="30" /></td> </tr> <tr> <td height="41" align="center"><label for="question">question</label></td> <td><textarea name="question" cols="30" rows="5" id="question"></textarea></td> </tr> <tr> <td height="38"> </td> <td><label> <input type="submit" name="Submit" id="Submit" value="Submit" /> </label></td> </tr> </table> </form> </body> </html> then an email.php file: <?php ob_start(); $to = 'myemail.com'; $name = $_POST['name']; $email = $_POST['email']; $subject = 'Web Submission'; $comments = $_POST['comments']; $hidden = $_POST['hidden']; $from = $email; print (' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> <title>Email</title> <script type="text/javascript"> /*<![CDATA[*/ function progress(){ intWidth = parseInt(document.getElementById("container1").style.width) + 1; if(intWidth <= 400){ document.getElementById("container1").style.width = intWidth+"px"; }else{ document.getElementById("container1").style.width = 0; } setTimeout("progress()",300); } /*]]>*/ </script> </head> <body> '); //Do a reg_ex check on the email if (!empty ($hidden)) { $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"; if (eregi($regexp, $email)) { }else{ $email = ''; } } // End of email checking if (empty ($hidden)) { print ('<div id="container2"> <h1 id="content_h1"><a name="text"> E-mail </a></h1> <p>Please use the following form to e-mail us:</p> '); include ("form.php"); print ('</div> <div id="container0" style="display: none;"> <p style="font-size: 15pt; font-family: sans-serif; color:#fd6700; background:#fff;"> Loading... </p> <div id="container1" style="width:0px; height:5px; background-color:#fd6700; margin-top:0px; text-align: left;"></div> <p>Please be patient while your data is processed. This may take a few moments especially if you are uploading a file.</p> </div> '); } if (!empty ($hidden)) { if ($_FILES['fileatt1']['error'] == 1){ print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1> <p>The maximum file size that can be uploaded using this form is 2 megabytes. </p>'); }elseif ( (!empty ($name)) && (!empty ($email)) && (!empty ($comments))&& (!empty ($subject))) { // Get html message content $form_data = "<p>This email is from <span class=\"bold\">$name</span> \n\n "; $form_data .= "<p>$comments</p>"; $message = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \n" . " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> \n" . "<html xmlns=\"http://www.w3.org/1999/xhtml\"> \n" . "<head> \n" . " <meta http-equiv=\"content-type\" content= \n" . " \"text/html; charset=iso-8859-1\" /> \n" . "<style type=\"text/css\"> \n" . "body { font-size: 9pt; font-family: verdana, sans-serif; color: #000; background:#fff; } \n" . ".bold { font-weight: bold; } \n" . "</style> \n" . "</head> \n" . "<body>$form_data \n" . "</body> \n" . "</html> \n\n"; // Obtain file upload vars $fileatt1 = $_FILES['fileatt1']['tmp_name']; $fileatt_type = $_FILES['fileatt1']['type']; $fileatt_name = $_FILES['fileatt1']['name']; $headers = "From: $from"; if (is_uploaded_file($fileatt1)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt1,'rb'); $data = fread($file,filesize($fileatt1)); fclose($file); if (is_uploaded_file($fileatt2)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt2,'rb'); $data = fread($file,filesize($fileatt2)); fclose($file); if (is_uploaded_file($fileatt3)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt3,'rb'); $data = fread($file,filesize($fileatt3)); fclose($file); if (is_uploaded_file($fileatt4)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt4,'rb'); $data = fread($file,filesize($fileatt4)); fclose($file); if (is_uploaded_file($fileatt5)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt5,'rb'); $data = fread($file,filesize($fileatt5)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the html message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); //We now have everything we need to write the portion of the message that contains the file attachment. Here's the code: // Add file attachment to the message $message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" . " name=\"{$fileatt_name}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; }else{ // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the html message $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; } //That completes the modifications necessary to accommodate a file attachment. We can now send the message with a quick call to mail: // Send the message mail($to, $subject, $message, $headers); $body = "Dear $name, \n\nThank you for your email. We will contact you as soon as possible regarding the matter. \n \n"; mail ($email, "Re: Web Submission", $body, 'From:myemail.com'); print ('<h1 id="content_h1"><a name="text"> Thank you </a></h1> <p>We will contact you as soon as possible. You will receive an automatic e-mail immediately confirming the reception of your email.</p>'); }else{ print ('<h1 id="content_h1"><a name="text">There has been an error</a></h1> <p>Please fill in all the compulsory fields correctly and then resubmit the form. Thank you.</p>'); include ("form.php"); } } // This is the end of the insert print (' </div> </body> </html> '); ob_end_flush(); ?> Thank you! Link to comment https://forums.phpfreaks.com/topic/191304-please-help-with-email-attachments-code/ Share on other sites More sharing options...
newtophp2 Posted February 8, 2010 Author Share Posted February 8, 2010 Rats!! That was not the form.php form that worked! Sorry I was trying a lot of different ways and posted the wrong thing. Here is the form.php that worked along side the email.php -- again, before I added the additional fields (fileatt2, 3, 4, 5), it worked with just one fileatt. <body bgcolor="#E1E1E1"> <div id="form"> <form action="email.php" enctype="multipart/form-data" method="post" onsubmit=" document.getElementById('container0').style.display=''; document.getElementById('container2').style.display='none'; progress(); return true;"> <p align="center"><font size="6" color="#800000"><b>My School<br> </b></font>Test Page</p> <fieldset id="fieldset"> <label for="name">Contact name<span style="color: red;">*</span>:</label> <input <?php if (!empty ($hidden)) { if (empty ($name)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="name" type="text" name="name" value="<?php print "$name"; ?>" tabindex="1" /> <br /> <label for="email">E-mail address<span style="color: red;">*</span>:</label> <input <?php if (!empty ($hidden)) { if (empty ($email)) { print 'style="background: pink;"'; } } ?> class="form_elements" id="email" type="text" name="email" value="<?php print "$email"; ?>" tabindex="1" /> <br /> <label for="comments">Comments<span style="color: red;">*</span>:</label> <textarea <?php if (!empty ($hidden)) { if (empty ($comments)) { print 'style="background: pink;"'; } } ?>class="form_elements" id="comments" name="comments" cols="75" rows="5" tabindex="1"><?php print "$comments"; ?></textarea> <br /><br /> <label for="fileatt1">Attach Picture 1:</label> <input id="fileatt1" type="file" name="fileatt1" tabindex="1" /> <p> <label for="fileatt2">Attach Picture 2:</label> <input id="fileatt2" type="file" name="fileatt2" tabindex="2" /> </p> <p> <label for="fileatt3">Attach Picture 3:</label> <input id="fileatt3" type="file" name="fileatt3" tabindex="3" /> </p> <p> <label for="fileatt4">Attach Picture4:</label> <input id="fileatt4" type="file" name="fileatt4" tabindex="4" /> </p> <p> <label for="fileatt5">Attach Picture5:</label> <input id="fileatt5" type="file" name="fileatt5" tabindex="5" /> <br /> <input type="hidden" name="hidden" value="1" /><br /> <label for="submit"><span style="color: red;">*</span> Required.<br> <br> </label> <input id="submit" type="submit" value="Send" tabindex="6" /> </p> </fieldset> </form> </div> PLEASE HELP!! Link to comment https://forums.phpfreaks.com/topic/191304-please-help-with-email-attachments-code/#findComment-1008839 Share on other sites More sharing options...
newtophp2 Posted February 8, 2010 Author Share Posted February 8, 2010 I got it!!! thanks anyway. Link to comment https://forums.phpfreaks.com/topic/191304-please-help-with-email-attachments-code/#findComment-1008933 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.