Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django使用£发送邮件;签名在outlook中显示不正确_Python_Django_Email - Fatal编程技术网

Python Django使用£发送邮件;签名在outlook中显示不正确

Python Django使用£发送邮件;签名在outlook中显示不正确,python,django,email,Python,Django,Email,我正在使用django使用内置的send_mail功能发送电子邮件 发送电子邮件的功能类似于: from __future__ import unicode_literals from django.template import loader from django.core.mail import send_mail #stuff to get user context = {'user':user} subject = loader.render_to_string('subjectte

我正在使用django使用内置的send_mail功能发送电子邮件

发送电子邮件的功能类似于:

from __future__ import unicode_literals

from django.template import loader
from django.core.mail import send_mail
#stuff to get user
context = {'user':user}
subject = loader.render_to_string('subjecttemplate.txt', context)
# Email subject cannot contain newlines
subject = ''.join(subject.splitlines())
message = loader.render_to_string('emailtemplate.html', context)
send_mail(subject, message, None, [user.email])
模板
emailtemplate.html
看起来像

Dear {{ user.first_name }},

The price is £20.

etc.
从大多数电子邮件客户端来看,这是我想要的。但是,在microsoft outlook中,消息中的英镑符号显示为�.

我假设这是某种编码不匹配的问题,但我不确定如何修复它。我是否需要在电子邮件标题中的某个地方声明电子邮件将采用unicode编码

更新

我对这件事还没有进一步的了解。我发现旧的PHP问题似乎可以通过删除标题中字符集周围的引号来解决。这是Django中类的
message
方法内置的,因此我编写了一个快速自定义类(见下文),并尝试使用它发送电子邮件,但似乎没有什么不同

import re

from django.core.mail.message import EmailMultiAlternatives
from django.conf import settings


class CustomEmailMultiAlternatives(EmailMultiAlternatives):
    """
    Customisation of built in Django email class to adjust header so that no
    quotes around charset
    """
    def message(self):
        msg = super(CustomEmailMultiAlternatives, self).message()
        charset = self.encoding or settings.DEFAULT_CHARSET
        if charset == 'utf-8':
            CT_header = msg.get('Content-Type')
            new_CT_header = re.sub(r'(charset=)(")(.*)(")', r'\1\3', CT_header)
            msg.replace_header('Content-Type', new_CT_header)
        return msg

只是在黑暗中拍摄:如果将字符串编码为UTF-8会发生什么?默认情况下,python3执行此操作,但python2不执行此操作。也许,默认编码是outlook中的其他编码?邮件到达时带有标题
内容类型:text/plain;charset=“utf-8”
所以我认为它已经告诉outlook它是utf-8了?很遗憾,outlook默认情况下似乎没有编码为utf-8。