def create_imap_connection(host, port, ssl_required, use_timeout=True):
 use_ssl = (port == 993)
 timeout = (120 if use_timeout else None)
 context = create_default_context()
 conn = IMAPClient(host, port=port, use_uid=True, ssl=use_ssl, ssl_context=context, timeout=timeout)
 if (not use_ssl):
  if conn.has_capability('STARTTLS'):
   try:
    conn.starttls(context)
   except Exception:
    if (not ssl_required):
     log.warning('STARTTLS supported but failed for SSL NOT required authentication', exc_info=True)
    else:
     raise
  elif ssl_required:
   raise SSLNotSupportedError('Required IMAP STARTTLS not supported.')
 return conn

def create_imap_connection ( host , port , ssl_required , use_timeout = True ) : use_ssl = ( port == 993 ) timeout = ( 120 if use_timeout else None ) context = create_default_context ( ) conn = IMAPClient ( host , port = port , use_uid = True , ssl = use_ssl , ssl_context = context , timeout = timeout ) if ( not use_ssl ) : if conn . has_capability ( 'STARTTLS' ) : try : conn . starttls ( context ) except Exception : if ( not ssl_required ) : log . warning ( 'STARTTLS supported but failed for SSL NOT required authentication' , exc_info = True ) else : raise elif ssl_required : raise SSLNotSupportedError ( 'Required IMAP STARTTLS not supported.' ) return conn
return a connection to the imap server .