AttributeError: 'tuple' object has no attribute 'lstrip'
John Machin
sjmachin at lexicon.net
Mon Jan 5 03:12:54 EST 2009
On Jan 5, 6:15 pm, Gilles Ganault <nos... at nospam.com> wrote:
> Hello
>
> I successfully use the email package to send e-mail from Python
> scripts, but this script fails when I fetch addresses from an SQLite
> database where data is Unicode-encoded:
>
> ======
> from email.MIMEText import MIMEText
> import smtplib,sys
> import apsw
>
> connection=apsw.Connection("test.sqlite")
> cursor=connection.cursor()
>
> subject = "My subject"
> f = open("message.txt", "r")
> message = f.read()
> f.close()
>
> msg = MIMEText(message)
>
> msg['Subject'] = subject
> From = "m... at acme.com"
> msg['From'] = From
>
> server = smtplib.SMTP("smtp.acme.com")
>
> sql="SELECT email FROM people WHERE email IS NOT NULL"
> rows=list(cursor.execute(sql))
> for email in rows:
> To = email
> msg['To'] = email
>
> #print To
> #(u'du... at acme.com',)
That looks like a tuple to me. What does it look like to you?
>
> #AttributeError: 'tuple' object has no attribute 'lstrip'
> #server.sendmail(From,[To],msg.as_string())
When are asking for help, could you *PLEASE* supply the actual code
that you ran, with the actual output, and (just in case the problem is
not otherwise screamingly obvious) the *FULL* traceback?
The to_addrs arg of SMTP.sendmail() should be a single string, or a
list of strings. You have supplied [(u'du... at acme.com',)] which is a
list containing one tuple. That tuple is a result of your SQL query,
which has returned a 1-column row as a tuple (as expected/documented).
SMTP.sendmail() treats your tuple as though it were a string, and
consequently an exception is raised.
> server.quit
Should that perhaps be server.quit() ?
> Does email choke on Unicode?
I've got no idea; I've never used email or smtplib. Why don't you feed
them some Unicode and find out? Also consider having a look at the
manual.
HTH,
John
More information about the Python-list
mailing list