Adding an Attachment to Contact Form Using wp_mail

In short, I was simply failing to pass in the correct variable:

if ( ! function_exists( 'wp_handle_upload' ) ) {
    require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$uploadedfile       = $_FILES['uploaded_file'];
$upload_overrides   = array( 'test_form' => false );
$movefile           = wp_handle_upload( $uploadedfile, $upload_overrides );

if( $movefile ) {
    //echo "File is valid, and was successfully uploaded.\n";
    //var_dump( $movefile);
    $attachments = $movefile[ 'file' ];
    wp_mail($to, $subject, strip_tags($message), $headers, $attachments);
} else {
    echo "Possible file upload attack!\n";
}

My form field should have been:

<input type="file" name="uploaded_file" accept="application/pdf">

Leave a Comment