下面是一个简单的测试,它演示了我所看到的问题:

import smtplib
from email.message import EmailMessage

my_addr = 'my_email@my_domain.com'

str = ''
for i in range(500):
    str += f'{i}: <a href="https://google.com">https://google.com</a><br>'

msg = EmailMessage()
msg.set_content('')
msg.add_alternative(str, subtype='html')
msg['Subject'] = 'html test'
msg['From'] = my_addr
msg.add_header('reply-to', my_addr)
msg['To'] = my_addr

with smtplib.SMTP('localhost', timeout=30) as smtp_server:
    smtp_server.send_message(msg)

由此产生的邮箱似乎在输入的html中每1000个字节就"中断"一次.此长度包括HTML字符串中的所有标记.收到的邮箱如下所示:

enter image description here

enter image description here

enter image description here

我try 了不同的SMTP服务器,使用不同的消息长度限制,但都设置为10M或更高,所以我怀疑这是否有任何效果.此外,我没有收到错误代码或异常.每样东西都送得干干净净.Smtplib似乎以某种方式打破了这一信息.

你知道问题出在哪里吗?或者如何纠正这个问题?

推荐答案

Problem:
It is a result of line line length limit imposed by SMTP servers.
It causes breaking too long lines.

Solution:
Insert line break characters into your html string.

for i in range(500):
  str += f'{i}: <a href="https://google.com">https://google.com</a><br>\n'

tidy程序可用于打断较长的HTML行(除其他事项外).

Links:
Is there an accepted maximum line length for SMTP header fields?

Python相关问答推荐

如何判断. text文件中的某个字符,然后读取该行

如何使用函数正确索引收件箱?

删除pandas rame时间序列列中未更改的值

Pandas 在时间序列中设定频率

Pandas :多索引组

有条件地采样我的大型DF的最有效方法

Pandas 在最近的日期合并,考虑到破产

试图找到Python方法来部分填充numpy数组

Pytest两个具有无限循环和await命令的Deliverc函数

什么相当于pytorch中的numpy累积ufunc

如何列举Pandigital Prime Set

如何在虚拟Python环境中运行Python程序?

如何从在虚拟Python环境中运行的脚本中运行需要宿主Python环境的Shell脚本?

加速Python循环

在Mac上安装ipython

如何请求使用Python将文件下载到带有登录名的门户网站?

基于索引值的Pandas DataFrame条件填充

在ubuntu上安装dlib时出错

Pandas Loc Select 到NaN和值列表

有没有一种ONE—LINER的方法给一个框架的每一行一个由整数和字符串组成的唯一id?