Animals at nighttime
You don't need to close the barn doors at night. I have went in-game years without closing barn doors. Animals will go inside when they want with the doors open.
From the wiki, They will get unhappy if they are outside after 7PM
Animal outside past 7PM (-4 to -8 every 10 minutes does not stack with any other "every 10 minutes" effect)
But being inside has a similar effect
Player staying awake past 6PM, animal inside Barn or Coop (-4 to -8 every 10 minutes the Player stays awake, until a minimum of 150 is reached.)
You are misunderstanding how and when animals are attacked
From the wiki
If an animal has been trapped outside with the barn or coop doors closed, there is a chance they will be attacked by a wild animal during the night.
After the player goes to sleep, any events that will happen during the night (such as Bundle events) are calculated as normal. If no events are due to occur, there will be a 50% chance that the game will attempt to start a wild animal attack event. If this event proceeds, the game will go through all buildings to find a barn or coop that has their doors closed and has animals left outside.
Animals will not lose happiness until they are outside past 7PM, but they will start losing happiness if have already put them inside before then (starting at 6PM).
Thus, if you put your animals inside prior to 6PM, your animals will begin losing happiness then, because you are still awake.
Regarding the wild animal attacks, from the wiki section on this subject, it seems like the "wild animal attack" check is only performed overnight. It doesn't matter when the door is closed, so long as it is closed.
After the player goes to sleep, any events that will happen during the night (such as Bundle events) are calculated as normal. If no events are due to occur, there will be a 50% chance that the game will attempt to start a wild animal attack event. If this event proceeds, the game will go through all buildings to find a barn or coop that has their doors closed and has animals left outside.
However, from this, it sounds like this check is only done to see if any animals have been left outside and have their housing door closed behind them.
I've seen other threads that claim that leaving the doors open will not result in a wild animal attack, like this one:
I always leave the doors open. No reason not to really. If there's a downside to it, I've not noticed it. On the other hand, I hear you can potentially trap your animals outside leaving them vulnerable to wolves if you close them before they've gone in.
Another thread says something similar:
There is pretty much no downside. I leave it open all the time.
The only downside might be that goats supposedly don't like it, but mine are at 5 hearts for years now. It might take a bit longer to get them to 5 hearts though, but I didn't notice since I couldn't compare it