Skip to content

Instantly share code, notes, and snippets.

@lsowen
Forked from kiowa/tlsimap.py
Last active June 14, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lsowen/4419922 to your computer and use it in GitHub Desktop.
Save lsowen/4419922 to your computer and use it in GitHub Desktop.
Update to work with Python 2.7.3
""" Python IMAP with TLS/SSL support """
##
## Author: Alexander Brill <alex@brill.no>
## Copyright (C) 2004 Alexander Brill
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
##
"""USAGE:
import tlsimap
imap = tlsimap.IMAP4('hostname')
imap.starttls(keyfile=None, certfile=None)
# create a simple function for our PLAIN login
def sendAuth(response):
return "user\0auth\0password"
typ, data = imap.authenticate("PLAIN", sendAuth)
"""
import imaplib, ssl
Commands = {
'STARTTLS': ('NONAUTH')
}
imaplib.Commands.update(Commands)
class IMAP4(imaplib.IMAP4):
def starttls(self, keyfile = None, certfile = None):
name = "STARTTLS"
typ, dat = self._simple_command(name)
if typ != 'OK':
raise self.error(dat[-1])
#sslobj = socket.ssl(self.sock, keyfile, certfile)
self.sock = ssl.wrap_socket(self.sock)
self.file = self.sock.makefile()
cap = 'CAPABILITY'
self._simple_command(cap)
if not cap in self.untagged_responses:
raise self.error('no CAPABILITY response from server')
self.capabilities = tuple(self.untagged_responses[cap][-1].upper().split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment