Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
logging.handlers.SMTPHandler Class Reference

List of all members.

Public Member Functions

def __init__
def getSubject
def emit

Public Attributes

 mailport
 password
 username
 fromaddr
 toaddrs
 subject
 secure

Detailed Description

A handler class which sends an SMTP email for each logging event.

Definition at line 809 of file handlers.py.


Constructor & Destructor Documentation

def logging.handlers.SMTPHandler.__init__ (   self,
  mailhost,
  fromaddr,
  toaddrs,
  subject,
  credentials = None,
  secure = 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. To specify
authentication credentials, supply a (username, password) tuple
for the credentials argument. To specify the use of a secure
protocol (TLS), pass in a tuple for the secure argument. This will
only be used when authentication credentials are supplied. The tuple
will be either an empty tuple, or a single-value tuple with the name
of a keyfile, or a 2-value tuple with the names of the keyfile and
certificate file. (This tuple is passed to the `starttls` method).

Definition at line 814 of file handlers.py.

00814 
00815                  credentials=None, secure=None):
00816         """
00817         Initialize the handler.
00818 
00819         Initialize the instance with the from and to addresses and subject
00820         line of the email. To specify a non-standard SMTP port, use the
00821         (host, port) tuple format for the mailhost argument. To specify
00822         authentication credentials, supply a (username, password) tuple
00823         for the credentials argument. To specify the use of a secure
00824         protocol (TLS), pass in a tuple for the secure argument. This will
00825         only be used when authentication credentials are supplied. The tuple
00826         will be either an empty tuple, or a single-value tuple with the name
00827         of a keyfile, or a 2-value tuple with the names of the keyfile and
00828         certificate file. (This tuple is passed to the `starttls` method).
00829         """
00830         logging.Handler.__init__(self)
00831         if isinstance(mailhost, tuple):
00832             self.mailhost, self.mailport = mailhost
00833         else:
00834             self.mailhost, self.mailport = mailhost, None
00835         if isinstance(credentials, tuple):
00836             self.username, self.password = credentials
00837         else:
00838             self.username = None
00839         self.fromaddr = fromaddr
00840         if isinstance(toaddrs, str):
00841             toaddrs = [toaddrs]
00842         self.toaddrs = toaddrs
00843         self.subject = subject
00844         self.secure = secure

Here is the caller graph for this function:


Member Function Documentation

def logging.handlers.SMTPHandler.emit (   self,
  record 
)
Emit a record.

Format the record and send it to the specified addressees.

Definition at line 854 of file handlers.py.

00854 
00855     def emit(self, record):
00856         """
00857         Emit a record.
00858 
00859         Format the record and send it to the specified addressees.
00860         """
00861         try:
00862             import smtplib
00863             from email.utils import formatdate
00864             port = self.mailport
00865             if not port:
00866                 port = smtplib.SMTP_PORT
00867             smtp = smtplib.SMTP(self.mailhost, port)
00868             msg = self.format(record)
00869             msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
00870                             self.fromaddr,
00871                             ",".join(self.toaddrs),
00872                             self.getSubject(record),
00873                             formatdate(), msg)
00874             if self.username:
00875                 if self.secure is not None:
00876                     smtp.ehlo()
00877                     smtp.starttls(*self.secure)
00878                     smtp.ehlo()
00879                 smtp.login(self.username, self.password)
00880             smtp.sendmail(self.fromaddr, self.toaddrs, msg)
00881             smtp.quit()
00882         except (KeyboardInterrupt, SystemExit):
00883             raise
00884         except:
00885             self.handleError(record)

Here is the call graph for this function:

def logging.handlers.SMTPHandler.getSubject (   self,
  record 
)
Determine the subject for the email.

If you want to specify a subject line which is record-dependent,
override this method.

Definition at line 845 of file handlers.py.

00845 
00846     def getSubject(self, record):
00847         """
00848         Determine the subject for the email.
00849 
00850         If you want to specify a subject line which is record-dependent,
00851         override this method.
00852         """
00853         return self.subject

Here is the caller graph for this function:


Member Data Documentation

Definition at line 838 of file handlers.py.

Definition at line 831 of file handlers.py.

Definition at line 835 of file handlers.py.

Definition at line 843 of file handlers.py.

Definition at line 842 of file handlers.py.

Definition at line 841 of file handlers.py.

Definition at line 837 of file handlers.py.


The documentation for this class was generated from the following file: