Automatic adding thumbnail to post

This should work:

$wp_upload_dir = wp_upload_dir();

$fileurl = "http://damianc.pl/th.jpg";
$filename = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename( $fileurl );

if ( file_put_contents( $filename, file_get_contents( $fileurl ) ) ) {
    require_once(ABSPATH . 'wp-admin/includes/image.php');

    $wp_filetype = wp_check_filetype(basename($filename), null);
    $attachment = array(
      'post_mime_type' => $wp_filetype['type'],
      'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
      'post_content' => '',
      'post_status' => 'inherit'
    );
    $attach_id = wp_insert_attachment( $attachment, $filename, $pID );
    $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
    wp_update_attachment_metadata( $attach_id, $attach_data );

    update_post_meta( $pID, '_thumbnail_id', $attach_id );
}