If you are willing to use an additional package, I'd suggest you to use tcolorbox
. This offers you endless customization possibilities and offers you mechanisms to solve the issues you mention (no page breaks in the title, minimum 5 lines before a page break, automatic handling of the optional argument, easily customization of spacing betwee the various elements).
Here's a little example emulating your environment:
\documentclass{article}\usepackage[a5paper]{geometry}% just for the example\usepackage{lipsum}\usepackage{tcolorbox}\tcbuselibrary{most}\tcbset{mybox/.style={ before={\par\vskip3ex\noindent}, after={\par\vskip1.5ex}}}\newtcolorbox[auto counter=example]{example}[1][]{breakable,mybox,notitle after break,lines before break=5,enhanced,colback=white,colframe=white,coltitle=black,fonttitle=\bfseries,arc=0pt,outer arc=0pt,leftrule=0pt,rightrule=0pt,left=0pt,right=0pt,boxsep=0pt,toptitle=.5\baselineskip,bottomtitle=-.1\baselineskip,title=#1,enlarge top by=4ex,overlay unbroken={ \node[fill=white,anchor=west,font=\bfseries,inner xsep=0pt] at (frame.north west) (title) {Example~\thetcbcounter\enspace}; \draw[thick] (title.east|-frame.north east) -- (frame.north east); \draw[thick] (frame.south west) -- (frame.south east); },overlay first={ \node[fill=white,anchor=west,font=\bfseries,inner xsep=0pt] at (frame.north west) (title) {Example~\thetcbcounter\enspace}; \draw[thick] (title.east|-frame.north east) -- (frame.north east); },overlay last={ \draw[thick] (frame.south west) -- (frame.south east); },}\begin{document}\lipsum[5]\begin{example}[My example]\lipsum[5]\end{example}\lipsum[5]\begin{example}\lipsum[1-3]\end{example}\lipsum[5]\end{document}