def test_imap_connection(settings): host = settings['settings']['imap_server_host'] port = settings['settings']['imap_server_port'] conn = IMAPClient(host, port=port, use_uid=True, ssl=False, timeout=120) if conn.has_capability('STARTTLS'): conn = create_imap_connection(host, port, ssl_required=True) conn.login(settings['settings']['imap_username'], settings['settings']['imap_password']) else: with pytest.raises(SSLNotSupportedError): create_imap_connection(host, port, ssl_required=True) conn = create_imap_connection(host, port, ssl_required=False) conn.login(settings['settings']['imap_username'], settings['settings']['imap_password'])
def try_fill_config_data(email_address, password): response = {} subdomains = ['mail.', 'imap.', 'smtp.', ''] domain = email_address.split('@')[1].lower() smtp_port = 587 imap_port = 993 log = get_logger() for imap_host in [subdomain + domain for subdomain in subdomains]: try: create_imap_connection(imap_host, imap_port, True, use_timeout=10) response['imap_server_host'] = imap_host response['imap_server_port'] = imap_port break except SSLNotSupportedError: create_imap_connection(imap_host, imap_port, False, use_timeout=10) response['imap_server_host'] = imap_host response['imap_server_port'] = imap_port response['ssl_required'] = False break except (IMAPClient.Error, socket.error): continue if 'imap_server_host' not in response: raise ConnectionError( 'IMAP connection failed. Check your password. ' 'If error persists try provide connection details') for smtp_host in [subdomain + domain for subdomain in subdomains]: try: with SMTPConnection(0, email_address, email_address, 'password', password, (smtp_host, smtp_port), True, log): response['smtp_server_host'] = smtp_host response['smtp_server_port'] = smtp_port break except: continue if 'smtp_server_host' not in response: raise ConnectionError( 'SMTP connection failed. Check your password. ' 'If error persists try provide connection details') return response
def _get_IMAP_connection(self, account): host, port = account.imap_endpoint try: conn = create_imap_connection(host, port, ssl_required=True) except (IMAPClient.Error, socket.error) as exc: log.error('Error instantiating IMAP connection', account_id=account.id, email=account.email_address, imap_host=host, imap_port=port, error=exc) raise return conn
def slurp_namespace(self, namespace, account, db): info = account.provider_info host, port = account.imap_endpoint imap = create_imap_connection(host, port) imap.debug = self.args.debug_imap if info['auth'] == 'oauth2': imap.oauth2_login(account.email_address, account.access_token) elif info['auth'] == 'password': imap.login(account.email_address, account.password) else: raise NotImplementedError( "auth mechanism {0!r} not implemented; provider: {1!r}".format( info['auth'], account.provider)) slurp_imap_namespace_gmail( imap, namespace=namespace, account=account, db=db)
def slurp_namespace(self, namespace, account, db): info = account.provider_info host, port = account.imap_endpoint imap = create_imap_connection(host, port) imap.debug = self.args.debug_imap if info['auth'] == 'oauth2': imap.oauth2_login(account.email_address, account.access_token) elif info['auth'] == 'password': imap.login(account.email_address, account.password) else: raise NotImplementedError( "auth mechanism {0!r} not implemented; provider: {1!r}".format( info['auth'], account.provider)) slurp_imap_namespace_gmail(imap, namespace=namespace, account=account, db=db)