Created
October 15, 2010 14:10
-
-
Save kiowa/628240 to your computer and use it in GitHub Desktop.
Python IMAP with TLS/SSL support
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
""" Python IMAP with TLS/SSL support """ | |
## | |
## Author: Alexander Brill <[email protected]> | |
## 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 imaptls | |
imap = imaptls.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, socket | |
from smtplib import SSLFakeSocket, SSLFakeFile | |
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 = SSLFakeSocket(self.sock, sslobj) | |
self.file = SSLFakeFile(sslobj) | |
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