Not bad?

Thanks!

  • Blog
  • About

Bits about Django performance, scalability and deployment

How to send a proper unicode encoded email using Python 2.7

Posted by tkopczuk on June 28, 2011

While working on a Django Celery task to send thousands of emails in various languages I had a problem with sending accented letters. Web crossed the Unicode line a while ago – email has yet to follow.

I have recently seen many posts on sending unicode email from Python (i.e. on Twisted Matrix Blog), but the methods did not produce proper results.

This method works, even with SendGrid.

(unknown sender) Gmail problem

Cited example lacks proper encoding of the From: header. It results in Gmail describing the sender as (unknown sender) if you attempt to give sender a name. The trick lies in Unicode quoted printable encoding the name and not the email address. Especially not “< >” characters that mark the address.

Complete code to send an email:

This quick tip marks the beginning of our useful Django performance tips series.
Through this weblog members of our Django team will share their experience to help you get your Django pony prancing!

We – Blade Polska – specialize in delivering highly scalable web applications.
We choose Django, as with a bit of work it can be made blazingly fast, scalable and first of all – managable. And full of magic.

Follow us on Twitter!

Comments

  • Maggi Trymbill

    Great article, was looking for a reason for the (unknown sender) in Gmail.  One question; Why do you create a generator and flatten message object to file?  What does that actually do, and what do you gain from it?

    Thanks! :)

    • https://askthepony.com/blog Tomek Kopczuk

      Actually you gain nothing, the effect is the same as using msg.as_string().

      Generator has some parameters you can set, more or less useful (like mangle_from), but overall you do not lose much not using them (9 in 10 chance you lose nothing). The default one is doing all right. 

      The advantage is though, that you can create your own generator and have more control over the parsing and processing the message object tree. We never had to, so far :-)

      Cheers!

  • http://praveens.myopenid.com/ Praveens

    I want to have a similar theme for my blog. Can you help me on this?

    • http://praveens.myopenid.com/ Praveens

      Guys you can atleast post a reply here if its custom and guide me with some tutorials. I am just starting into web dev and want to learn by experimentation.

      • https://askthepony.com/blog Tomek Kopczuk

        We’re working people, have some patience mate. As for the theme, it’s custom as far as I remember. 

        • http://praveens.myopenid.com/ Praveens

          Ok sorry for rushing you guys. 

  • Jakob Olsen

    http://docs.python.org/library/email-examples.html#id5
    # Attach parts into message container.
    # According to RFC 2046, the last part of a multipart message, in this case
    # the HTML message, is best and preferred.

  • Pingback: Email unicode | Mendocinophotos()

  • Pingback: Python and Twisted: making utf-8 html mail with message-id | TechwikiHow()

  • Victor Varvariuc

    Thanks! msg['From'] = “”%s” ” % (Header(from_address[0], ‘utf-8′), from_address[1]) did the trick!

About this blog

Ask The Pony is a weblog, where various members of our team share knowledge about Django.

Make lightning fast apps that scale.




Are you a designer?

SnapRuler
our delicious screen ruler for Mac

”

If you’re a full time designer or developer, get ready to have your mind blown.
Harrison Weber, The Next Web

Recent Posts

  • Fabric script to deploy minified, combined and otherwise optimized media.
  • Setup a complete Django server, deploy, rollback – all in one powerful script.
  • Getting Django on Heroku prancing 8 times faster.
  • Django and PostgreSQL – improving the performance with no effort and no code.
  • How to send a proper unicode encoded email using Python 2.7

Copyright © 2011 Blade Polska

  • Blog
  • About