[email/quoprimime.py] AttributeError: 'tuple' object has no attribute 'lstrip'

G

Gilles Ganault

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 = "(e-mail address removed)"
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'(e-mail address removed)',)

#AttributeError: 'tuple' object has no attribute 'lstrip'
#server.sendmail(From,[To],msg.as_string())

server.quit

connection.close(True)
======

Does someone know what is wrong with the above? Does email choke on
Unicode?

Thank you.
 
C

Chris Rebert

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 = "(e-mail address removed)"
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'(e-mail address removed)',)

#AttributeError: 'tuple' object has no attribute 'lstrip'
#server.sendmail(From,[To],msg.as_string())

server.quit

connection.close(True)
======

Does someone know what is wrong with the above? Does email choke on
Unicode?

Please include the full Traceback so that you can be helped more easily.

Cheers,
Chris
 
J

John Machin

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 = "(e-mail address removed)"
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'(e-mail address removed)',)

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'(e-mail address removed)',)] 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
 
T

Terry Reedy

Gilles said:
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 = "(e-mail address removed)"
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

Why is 'email' renamed 'To'?
msg['To'] = email

#print To
#(u'(e-mail address removed)',)

Why are these line comments?
Why is the string enclosed in a tuple?
#AttributeError: 'tuple' object has no attribute 'lstrip'

True. Only strings have lstrip method.
#server.sendmail(From,[To],msg.as_string())

Ditto. This looks looks a line from a doc.
If you want help interpreting an error message,
copy and paste the *entire traceback* without editing.
server.quit

connection.close(True)
======

Does someone know what is wrong with the above?

Why do you think anything is wrong? Post the actual error message
separate from the code that generates the error message.
> Does email choke on Unicode?

tjr
 
G

Gilles Ganault

Thanks guys. Turns out email is a tuple, so here's how to extract the
columns:

for email in rows:
email=email[0]
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top