forms - mail arrive without the file php

411

Next page works, but I do not get the file with the mail when submit.

The mail that arrives:

MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_xa18882382b8d92109533240902ace32ex"

--==Multipart_Boundary_xa18882382b8d92109533240902ace32ex Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit

...and after thet ther is the valius from the inputs ( $msg)

... then

--==Multipart_Boundary_xa18882382b8d92109533240902ace32ex

**without the file

<?php 

    if(isset($_FILES) && (bool) $_FILES) {

        $allowedExtensions = array("pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt");
        $files = array();

        foreach($_FILES as $name=>$file) {
            $file_name = $file['name']; 
            $temp_name = $file['tmp_name'];

            $path_parts = pathinfo($file_name);
            $ext = $path_parts['extension'];
            if(!in_array($ext,$allowedExtensions)) {
                die("File extensions not allowed");
            }

            $server_file = "/tmp/$path_parts[basename]";
            move_uploaded_file($temp_name,$server_file);

            array_push($files,$server_file);
        }

    $mail_to = '[email protected]'; // specify your email here

    // Assigning data from the $_POST array to variables
    $name = $_POST['sender_name'];
    $suname = $_POST['sender_suname'];
    $Bday = $_POST['sender_Bday'];
    $nowWork = $_POST['sender_work'];
    $phone = $_POST['sender_phone'];
    $mail_from = $_POST['sender_email'];
    $free = $_POST['sender_way'];

    // Construct email headers
    $headers = 'From: ' . $mail_from . "\r\n";

    // Construct email subject
    $subject = 'בקשה לרעיון עבודה' . $name;

    // Construct email body
    $msg = 'name: ' . $name . "\r\n";
    $msg .= 'suname: ' . $suname . "\r\n";
    $msg .= 'Bday: ' . $Bday . "\r\n";
    $msg .= 'nowWork: ' . $nowWork . "\r\n";
    $msg .= 'phone: ' . $phone . "\r\n";
    $msg .= 'mail_from: ' . $mail_from . "\r\n";
    $msg .= 'free: ' . $free;

    $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 

    $headers .= "\nMIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed;\n";
    $headers .= " boundary=\"{$mime_boundary}\""; 

    $message ="\n\n--{$mime_boundary}\n";
    $message .="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $message .="Content-Transfer-Encoding: 7bit\n\n" . $msg . "\n\n";
    $message .= "--{$mime_boundary}\n";

    foreach($files as $file) {
        $aFile = fopen($file,"rb");
        $data = fread($aFile,filesize($file));
        fclose($aFile);
        $data = chunk_split(base64_encode($data));
        $massage .= "Content-Type: {\"application/octet-stream\"};\n";
        $massage .= " name=\"$file\"\n";
        $massage .= "Content-Disposition: attachment;\n";
        $massage .= " filename=\"$file\"\n";
        $massage .= "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $massage .= "--{$mime_boundary}\n";
    }

    $mail_sent = mail($mail_to, $subject, $message, $headers);

        if ($mail_sent){ ?>
            <script language="javascript" type="text/javascript">
            alert('yay');

            </script>
            <?php } else { ?>
            <script language="javascript" type="text/javascript">  
            alert('nay');

        </script>
        <?php     
        }

   }    
?>

What's wrong? Why I'm not getting the file if I do not get any php warning from the page?

526

Answer

Solution:

You are typecasting your $_FILES (array) to be a bool, in your if statement. You should write is_array($_FILES) instead of (bool) $_FILES, then you should get your file.

People are also looking for solutions to the problem: php - How to display many to one relation objects in index action

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.