user2950593
user2950593

Reputation: 9647

Django python send email with non-english characters

I try to send email with russian chracters, but as an output I get

Subject: (b'\xd0\xaf\xd0\x9a\xd0\xbb\xd1\x8e\xd1\x87\xd0\xbd\xd0\xb8\xd0\xba - \xd0\xbf\xd0\xbe\xd1\x81\xd1\x82\xd1\x83\xd0\xbf\xd0\xb8\xd0\xbb\xd0\xb0 \xd0\xbe\xd0\xbf\xd0\xbb\xd0\xb0\xd1\x82\xd0\xb0', 27)

This is my code:

subject = 'поступила оплата'
body = 'email body'
    send_mail(
                subject
                body,
                '[email protected]',
                ["[email protected]"],
                fail_silently=False,
            )

I tried

subject.encode('utf8') 
subject.decode('utf8')
subject.encode('utf8').decode('utf8')
codecs.utf_8_encode(subject)

But didn't help. What do I do?

Upvotes: 1

Views: 803

Answers (1)

oriash
oriash

Reputation: 159

Try:

subject = u'поступила оплата'

The u in front of the string means the string has been represented as unicode. Letters before strings in Python are called "String Encoding declarations". Unicode is a way to represent more characters than normal ASCII can manage.

You can also convert to unicode like this:

subject = unicode('поступила оплата')

By the way, you may also need to declare the encoding in the beginning of the script, like this:

#encoding:utf8


Source: What does the 'u' symbol mean in front of string values?

Upvotes: 2

Related Questions