Created
February 2, 2011 16:37
-
-
Save sente/807944 to your computer and use it in GitHub Desktop.
imaplib_connection class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#taken from Doug Hellman's PyMOTW | |
import imaplib | |
import ConfigParser | |
import os | |
def open_connection(verbose=False): | |
# Read the config file | |
config = ConfigParser.ConfigParser() | |
config.read([os.path.expanduser('~/.pymotw')]) | |
# Connect to the server | |
hostname = config.get('server', 'hostname') | |
if verbose: | |
print 'Connecting to', hostname | |
connection = imaplib.IMAP4_SSL(hostname) | |
# Login to our account | |
username = config.get('account', 'username') | |
password = config.get('account', 'password') | |
if verbose: | |
print 'Logging in as', username | |
connection.login(username, password) | |
return connection | |
if __name__ == '__main__': | |
c = open_connection(verbose=True) | |
try: | |
print c | |
finally: | |
c.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment