Django电子邮件应用程序虚线 - 最大行长度(以及如何更改它)? [英] Django email app broken lines - maximum line length (and how to change it)?

查看:159
本文介绍了Django电子邮件应用程序虚线 - 最大行长度(以及如何更改它)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

# 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视图代码导致包含虚线的文本文件:

The Django view code above, results in a text file that contains a broken line:

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中是否有一些设置可以控制?版本1.2的Django。

Anyone know how to change it so the output file doesn't have linebreaks? Is there some setting in Django that controls this? Version 1.2 of Django.

更新 - 备份一个级别并解释我的原始问题:)我是
使用 django-registration 应用程序,它发送一个包含
帐户激活链接的电子邮件。此链接是一个长的URL,最后有一个随机的
令牌(30+个字符),结果是该行在令牌的中间被打破。

Update - to back up a level and explain my original problem :) I'm using the django-registration app, which sends an email with an account activation link. This link is a long URL, with a random token at the end (30+ characters), and as a result, the line is breaking in the middle of the token.

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

In case the problem was using the Django's filebased EmailBackend, I switched to the smtp backend and ran the built-in Python smtpd server, in debugging mode. This dumped my email to the console, where it was still broken.

我确定,django-registration 正在工作,数以百万计的人使用它:)所以它必须是我做错了或错误配置的东西。我只是不知道什么。

I'm sure django-registration is working, with zillions of people using it :) So it must be something I've done wrong or mis-configured. I just have no clue what.

更新2 - 根据Django列表中的帖子,它真的是底层的 Python email.MIMEText对象,如果正确,只能再次将问题推回来。它仍然没有告诉我如何解决它。看看文档,我没有看到任何甚至提到换行的东西。

Update 2 - according to a post in a Django list, it's really the underlying Python email.MIMEText object, which, if correct, only pushes the problem back a little more. It still doesn't tell me how to fix it. Looking at the docs, I don't see anything that even mentions line-wrapping.

更新3(叹息) - 我排除了它是一个MIMEText对象问题。我使用了一个纯Python程序和smtplib / MIMEText来创建和发送一个测试电子邮件,它工作正常。它使用了一个charset =us-ascii,其中有人建议是在MIMEText对象中 包装文本的唯一字符集。我不知道这是否正确,但我更仔细地看了我的Django电子邮件输出,它的字符串为utf-8。

Update 3 (sigh) - I've ruled out it being a MIMEText object problem. I used a pure Python program and the smtplib/MIMEText to create and send a test email, and it worked fine. It also used a charset = "us-ascii", which someone suggested was the only charset to not wrap text in MIMEText objects. I don't know if that's correct or not, but I did look more closely at my Django email output, and it has a charset of "utf-8".

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

Could the wrong charset be the problem? And if so, how do I change it in Django?

以下是Django电子邮件中的所有输出流:

Here's the entire output stream from Django's email:

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


推荐答案

您可以通过创建EmailMessage对象并传入headers = {'格式,让您的电子邮件客户端不会破坏78个字符的软限制':'flowed'}像这样:

You might be able to get your email client to not break on the 78 character soft limit by creating an EmailMessage object and passing in headers={'format': 'flowed'} Like so:

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设置将文件发送到根据电子邮件中定义的规则呈现电子邮件的实际电子邮件客户端r。

If this doesn't work, try using a non-debug smtp setup to send the file to an actual email client that renders the email according to rules defined in the email header.

这篇关于Django电子邮件应用程序虚线 - 最大行长度(以及如何更改它)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆