-
Notifications
You must be signed in to change notification settings - Fork 2
/
imaplib_connect.py
47 lines (37 loc) · 1.24 KB
/
imaplib_connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import imaplib
import configparser
import os
def readconf():
config = configparser.ConfigParser()
path = os.path.dirname(os.path.realpath(__file__))
file = os.path.join(path, '.pymotw')
config.read([os.path.expanduser(file)])
#config["env"]={'path':path}
options = {'path': path, 'account': []}
for section in config.sections():
account = {}
account['hostname'] = config.get(section, 'hostname')
account['port'] = config.get(section, 'port')
account['username'] = config.get(section, 'username')
account['password'] = config.get(section, 'password')
options['account'].append(account)
return options
def open_connection(verbose=False,hostname='',port='',username='',password=''):
# Read the config file
if verbose:
print('Connecting to', hostname)
try:
connection = imaplib.IMAP4_SSL(hostname, port)
except Exception as err:
print(err, "connecterror")
return False
if verbose:
print('Logging in as', username)
try:
connection.login(username, password)
except Exception as err:
print(err, "autherror")
return False
return connection