logging module, SMTPHandler and gmail in python 2.6

Discussion in 'Python' started by mynthon, Dec 4, 2009.

  1. mynthon

    mynthon Guest

    You cannot use gmail account for sending emails with logging module.
    It is because google requires TLS connection and logging module
    doesn't support it. To use gmail you have to extend
    logging.handlers.SMTPHandler class and override SMTPHandler.emit()
    method. Here is source code.

    (There really should be option to add user comments on pythons
    documentation page!)

    #--code--#

    import logging
    import logging.handlers

    class TlsSMTPHandler(logging.handlers.SMTPHandler):
    def emit(self, record):
    """
    Emit a record.

    Format the record and send it to the specified addressees.
    """
    try:
    import smtplib
    import string # for tls add this line
    try:
    from email.utils import formatdate
    except ImportError:
    formatdate = self.date_time
    port = self.mailport
    if not port:
    port = smtplib.SMTP_PORT
    smtp = smtplib.SMTP(self.mailhost, port)
    msg = self.format(record)
    msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r
    \n%s" % (
    self.fromaddr,
    string.join(self.toaddrs, ","),
    self.getSubject(record),
    formatdate(), msg)
    if self.username:
    smtp.ehlo() # for tls add this line
    smtp.starttls() # for tls add this line
    smtp.ehlo() # for tls add this line
    smtp.login(self.username, self.password)
    smtp.sendmail(self.fromaddr, self.toaddrs, msg)
    smtp.quit()
    except (KeyboardInterrupt, SystemExit):
    raise
    except:
    self.handleError(record)

    logger = logging.getLogger()

    gm = TlsSMTPHandler(("smtp.gmail.com", 587), 'bugs@my_company.com',
    ['admin@my_company.com'], 'Error found!',
    ('', 'top_secret_gmail_password'))
    gm.setLevel(logging.ERROR)

    logger.addHandler(gm)

    try:
    1/0
    except:
    logger.exception('FFFFFFFFFFFFFFFFFFFFFFFUUUUUUUUUUUUUUUUUUUUUU-')

    #--/code--#


    see also fortmatted version at
    http://mynthon.net/howto/-/python/python - logging.SMTPHandler-how-to-use-gmail-smtp-server.txt.
    mynthon, Dec 4, 2009
    #1
    1. Advertising

  2. mynthon

    Vinay Sajip Guest

    On Dec 4, 12:31 pm, mynthon <> wrote:

    Thank you for this suggestion. Ideally, you would have created an
    issue for this on bugs.python.org, because then it would be more
    likely to be acted upon.

    I've implemented this feature in r76691 (in Python trunk and py3k) in
    a more general way. It works by specifying an optional secure argument
    to SMTPHandler.__init__. This defaults to None, which implements the
    current behaviour - no TLS support.

    If TLS support is required, pass in a tuple for secure, with one of
    the following values:

    1. An empty tuple
    2. A 1-tuple with the name of the PEM-formatted keyfile with a private
    key.
    3. A 2-tuple with the name of the PEM-formatted keyfile and a PEM-
    formatted certificate chain file.

    This tuple, if specified, is passed to the SMTP object's starttls
    method (and via that to socket.ssl). Check the starttls/ssl methods
    for more information.

    Regards,

    Vinay Sajip
    Vinay Sajip, Dec 6, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pete Jereb
    Replies:
    0
    Views:
    530
    Pete Jereb
    Oct 7, 2003
  2. Replies:
    3
    Views:
    493
    Vinay Sajip
    May 1, 2007
  3. akonsu
    Replies:
    4
    Views:
    941
    Chris Withers
    Sep 30, 2009
  4. norbert

    SMTPHandler and Unicode

    norbert, Jul 5, 2010, in forum: Python
    Replies:
    13
    Views:
    963
    Chris Withers
    Jan 14, 2012
  5. pstatham
    Replies:
    2
    Views:
    602
    Chris Withers
    Oct 11, 2010
Loading...

Share This Page