login community faq

Hi,

In django I am using django-tinymce to create newsletter for my registered users. Content (which is HTMLField()) is in unicode and it is stored in my database (mysql) as unicode correctly.

I have this script to send the newsletter to all registered users (except those who have opted-out).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
smtp = SMTP()
smtp.connect('smtp.webfaction.com')
smtp.login('username','password')
cursor.execute('select subject, content from main_newslettermessage where name=%s', (name,))
row = cursor.fetchone()
msg = MIMEMultipart('alternative')
msg['Subject'] = row[0]
msg['From'] = 'no-reply@example.com'
content = row[1]
part1 = MIMEText(content, 'plain', _charset='utf-8')
part2 = MIMEText(content, 'html', _charset='utf-8')
msg.attach(part1)
msg.attach(part2)

cursor.execute('select email from auth_user where 1')
rows = cursor.fetchall()
count = 0
for mail_row in rows:
    to_email = mail_row[0]
    if to_email not in optout:
        msg['To'] = to_email
        smtp.sendmail('no-reply@example.com', to_email, msg.as_string())

The problem is that unicode characters in the email message is shown as questionmarks (ie. ???????) What am I doing wrong?

PS: I also tried this part2 = MIMEText(content.encode('utf-8'), 'html', _charset='utf-8') but I receive a unicode error while running the script.

asked Oct 04 '12 at 06:53

xpanta's gravatar image

xpanta
6014

Can you confirm that the content stored in your database is in the correct format and displays correctly? You can try printing it out to a file or to a webpage instead of sending it as an email.

(Oct 04 '12 at 10:21) todork todork's gravatar image

On my browser when viewing/editing my database using phpMyAdmin content appears correct. When I do a print content instead of sendmail on the above script I do get questionmarks on my terminal screen. Does that make any sense?

(Oct 05 '12 at 05:55) xpanta xpanta's gravatar image

will you post a sample of what you have in your database? It sounds like what you are putting into the email is not coming out correctly because it is not in the database correctly.

(Oct 05 '12 at 08:54) gmaniac gmaniac's gravatar image

It looks to me like you're not telling MySQLdb to use unicode. Try setting up your database connection like this:

1
2
db = MySQLdb.connect(host='localhost', user='dbname', passwd='dbpass', 
                     db='dbname', use_unicode=True, charset='utf8')

If that doesn't work (or perhaps as an altogether better approach) you might try accessing your data via the Django model, since Django should handle the unicode stuff for you automatically. For example:

1
2
3
4
5
6
7
# just guessing at your model and field names based on your query
from main.models import Newslettermessage
message = Newslettermessage.objects.get(name=name)
msg = MIMEMultipart('alternative')
msg['Subject'] = message.subject
msg['From'] = 'no-reply@example.com'
content = message.content

Hope that helps!

answered Oct 05 '12 at 12:48

seanf's gravatar image

seanf ♦♦
56291220

edited Oct 05 '12 at 14:34

Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×229
×3

Asked: Oct 04 '12 at 06:53

Seen: 315 times

Last updated: Oct 05 '12 at 14:34

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited