Page 1 of 1

Consolidate foreach error messages and redirect to function

Posted: Mon Dec 22, 2008 10:49 pm
by chris8585
I currently have a script that uses a foreach to send multiple files as attachments to an email using a form. At least one file must be attached (there are 5 slots), and when one is not attached I have it redirect to a function that displays an error message. My problem is If I don't attach a file, it redirects fine, but there is an error message for all 5 slots and I just want it to consolidate them into one. Also, when it redirects to my function it still runs through the rest of the code as if a file is attached and sends the email. I thought it would immediately stop once it was redirected through my else statement (line 60) but it's not. The foreach starts on line 19. It's probably extremely confusing to understand what I want but any suggestions are fine with me.

Code: Select all

 
function sendmail() {
   
   if ($_SERVER["REQUEST_METHOD"]=="POST"){
$sfn     = $_REQUEST['sfn']; // first name 
$bfn     = $_REQUEST['bfn']; // first name
$sln     = $_REQUEST['sln']; // last name
$bln     = $_REQUEST['bln']; // last name
$sco     = $_REQUEST['sco']; // company name
$bco     = $_REQUEST['bco']; // company name
$sad     = $_REQUEST['sad']; // address
$bad     = $_REQUEST['bad']; // address
$sfsd    = $_REQUEST['sfsd']; // apt
$bfsd    = $_REQUEST['sfsd']; // apt
$sc      = $_REQUEST['sc']; // city
$bc      = $_REQUEST['bc']; // city
$sst     = $_REQUEST['sst']; // state
$bst     = $_REQUEST['bst']; // state
$szip    = $_REQUEST['szip']; // zip
$bzip    = $_REQUEST['bzip']; // zip
$sp      = $_REQUEST['sp']; // phone
$bp      = $_REQUEST['bp']; // phone
$se      = $_REQUEST['se']; // email
$t1      = $_REQUEST['type1']; // button type
$t2      = $_REQUEST['type2']; // button type
$t3      = $_REQUEST['type3']; // button type
$t4      = $_REQUEST['type4']; // button type
$t5      = $_REQUEST['type5']; // button type
$q1      = $_REQUEST['quantity1']; // button quantity
$q2      = $_REQUEST['quantity2']; // button quantity
$q3      = $_REQUEST['quantity3']; // button quantity
$q4      = $_REQUEST['quantity4']; // button quantity
$q5      = $_REQUEST['quantity5']; // button quantity
$comments = $_REQUEST['comments']; // message
 
 
   // we'll begin by assigning the To address and message subject
   $to='email';
   $subject='Order';
 
   // get the sender's name and email address
   // we'll just plug them a variable to be used later
   $from = stripslashes($_POST['sfn' + 'sln'])."<".stripslashes($_POST['se']).">";
 
   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
 
   // 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}\"";
 
   // here, we'll start the message body.
   // this is the text that will be displayed
   // in the e-mail
   $message='Shipping
 
First Name: '.$sfn.'
Last Name: '.$sln.'
Company/Organization: '.$sco.'
Address: '.$sad.'
Floor/Suite/Department: '.$sfsd.'
City: '.$sc.'
State: '.$sst.'
Zip: '.$szip.'
Phone Number: '.$sp.'
E-mail: '.$se.'
 
Billing
 
First Name: '.$bfn.'
Last Name: '.$bln.'
Company/Organization: '.$bco.'
Address: '.$bad.'
Floor/Suite/Department: '.$bfsd.'
City: '.$bc.'
State:'.$bst.'
Zip: '.$bzip.'
Phone Number: '.$bp.'
 
Comments: '.$comments.'
 
Order #1
 
Type: '.$t1.'
Quantity: '.$q1.'
 
Order #2
 
Type: '.$t2.'
Quantity: '.$q2.'
 
Order #3
 
Type: '.$t3.'
Quantity: '.$q3.'
 
Order #4
 
Type: '.$t4.'
Quantity: '.$q4.'
 
Order #5
 
Type: '.$t5.'
Quantity: '.$q5.'';
 
   // next, we'll build the invisible portion of 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 process our uploaded files
   foreach($_FILES as $userfile){
      // store the file information to variables for easier access
      $tmp_name = $userfile['tmp_name'];
      $type = $userfile['type'];
      $name = $userfile['name'];
      $size = $userfile['size'];
      
      // 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 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.
         // NOTE: we don't set another boundary to indicate that the end of the 
         // file has been reached here. we only want one boundary between each file
         // we'll add the final one after the loop finishes.
         $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";
      }
      else {nofile();}
   }
   // here's our closing mime boundary that indicates the last of the message
   $message.="--{$mime_boundary}--\n";
   
   // now we just send the message
   if (@mail($to, $subject, $message, $headers)) {
   ?>
   <?php $pageName='Successful Order';?>
   <?php include("glbl/header.php"); ?>
   <body>
   <div id="banner">
   </div>
   <div id="nav">
   <?php include("glbl/nav.txt"); ?>
   </div>
   <div id="content">
   <span class="heading">Your order was successful!</span><br/><br/>
   You will receive and email confirmation within 24 hours detailing your pricing options.  If you DO NOT receive this email please   contact us to make sure we received your order.<br/><br/>
   You may close this window or return to our home page by clicking the link below:<br/><br/>
   
   <a href="index2.php">Return Home</a>
   </div>
   <div id="footer">
   <?php include("glbl/footer.txt"); ?>
   </div>
   <div id="margin">
   </div>
   </body>
   </html>
   <?php
   }
}
}
 
   function nofile() {
   ?>
   <?php $pageName='Error';?>
   <?php include("glbl/header.php"); ?>
   <body>
   <div id="banner">
   </div>
   <div id="nav">
   <?php include("glbl/nav.txt"); ?>
   </div>
   <div id="content">
   <span class="heading">Error</span><br/><br/>
   You must upload atleast one file.<br/><br/>
   Please click the back button to return to the order page.
   </div>
   <div id="footer">
   <?php include("glbl/footer.txt"); ?>
   </div>
   <div id="margin">
   </div>
   </body>
   </html>
   <?php
   }
?>
 

Re: Consolidate foreach error messages and redirect to function

Posted: Tue Dec 23, 2008 10:29 pm
by Syntac
Just a minor nitpick, but do you really need to use $_REQUEST? Wouldn't $_POST suffice?