diff options
author | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2007-05-01 10:20:03 (GMT) |
---|---|---|
committer | Vinay Sajip <vinay_sajip@yahoo.co.uk> | 2007-05-01 10:20:03 (GMT) |
commit | 70c8e8b8613c2af0025d5c950e0be8d1b6296294 (patch) | |
tree | 3ad753f1c8c257e138c2b7d81857659532a37102 /Lib/logging | |
parent | 09728b7ef3c2e12677725703e67fa9fddc84463e (diff) | |
download | cpython-70c8e8b8613c2af0025d5c950e0be8d1b6296294.zip cpython-70c8e8b8613c2af0025d5c950e0be8d1b6296294.tar.gz cpython-70c8e8b8613c2af0025d5c950e0be8d1b6296294.tar.bz2 |
Added new optional credentials argument to SMTPHandler.__init__, and smtp.login() is now called in SMTPHandler.emit() if credentials are specified.
Diffstat (limited to 'Lib/logging')
-rw-r--r-- | Lib/logging/handlers.py | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/Lib/logging/handlers.py b/Lib/logging/handlers.py index c713a11..3c54c8f 100644 --- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -722,22 +722,25 @@ class SMTPHandler(logging.Handler): """ A handler class which sends an SMTP email for each logging event. """ - def __init__(self, mailhost, fromaddr, toaddrs, subject): + def __init__(self, mailhost, fromaddr, toaddrs, subject, credentials=None): """ Initialize the handler. Initialize the instance with the from and to addresses and subject line of the email. To specify a non-standard SMTP port, use the - (host, port) tuple format for the mailhost argument. + (host, port) tuple format for the mailhost argument. To specify + authentication credentials, supply a (username, password) tuple + for the credentials argument. """ logging.Handler.__init__(self) if type(mailhost) == types.TupleType: - host, port = mailhost - self.mailhost = host - self.mailport = port + self.mailhost, self.mailport = mailhost else: - self.mailhost = mailhost - self.mailport = None + self.mailhost, self.mailport = mailhost, None + if type(credentials) == types.TupleType: + self.username, self.password = credentials + else: + self.username = None self.fromaddr = fromaddr if type(toaddrs) == types.StringType: toaddrs = [toaddrs] @@ -793,6 +796,8 @@ class SMTPHandler(logging.Handler): string.join(self.toaddrs, ","), self.getSubject(record), formatdate(), msg) + if self.username: + smtp.login(self.username, self.password) smtp.sendmail(self.fromaddr, self.toaddrs, msg) smtp.quit() except (KeyboardInterrupt, SystemExit): |