The way you’re appending a foreach loop to a variable is not possible.
Try this:
$form_fields["custom4"]["html"] = "<select name="attachments[{$post->ID}][custom4]" id='attachments[{$post->ID}][custom4]'>";
foreach ($categories as $category) {
$form_fields["custom4"]["html"] .= '<option value="$category">' . $category . '</option>';
}
$form_fields["custom4"]["html"] .= "</select>";