电子邮件中的阿拉伯字符被解码(Django)

Arabic characters in emails are getting decoded (Django)

我用这个 code 帮助我从 linux 服务器发送电子邮件,一切正常,除了如果有阿拉伯字符,所有电子邮件将是这样的:2YrYs9mK2KfYq9i62KfZhNix2LPZig ==

注意我用的是python3.

P.S.: 在代码中我在第46行添加了.encode('utf-8')因为我以前得到这个错误'str'不支持缓冲区接口


更新:

这是我使用电子邮件功能发送的数据

theemail = EmailMessage(subject, message , sender_email, recipients, headers = {
                        'Reply-To': sender_email, 'Content-Type':'text/html; charset=utf-8',
                        'From':'sales@e3lani.me', 'Return-Path':sender+' <'+sender_email+'>',
                        'Organization':sender, 'MIME-Version':'1.0', 'Content-Transfer-Encoding':'8bit',
                        'X-Priority':'3', 'X-Originating-IP':request.META.get('REMOTE_ADDR'),
                        'X-Mailer':'Python/3.4', 'Message-ID':make_msgid(), 'Date':datetime.datetime.now().time()})

theemail.content_subtype = "html" # though I'm still seeing the <br/> tag as a string instead of giving me a new line LOL
theemail.send()

我解决了问题,我的问题是我在用于发送电子邮件的片段代码中编辑的行

你看原来的代码里有ps.stdin.write(email_message.message().as_string()),我加了.encode('utf-8')把输出转成字节! 解决问题我添加了.as_bytes()而不是.as_string()

我查了一下原因,发现在Python3中bytes可以解码为str,而str可以编码为bytes ],反之则不然!