1

I've got a problem with sending emails with russian text from my server. I'm sending emails like this:

from django.core.mail import EmailMultiAlternatives
message = EmailMultiAlternatives(
    'some subject',
    '',
    'from@example.com',
    ['to@example.com']
)
message.attach_alternative(unicode_text_with_html, "text/html")
message.send()

When this email comes to mail service (i.e. gmail.com) there sometimes occur couple of damaged symbols - Ð � instead of letter. Other letters (even same letters) don't get damaged. When I specify cp1251 encoding (i.e. message.encoding = 'cp1251') emails don't get damaged. But there are email services that do not accept cp1251 encoding and give error to user when he tries to read message.

When I use filebased email backend with utf-8 encoding I don't get damaged symbols in message body either. And the charset is set to utf-8.

I wonder how can utf-8 encoded message be broken like that. Is there anything I can do to get rid of that symbols?

P.S.: I use Postfix as email server to send emails

UPDATE: On my test server the situation was exactly the same. I've removed Postfix and replaced it by Qmail. Everything worked fine. I swapped it back and symbols came back again. So my latest guess is that it's Postfix problem. I'll try to tune config.

PukeCloud
  • 116
  • 8
  • Did you try using [Django's ugettext](https://docs.djangoproject.com/en/dev/topics/i18n/translation/#standard-translation) `_(unicode_text_with_html)` – yuvi Dec 13 '13 at 14:39
  • I wonder how this can help. Just tried this and it did nothing. – PukeCloud Dec 13 '13 at 15:07

2 Answers2

1

I would consider adding just this lines:

#-*- coding: utf-8 -*-
from __future__ import unicode_literals

It helped me to get rid off errors like:

'ascii' codec can't encode character u'\u0119' in position 6: ordinal not in range(128)
andilabs
  • 22,159
  • 14
  • 114
  • 151
0

You need to run it through something like php's htmlentities() to convert all non-recognized characters into html safe entities. In your case, Ð converts to this: Ð

Alternatively, there are several web based converters, a few are listed in this answer.

Community
  • 1
  • 1
John
  • 11,985
  • 3
  • 45
  • 60
  • The deal is that every symbol is recognized and is ok. When I send exactly the same email through gmail backend everything is fine. I suspect that the problem is somewhere in postfix settings – PukeCloud Dec 13 '13 at 15:52