【问题标题】:unwanted line break in email header when using email.mime使用 email.mime 时,电子邮件标题中出现不需要的换行符
【发布时间】:2012-03-05 06:53:53
【问题描述】:

我正在使用 SMTP 并使用 email.mime 来提供标头结构。由于某种原因,当尝试添加超过一定长度的标题时,会在我的标题行中添加换行符。

例如

from email.mime.text import MIMEText
message = 'some message'
msg = MIMEText(message)
msg.add_header('some header', 'just wondering why this sentence is continually cut in half for a reason I can not find')

print msg['some header']
print msg

print msg['some header'] 打印:-

some header: just wondering just wondering why this sentence is continually cut in half for a reason I can not find

打印味精打印:-

some header: just wondering why this sentence is continually cut in half for a
 reason I can not find

我确实发现的一件事是,它被截断的长度是标题标题及其值的组合。因此,当我将 'some header' 缩短为 'some' 时,行 return 变为 after 'reason' 而不是 before。

这不仅仅是我查看页面的宽度:),它实际上是在电子邮件标题中发送带有换行符的电子邮件。

有什么想法吗?

【问题讨论】:

    标签: python mime mime-message


    【解决方案1】:

    这是正确的行为,它是 email 包执行此操作(以及大多数电子邮件生成代码)。不必是单行。像这样折叠标题被认为是一种很好的做法,并且缩进标题正文其余部分的制表符意味着标题是继续的。

    【讨论】:

    • 另见:ietf.org/rfc/rfc2822.txt 第 2.1.1 节描述了建议的 78 个字符的行长,第 2.2.3 节描述了行“折叠”。
    • @thomas 感谢您的快速回复。有没有办法覆盖电子邮件包,所以它不会强制换行?
    • @Marty 谢谢那个Marty。
    • 我相信唯一真正的方法是直接实例化email.header.Header,或者用更大的值替换email.header.MAXLINELEN。不过,我建议不要这样做。生成的email 包是完全兼容和鼓励的格式。也许您不应该做要求消息以等效但不同的格式表示的事情。
    • @ThomasWouters 再次感谢 Thomas。
    猜你喜欢
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多