0

I have set up an admin email via wp_mail where I have the ability set to attach multiple files. It works fine with one caveat, the last attachment (or, if the only attachment) is attached twice. I can't figure out why?

    // Handle File Upload
    if ( ! function_exists( 'wp_handle_upload' ) ) {
        require_once( ABSPATH . 'wp-admin/includes/file.php' );
    }
    $upload_overrides = array( 'test_form' => false );
    $theFiles = $_FILES['msgatt'];
    $attachments = array();
    
    // Last file added is always sent twice?!  Regardless of how many sent.
        
    
    foreach ( $theFiles['name'] as $key => $value ) {
        if ( $theFiles['name'][ $key ] ) {
                $file = array(
                    'name' => $theFiles['name'][ $key ],
                    'type' => $theFiles['type'][ $key ],
                    'tmp_name' => $theFiles['tmp_name'][ $key ],
                    'error' => $theFiles['error'][ $key ],
                    'size' => $theFiles['size'][ $key ]
                );
 
            $movefile = wp_handle_upload( $file, $upload_overrides );
            $attachments[] = $movefile[ 'file' ];   
        }
    } 

        
    if ( $movefile && ! isset( $movefile['error'] ) ) {
        echo __( 'File(s) valid, and successfully uploaded.', 'textdomain' ) . "\n";
        $attachments[] = $movefile[ 'file' ];
    } else {
        echo $movefile['error'];
        $attachments = '';
    }
    
    $body = wpautop(stripslashes($mbrmsg));

    wp_mail($emailTo, $subject, $body, $headers, $attachments);
    $emailSent = true;
}

I've done a var_dump on the $_FILES and it looks correct, just the one file (in the case of a single attachment). So it doesn't seem to be duplicated in the array.

0 Answers0