9
# settings.py
EMAIL_BACKEND = 'django.core.mail.backends.filebased.EmailBackend'

# view.py
from django.core.mail import send_mail

def send_letter(request):
    the_text = 'this is a test of a really long line that has more words that could possibly fit in a single column of text.'
    send_mail('some_subject', the_text, 'me@test.com', ['me@test.com'])

上面的 Django 视图代码会生成一个包含虚线的文本文件:

this is a test of a really long line that has more words that could possibl=
y fit in a single column of text.
-------------------------------------------------------------------------------

任何人都知道如何更改它以使输出文件没有换行符?Django中有一些设置可以控制这个吗?Django 1.2 版。

更新 - 备份一个级别并解释我原来的问题 :) 我正在使用django-registration应用程序,它会发送一封带有 帐户激活链接的电子邮件。此链接是一个长 URL,末尾带有一个随机标记(30 多个字符),因此,该行在标记中间中断。

如果问题是使用 Django 的基于文件的EmailBackend,我切换到smtp后端并在调试模式下运行内置的 Python smtpd 服务器。这将我的电子邮件转储到控制台,它仍然被破坏。

我确定django-registration正在工作,有无数人在使用它:) 所以这一定是我做错了或配置错误。我只是不知道是什么。

更新 2 - 根据 Django 列表中的帖子,它实际上是底层Python email.MIMEText 对象,如果正确,它只会将问题推回一点。它仍然没有告诉我如何解决它。查看文档,我没有看到任何甚至提到换行的内容。

更新 3(叹气) - 我已经排除它是 MIMEText 对象问题。我使用纯 Python 程序和 smtplib/MIMEText 来创建和发送测试电子邮件,它运行良好。它还使用一个 charset = "us-ascii",有人建议它是唯一一个不在MIMEText 对象中包装文本的字符集。我不知道这是否正确,但我确实更仔细地查看了我的 Django 电子邮件输出,它有一个字符集“utf-8”。

错误的字符集可能是问题吗?如果是这样,我如何在 Django 中更改它?

这是 Django 电子邮件的整个输出流:

---------- MESSAGE FOLLOWS ----------
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Subject: some_subject
From: me@test.com
To: me@test.com
Date: Tue, 17 May 2011 19:58:16 -0000

this is a test of a really long line that has more words that could possibl=
y fit in a single column of text.
------------ END MESSAGE ------------
4

4 回答 4

6

您可以通过创建一个 EmailMessage 对象并传入 headers={'format': 'flowed'} 来让您的电子邮件客户端不超过 78 个字符的软限制,如下所示:

from django.core.mail import EmailMessage

def send_letter(request):
    the_text = 'this is a test of a really long line that has more words that could possibly fit in a single column of text.'
    email = EmailMessage(
        subject='some_subject', 
        body=the_text, 
        from_email='me@test.com', 
        to=['me@test.com'],
        headers={'format': 'flowed'})

    email.send()

如果这不起作用,请尝试使用非调试 smtp 设置将文件发送到根据电子邮件标头中定义的规则呈现电子邮件的实际电子邮件客户端。

于 2011-06-09T02:33:25.363 回答
0

我已经看到这是 python2.5,它在 python2.7 中已修复。

email/generator.py 中的相关代码现在有一条评论说

# Header's got lots of smarts, so use it.  Note that this is
# fundamentally broken though because we lose idempotency when
# the header string is continued with tabs.  It will now be
# continued with spaces.  This was reversedly broken before we
# fixed bug 1974.  Either way, we lose.

您可以在此处阅读有关该错误的信息http://bugs.python.org/issue1974

或者您可以在这行 email/generator.py 中将 '\t' 更改为 ' '

print >> self._fp, Header(
v, maxlinelen=self._maxheaderlen,
header_name=h, continuation_ws='\t').encode()
于 2012-05-01T15:15:43.953 回答
0

电子邮件行本身并没有“损坏”——它们只是以引用的可打印编码表示。因此,在 76 个字符=\n处插入。任何有能力的邮件客户端都应该正确解码消息并删除中断。

如果要表示解码后的电子邮件正文,可以通过传递decode=Trueget_payload方法来使用它:

body = email.get_payload(decode=True)

这告诉消息解码引用的可打印编码。

更重要的是,如果你主要关心的是让 python 控制台调试服务器打印解码的消息,你可以做一些像这个片段这样快速而肮脏的事情,而不是使用内置的DebuggingServer. 更准确地说,您可以将“数据”字符串解析为电子邮件对象,打印出您关心的标题,然后使用decode=True.

于 2012-01-25T04:34:16.207 回答
0

尝试EMAIL_BACKEND在您的settings.py. 也许它不能解决你的问题,但它是定义它的正确位置,否则它可能不会被使用。

(由于我不确定我是否在这里解决了您的问题,因此我试图对您的问题发表评论,但显然我不能。)

于 2011-05-08T22:26:44.423 回答