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

时间:2016-02-04 12:02:26

标签: python django email python-3.x django-forms

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

请注意,我使用的是python 3。

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()

1 个答案:

答案 0 :(得分:0)

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

您在原始代码中看到ps.stdin.write(email_message.message().as_string()),我添加.encode('utf-8')将输出转换为字节! 解决我添加.as_bytes()而不是.as_string()

的问题

我搜索了原因并发现在Python3中bytes可以解码为str,而str可以编码为bytes,但相反的是不!