Advertisement
rfmonk

imap_connect.py

Mar 3rd, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.75 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import imaplib
  5. import ConfigParser
  6. import os
  7.  
  8.  
  9. def open_connection(verbose=False):
  10.     # Read the config file
  11.     config = ConfigParser.ConfigParser()
  12.     config.read([os.path.expanduser('~/.pymotw')])
  13.  
  14.     # Connect to the server
  15.     hostname = config.get('server', 'hostname')
  16.     if verbose:
  17.         print 'Connecting to', hostname
  18.     connection = imaplib.IMAP4_SSL(hostname)
  19.  
  20.     # Login to our account
  21.     username = config.get('account', 'username')
  22.     password = config.get('account', 'password')
  23.     if verbose:
  24.         print 'Logging in as', username
  25.     connection.login(username, password)
  26.     return connection
  27.  
  28. if __name__ == '__main__':
  29.     c = open_connection(verbose=True)
  30.     try:
  31.         print c
  32.     finally:
  33.         c.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement