Need help with PHP mail attachment script

Discussion in 'PHP' started by muiomuio, Apr 17, 2008.

  1. #1
    I got the Script and it works just fine as long as the attachment field has something. I am not a PHP coder therefore any help I can get is welcome.
    I ask you to please be specific.

    My problem is that the user might not attach anything and that's when I start getting errors.


    Here is the Script:

    <?php
    //config variables  $email
    
    $uploaddir = './uploads/';
    $our_email = "email@email.com" ; 
    $success_page ="../sucesso.php";
    $error_page = "../erro.php"; 
    $from_var = "Advice";
    $server_email = "email@email.com";
    $subject_line ="ADVICE - Recrutamento" ;
    $turn_on_error_for_numeric = "0";  // 1 is error, 0 is ignore.
    //***************End user defined variables::edit past here at your own risk.
    //Form fields parsed:
    $nome=$_POST['nome'];
    $morada=$_POST['morada'];
    $cpostal=$_POST['cpostal'];
    $cpostal2=$_POST['cpostal2'];
    $localidade=$_POST['localidade'];
    $email=$_POST['email'];
    $telefone=$_POST['telefone'];
    $attachment=$_POST['attachment'];
    
    if ($turn_on_error_for_numeric == "1") {
        if (!is_numeric($cpostal))
           $bounce = 1;
        if (!is_numeric($cpostal2))
           $bounce = 1;
        if (!is_numeric($telefone))
           $bounce = 1;
                    if ($bounce == 1) {    header("Location: $error_page");    }               
    }
    
    
    
    //check there is an file attachment else exit
    if (empty($attachment)) {
            //redirect to error page	
    header("Location: $error_page");
    	}
    
    $uploadfile =  basename($_FILES['attachment']['name']);
    
    
    if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) {
        //File is valid, and was successfully uploaded
        $file_name = $_FILES['attachment']['name'] ;
        $file_type = $_FILES['attachment']['type'] ;    	
    
    } else {
       //File was invalid: error out, return to form page
    header("Location: $error_page");
    }
    
    //set variables for email
    
            $to = "$nome <$our_email>"; 
            $from = "$from_var <$server_email>"; 
            $subject = "$subject_line";     
            $fileatt = "$file_name";
            $fileatttype = "$file_type"; 
            $fileattname = "$file_name";
        
            $headers = "From: $from";
    // Send the other data from form submission
    
    $form_fields = "Nome: $nome \n Morada: $morada \n CPostal:  $cpostal-$cpostal2 \n Localidade: $localidade \n Telefone:  $telefone \n Email: $email";
    
    //read file data into a variable    
            $file = fopen( $fileatt, 'rb' ); 
            $data = fread( $file, filesize( $fileatt ) ); 
            fclose( $file );
    
    //Send the message       
           $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}\"";
        
            $message = "This is a multi-part message in MIME format.\n\n" . 
                    "--{$mime_boundary}\n" . 
                    "Content-Type: text/plain; charset=\"UTF-8\"\n" . 
                    "Content-Transfer-Encoding: 7bit\n\n" . 
                     $form_fields .
                    $message . "\n\n";
        
            $data = chunk_split( base64_encode( $data ) );
                     
            $message .= "--{$mime_boundary}\n" . 
                     "Content-Type: {$fileatttype};\n" . 
                     " name=\"{$fileattname}\"\n" . 
                     "Content-Disposition: attachment;\n" . 
                     " filename=\"{$fileattname}\"\n" . 
                     "Content-Transfer-Encoding: base64\n\n" . 
                     $data . "\n\n" . 
                     "--{$mime_boundary}--\n"; 
    
            
            if( mail( $to, $subject, $message, $headers ) ) {
             
               header("Location: $success_page"); 
             
            }
            else { 
            
              header("Location: $error_page");
             
            }
      
    ?>
    
    Code (markup):
    The idea is when the field attachment is empty it sends an email with the other variables.
    When there is an attachment it sends a mail with the attachment.

    Can anyone help me out?

    Thanks
     
    muiomuio, Apr 17, 2008 IP
  2. dprundle

    dprundle Peon

    Messages:
    399
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use an IF statement to determine if the file exists or not. If it exists, attach it. If it doesn't exist then there's no need to attach it.
     
    dprundle, Apr 17, 2008 IP