import logging
import smtp


class StderrLogger(object):

    def __init__(self):
        self.logger = logging.getLogger('mail')

def write(self, message):
    self.logger.debug(message)
org_stderr = smtp.stderr
smtp.stderr = StderrLogger()
smtp.stderr = org_stderr
How to log smtp debug information to a file?