logging module, SMTPHandler and gmail in python 2.6

M

mynthon

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!',
('(e-mail address removed)', '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.
 
V

Vinay Sajip

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top