logging SMTPhandler Error

Ned Deily nad at acm.org
Sun Aug 23 02:07:29 CEST 2009


In article 
<c82d8338-c196-400a-9c09-c8f6dbc25c8f at l35g2000vba.googlegroups.com>,
 Bev in TX <countryone77 at yahoo.com> wrote:
> On Aug 21, 8:34 am, Bev in TX <countryon... at yahoo.com> wrote:
> > Hi,
> >
> > I've done some Python programming, but I still consider myself a
> > Python newbie.  I have a Mac Pro OS X 10.5.8 system and I installed
> > Python 2.6.2 (the latest package available for the Mac) yesterday.
> >
> > I was working through Matt Wilson's article on using the logging
> > module:http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module
> > ...
> > (If that does not work, then try:http://tinyurl.com/5v2lcy)
> >
> > Everything worked great until his last example.  My ISP does not
> > provide e-mail, so I tried using gmail in the line that sets h2.  I
> > substituted "mailid" for my actual e-mail address in the following
> > examples; in my test I used my actual e-mail ID.  Also, I used the
> > full path to the newly installed Python 2.6.2; otherwise it picked up
> > the older Python 2.5:
> > #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python
> >
> > First attempt:
> > h2 = logging.handlers.SMTPHandler('smtp.gmail.com', 'mai... at gmail.com',
> > ['mai... at gmail.com'],'ERROR log')
> > However, that caused the following error to be issued:
> >
> > Traceback (most recent call last):
> >   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> > python2.6/logging/handlers.py", line 868, in emit
> >     smtp.sendmail(self.fromaddr, self.toaddrs, msg)
> >   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> > python2.6/smtplib.py", line 698, in sendmail
> >     raise SMTPSenderRefused(code, resp, from_addr)
> > SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first.
> > 7sm3867994qwf.47', 'mai... at gmail.com')

The problem here is that gmail, like most modern mail services, requires 
the use of an encrypted (SSL or TLS) connection for mail relaying so 
that the required user name and password are not sent in the clear.  The 
logging SMTP handler uses the smtplib module from the standard module to 
send mail and, when built properly, smtplib does support TLS.  However, 
the caller of smtplib does need to do some extra work in that case, i.e. 
it needs to call the server object's starttls method at the right point 
in the protocol handshaking. It's currently not done automatically in 
smtplib and, unfortunately, there is no code in the logging smtp handler 
to detect the need for and to call starttls (in response to a 
250-STARTTLS response to an EHLO).

To make this work, either the logging module or, perhaps better, the 
smptlib module needs to be smarter about this case.  I didn't see an 
open issue on the Python bug tracker about this; you might want to open 
one.  In the meantime, some options would be to find an SMTP mail host 
that doesn't require TLS.  Or write a custom logger.  Or on OS X it's 
not *too* difficult to set up a local host mailer using the 
Apple-supplied prefix that would accept mail locally and forward it to a 
more sophisticated remote mailer.

-- 
 Ned Deily,
 nad at acm.org




More information about the Python-list mailing list