# 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 ------------