By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

logging SMTPHandler and Authentication

P: n/a
I'm new to Python, but I've been thrown into coding a pretty
complicated regression testing script. I need to email the log of the
daily test to the code owners. I thought I could use SMTPHandler for
this, but our email system requires authentication. I have not been
able to figure out how to log in using SMTPHandler. I found the
following post on comp.lang.python:

http://groups.google.com/group/comp....b67687e507c974

However, I haven't been able to translate this to anything I can use.
I've also looked at the source code for the SMTPHandler, but I don't
want to edit that.

Has anyone else dealt with this?

Thanks much,
James

May 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
En Mon, 30 Apr 2007 21:08:34 -0300, <ja**********@gmail.comescribió:
I'm new to Python, but I've been thrown into coding a pretty
complicated regression testing script. I need to email the log of the
daily test to the code owners. I thought I could use SMTPHandler for
this, but our email system requires authentication. I have not been
able to figure out how to log in using SMTPHandler. I found the
following post on comp.lang.python:

http://groups.google.com/group/comp....b67687e507c974

However, I haven't been able to translate this to anything I can use.
I've also looked at the source code for the SMTPHandler, but I don't
want to edit that.
Unfortunately -and despite what's said on that post- SMTPHandler does
*not* provide any hook to customize the way it sends the message. The SMTP
instance used internally is not available so you can't call its login()
method from the outside. If you don't want to edit the sources, you could
inherit from SMTPHandler, copy the emit() implementation and add the
missing login() call:

--- begin code ---
import logging, logging.handlers, string

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

Format the record and send it to the specified addressees.
"""
try:
import smtplib
try:
from email.Utils import formatdate
except:
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)
# ADD THE FOLLOWING LINE:
smtp.login(username, password)
smtp.sendmail(self.fromaddr, self.toaddrs, msg)
smtp.quit()
except (KeyboardInterrupt, SystemExit):
raise
except:
self.handleError(record)

rootLogger = logging.getLogger('')
smtpHandler = SMTPHandlerWithAuth(('localhost',587),
'f***********@xxx.com', 't*********@xxx.com', "Error subject")
rootLogger.addHandler(smtpHandler)
logging.error('This is an error message that should be sent by mail.')
--- end code ---

--
Gabriel Genellina

May 1 '07 #2

P: n/a
http://groups.google.com/group/comp....frm/thread/ef8...

That fairly old thread suggests that someone provide a patch, which
hasn't happened. Sorry I missed that thread - your requirement is a
reasonable one to be dealt with by the core. So I will look to
implement this functionality soon. As it's not a bugfix I can't
backport it, but you will be able to patch your version when I've made
the change.

Regards,

Vinay Sajip

May 1 '07 #3

P: n/a
On May 1, 1:08 am, james.p.n...@gmail.com wrote:
I'm new to Python, but I've been thrown into coding a pretty
complicated regression testing script. I need to email the log of the
daily test to the code owners. I thought I could use SMTPHandler for
this, but our email system requires authentication. I have not been
able to figure out how to log in using SMTPHandler. I found the
following post on comp.lang.python:

http://groups.google.com/group/comp....frm/thread/ef8...

However, I haven't been able to translate this to anything I can use.
I've also looked at the source code for the SMTPHandler, but I don't
want to edit that.

Has anyone else dealt with this?

Thanks much,
James
I've enhanced SMTPHandler in Python SVN (trunk) to accept a (username,
password) tuple in a credentials argument to SMTPHandler.__init__().
The credentials, if specified, are used to do a login() call before
the sendmail() call.

Regards,

Vinay

May 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.