简体   繁体   中英

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,
                'from@gmail.com',
                ["to_email@test.com"],
                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?

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM