El-Cap Mail app intermittent IMAP connection issues
This problem is reported so often that it is likely there are multiple causes.  But after many months of putting up with this exact issue, here is what worked for me:

Mail => Preferences => General => Check for new messages

The default setting is Automatically

Change this to Every 5 minutes

I imagine anything will work except "Automatically".  My guess is that the Automatic option leaves a connection open to the imap server, and this is what gets screwed up.  I have not tested but it also possible the problem occurs because I click the manual download button while Mac Mail is set to Automatic download.  All I know is that this has been a continuing annoyance for a long time and with this change, it works.