[Python-bugs-list] [ python-Bugs-521270 ] SMTP does not handle UNICODE

noreply@sourceforge.net noreply@sourceforge.net
Sun, 24 Feb 2002 07:09:00 -0800


Bugs item #521270, was opened at 2002-02-21 17:36
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=521270&group_id=5470

Category: Python Library
Group: Python 2.1.1
>Status: Closed
>Resolution: Fixed
Priority: 5
Submitted By: Noah Spurrier (noah)
Assigned to: Nobody/Anonymous (nobody)
Summary: SMTP does not handle UNICODE

Initial Comment:
The SMTP library does not gracefully handle
<type 'unicode'> strings. This type of string
is frequently returned from a databases and
particulary when working with COM objects.

For example, we pull email TO addresses and messages
from from a database. We would like to call:
    server.sendmail(FROM, TO, message)
instead we have to do this:
    server.sendmail(FROM, str(TO), str(message))
>From a users point of view it is easy to get
around this by putting str() around every string
before calling STMP methods, but I think it would
make more sense for SMTP to convert them or
gracefully handle them.





----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2002-02-24 07:09

Message:
Logged In: YES 
user_id=21627

Thanks for the example; the problem was that it considered
the Unicode string as a list of recipients. Fixed in
smtplib.py 1.48.

----------------------------------------------------------------------

Comment By: Noah Spurrier (noah)
Date: 2002-02-23 16:37

Message:
Logged In: YES 
user_id=59261

# This is a contrived example to show what happens
# when stmplib tries to swallow a unicode string.
# Here the TO address is passed as unicode. Note that
# this unicode string encode only regular ASCII characters
# that would otherwise not be a problem for the SMPT
# protocol. Where you might see this problem in the real
# world would be when you pull email addresses from a
# database or if you were getting data from a Windows
# COM object.
#
# I think that in keeping with the transparent spirit of
# unicode strings that it should be the responsibility
# of the smtplib class to convert these strings --
# or to at least throw an exception. On the other hand,
# if it is decided that smtplib should not convert unicode
# then it should at least trap unicode strings and throw an
# exception. Currently it treats unicode strings as regular
# strings (thus lulling the programmer into a false sense
# of security), but then it fails at the protocol level.
# There is no run-time exception. The SMTP server just
# rejects the recipient.

import smtplib

# For argument sake, say that this unicode string came 
# from a database; also Windows COM objects return unicode.
ADDRESS_TO = u'null@blackhole.org'

SMTP_SERVER = 'spruce.he.net'
ADDRESS_FROM = 'noah@noah.org'
SUBJECT = 'test'
MESSAGE = "From: %s\r\nTo: %s\r\nSubject:%s\r\n\r\n" % 
(ADDRESS_FROM, ADDRESS_TO, SUBJECT)

server = smtplib.SMTP(SMTP_SERVER)
server.set_debuglevel(1)
server.sendmail(ADDRESS_FROM, ADDRESS_TO, MESSAGE)
server.quit()


----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-02-23 15:11

Message:
Logged In: YES 
user_id=21627

Can you give a specific example? Please attach a script to
this report which exposes the error you are seeing (using
Unicode literals where necessary).

Perhaps you have non-ASCII characters in your strings? Those
are not supported by the SMTP protocol, so there is no way
smtplib could handle them gracefully.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=521270&group_id=5470