2

I am sending an email, and to send it I need to get three variables -- title, provider, and vendor_id. This is how I am getting it:

# # -*- coding: utf-8 -*-
...
## subject
title = title.encode('utf-8')
subject = "%s - %s"%(provider, title)
msg['Subject'] = subject

## content
body_text = "The following has been successfully delivered:\n\nVendor ID: %s\nProvider: %s\nTitle: %s\n\nThank you."%((vendor_id, provider, title))
content = MIMEText(body_text, 'plain')

However, when I send the email the subject is encoded correctly (which includes the title), but the body_text is not (which also includes the title). This is how it looks:

Subject: testmovieprovider - Une soirée d'enfer

Body: The following has been successfully delivered:

Vendor ID: 02352_FR
Provider: testmovieprovider
Title: Une soirée d'enfer   <------ why is this happening?

Thank you.

Why is the title in the body not encoding correctly, and what would I need to change in order for it to look like the first?

Update: Thank you for the feedback. This is what worked:

MIMEText(body_text, 'plain') --> content = MIMEText(body_text, 'plain', 'UTF-8')

6
  • Try to add #encoding utf-8 in top of file Commented Mar 18, 2012 at 21:12
  • How are you sending the email? Does the email have proper Content-Type header with charset="utf-8" in it? Commented Mar 18, 2012 at 21:22
  • This appears to be a text encoding problem. The trouble here is you're attempting to implicitly coerce unicode characters into ASCII when you perform your mime conversion. Have you tried some other value for MIMEText? Commented Mar 18, 2012 at 21:27
  • The only other thing I've tried is content = MIMEText(body_text, 'utf-8'), but that gives me no text at all in the email. Commented Mar 18, 2012 at 21:29
  • 2
    try content = MIMEText(body_text, 'plain', 'utf-8'). Commented Mar 18, 2012 at 21:31

1 Answer 1

4

Set the encoding for the message:

content = MIMEText(body_text, 'plain', 'utf-8')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.