PHP form..attachment works but..

Discussion in 'PHP' started by thumbslinger, Jun 27, 2007.

  1. #1
    I have a script that allows an attachment. Everything works fine when someone attaches a document, but what I need to do is make it ok if someone doesn't attach a file instead of showing this warning if they don't:

    Warning: Undefined variable: data in mail_module.php on line 96
    Warning: Undefined variable: fileatt_type in mail_module.php on line 98
    Warning: Undefined variable: fileatt_name in mail_module.php on line 98
    Warning: Undefined variable: fileatt_name in mail_module.php on line 98

    Here is the mail_module.php code (added 96 and 98 just to help see the line referred to towards the bottom left):

    
    <?
    /*
    -------------------------------------------------------
    FORM TO EMAIL WITH ATTACHMENT
    
    Last update: 12,Jun,03
    */
    include "config.php";
    ?>
    <link rel=stylesheet href="formstyle.css">
    <p align=center class=title>Form to Email with attachment</p>
    <?php  
    //----------------------------------------------
    function checkfileatt($fname,$fsize){
      global $max_size;
      global $banned_ext;
      $err="No file attached";
      //Checking file type in or out of banned file extensions list
        $pos1=strrchr($fname,".");
        $ftype=str_replace(".","",$pos1);
        $blist=explode("|",$banned_ext);
        for($i=0;$i<sizeof($blist)-1;$i++){
          if($ftype==$blist[$i]) $err="ERROR: Your file extension (<b>*.$ftype</b>) is not be accepted.";
        }
      //Check file size
        if(round($fsize/1024)>$max_size) $err="ERROR: Your file size (<b>" .round($fsize/1024) ."</b> Kb) is too large. We only accept <b>$max_size</b> Kb.";
      //Return the value
      return $err;
    }
    
    function formathtml($text){
      $text=stripslashes($text);
      $text=str_replace("\r\n","<BR>",$text);
      $text=str_replace("\n","<BR>",$text);
    return $text;
    }
    
    function checkemail($email){
      if(!eregi("^[A-za-z0-9\_\.-]+@[A-za-z0-9\_\.-]+.[A-za-z0-9\_-]+.*",$email) || empty($email))
      return FALSE;
      else return TRUE;
    }
    //----------------------------------------------
    
        if($mail_mode==0) $email_to=$email_to_anonymous;
        else $email_to=$my_email;
        
        if($version=="demo") {
              $email_from="anonymous@anonymous-server.com";
              $real_email_message.="<br>------------<br>Demo email sent from <a href=http://obiewebsite.sourceforge.net target=_blank>http://ObieWebsite.SourceForge.Net</a><br>Get more PHP and Javascripts for FREE!<br>";
        }
    
        $kt=0;
        //Checking valid email address
        if(!checkemail($email_to)) $kt=1;
        if(!checkemail($email_from)) $kt=1;
        if(empty($email_subject)) $kt=2;
        
        if($kt==1) print("<p>ERROR: Email address (From email or To email) is not correct</p>");
        else if($kt==2) print("<p>ERROR: Your email Subject is blank</p>");
        
        if($kt==0){
    
                    //BEGIN //----------------------------------------------
                          $email_message=formathtml($real_email_message);
                          $chked=0;
                          if ($upfile == "none" || $upfile_size==0) $chked=1;
                          if (checkfileatt($upfile_name,$upfile_size)!="No file attached") $chked=1;
                          if($chked==0) {
                        	  copy($upfile, $upfile_name);
                            $fileatt=$upfile_name;
                            $fileatt_type=$upfile_type;
                            $fileatt_name=$upfile_name;
                            $file = fopen($fileatt,'rb');
                            $data = fread($file,filesize($fileatt));
                            fclose($file);
                          }
                          $headers = "From: ".$email_from;
    
                          $semi_rand = md5(time());
                          $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
                          $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
                          $email_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" . $email_message . "\n\n";
    
     (LINE 96)                     $data = chunk_split(base64_encode($data));
    
     (LINE 98)                    $email_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";
                          $sending_ok = @mail($email_to, $email_subject, $email_message, $headers);
    
                          if($chked==0) unlink($upfile_name);
    
                    //END mode 3 and 0//----------------------------------------------
    
    
                    //Print the email result
                    if($sending_ok){
                      print("<title>Sending successful to $email_to</title><div align=center><p>Email sending successful</p>");
                    }
                    else print("<title>Sending failed to $email_to</title><div align=center><p><b>ERROR in mail server</b> Your email could not be sent</p>");
    
                    print("
                        <p><table border=0 width=80% cellspacing=1 bgcolor=#AED0F2><tr><td bgcolor=#F0F0F0>
                         <p><u><b>Sent to email</b></u><br>
                         $email_to
                         <p><u><b>From email</b></u><br>
                         $email_from
                        </table><p><br></p>
                        </td></tr></table>
                        
                        <p><b>Attachment status</b>
                        <p><table border=0 width=80% cellspacing=1 bgcolor=#AED0F2><tr><td bgcolor=#CAE4FF>");
                        if($mail_mode==0 || $mail_mode==1) {
                          if($chked==0) print("<p><b>$upfile_name</b><br>Filesize: " .round($upfile_size/1024) ." Kb | Filetype : $upfile_type");
                          else print("<p>" .checkfileatt($upfile_name,$upfile_size) ."</p>");
                        }
        }
    ?>
    </td></tr></table>
    <p><br></p>
    
    Code (markup):
    Here is the code for the config.php:
    
    <?
    /*
    -------------------------------------------------------
    FORM TO EMAIL WITH ATTACHMENT CONFIG FILE
    -------------------------------------------------------
    */
    
    /*------------------
    Mail mode:
     0 for Anonymous HTML email WITH attachment file (For TESTING only: "email_from" and "email_to" fields can be blank)
     1 for HTML email WITH attachment file
    ------------------*/
    
    	$mail_mode = 1;
    
    //------------------Max file size: Maximum size of attchment file, in (KB)
    
    	$max_size=45;
    
    //------------------My email: Your own email address
    
    	$my_email="me@here.com";
    
    
    //------------------My URL: Your own website address
    
    	$my_url="http://www.here.com";
    
    /*------------------
    List of banned file extensions:
    For security, users can not execute their script
     (uploaded by attachment file) on your server
    Note: the charactor "|" MUST be in the end
    ------------------*/
    
    	$banned_ext="php|phtml|cgi|pl|asp|jsp|c|cfm|shtml|exe|bat|com|";
    
    //------------------Script version
    
    	$version="1.2";
    ?>
    
    Code (markup):
    To recap, I just need to know how to NOT show those warning words when someone does not attach a file.

    Thanks
     
    thumbslinger, Jun 27, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    You can initialize all 4 variables at the beginning of your script to get rid of those Warnings..

    $data = '';
    $fileatt_type = '';
    $fileatt_name = '';
     
    gibex, Jun 27, 2007 IP
  3. ProgrammersTalk

    ProgrammersTalk Peon

    Messages:
    684
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Is string an object in PHP?
     
    ProgrammersTalk, Jun 27, 2007 IP
  4. thumbslinger

    thumbslinger Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    perfect, thanks!
     
    thumbslinger, Jun 28, 2007 IP