Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def connection(self):
"""Open a connection to the IMAP server, login and select a mailbox."""
if self._connection is None:
imap_cls = imaplib2.IMAP4_SSL if self.ssl else imaplib2.IMAP4
self._connection = imap_cls(self.host, self.port)
self._connection.login(self.username, self.password)
code, [msg] = self._connection.select(self.mailbox)
if not code == 'OK':
raise MailboxSelectionError(msg)
self._mailbox_selected = True
return self._connection
@staticmethod
def do_import_content(mbox, only_new=True):
mbox = mbox.db.merge(mbox)
session = mbox.db
session.add(mbox)
if mbox.use_ssl:
mailbox = IMAP4_SSL(host=mbox.host.encode('utf-8'), port=mbox.port)
else:
mailbox = IMAP4(host=mbox.host.encode('utf-8'), port=mbox.port)
if 'STARTTLS' in mailbox.capabilities:
# Always use starttls if server supports it
mailbox.starttls()
mailbox.login(mbox.username, mbox.password)
mailbox.select(mbox.folder)
command = "ALL"
search_status = None
email_ids = None
if only_new and mbox.last_imported_email_uid:
command = "(UID %s:*)" % mbox.last_imported_email_uid
search_status, search_result = mailbox.uid('search', None, command)
if needsync:
event.clear()
dosync()
config = open('config.txt', 'r')
sender = config.readline()
emailPass = config.readline()
phonenum = config.readline().strip()
Text.sendText("Send password.", sender, emailPass, phonenum)
M = imaplib2.IMAP4_SSL('imap.gmail.com')
M.login(sender, emailPass)
M.select("inbox")
check()
#init
thread = Thread(target=idle)
event = Event()
#start
thread.start()
time.sleep(60*60)
#stop
event.set()
def login(self):
try:
if self.source.use_ssl:
mailbox = IMAP4_SSL(host=self.source.host.encode('utf-8'), port=self.source.port)
else:
mailbox = IMAP4(host=self.source.host.encode('utf-8'), port=self.source.port)
if 'STARTTLS' in mailbox.capabilities:
#Always use starttls if server supports it
res = mailbox.starttls()
if not is_ok(res):
# TODO: Require bad login from client error
raise ReaderError(res)
if 'IDLE' in mailbox.capabilities:
self.can_push = True
res = mailbox.login(self.source.username, self.source.password)
if not is_ok(res):
# TODO: Require bad login from client error
raise ClientError(res)
res = mailbox.select(self.source.folder)
if not is_ok(res):
self.M.uid('store', uid, '+FLAGS', '\\Deleted')
self.M.expunge()
# Append the encrypted message
move_to = self.config['mailbox'] if 'move_to' not in self.config else self.config['move_to']
read = '' if self.config['monitor'] == 'UNSEEN' else '\\seen'
self.M.append(move_to, read, date, Util.flattenMessage(encrypted_mail))
if __name__ == '__main__':
config = yaml.load(open(sys.argv[1], 'r'))
while True:
# Login
M = imaplib2.IMAP4_SSL(config['server'])
try:
M.login(config['username'], config['password'])
M.select(config['mailbox'])
# Start the Idler thread
idler = Idler(M, config)
idler.start()
idler.join()
except:
pass
finally:
# Clean up.
M.logout()