Skip to content

Commit faec117

Browse files
committed
fix: SMTP EmailPolicy not using the correct line length for RCF 2045 enc
Signed-off-by: wangshun <96341168+xixi-box@users.noreply.github.com>
1 parent c07d1f4 commit faec117

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

‎Lib/email/contentmanager.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ def _finalize_set(msg, disposition, filename, cid, params):
132132
def _encode_text(string, charset, cte, policy):
133133
# If max_line_length is 0 or None, there is no limit.
134134
maxlen = policy.max_line_length or sys.maxsize
135+
# RFC 2045 limits the length of content-transfer-encoded lines to 76
136+
# characters, while max_line_length (78 by default) is the RFC 5322
137+
# limit for header lines.
138+
encoded_maxlen = min(maxlen, 76) if policy.max_line_length else maxlen
135139
lines = string.encode(charset).splitlines()
136140
linesep = policy.linesep.encode('ascii')
137141
def embedded_body(lines): return linesep.join(lines) + linesep
@@ -146,7 +150,7 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
146150
if policy.cte_type == '8bit':
147151
return '8bit', normal_body(lines).decode('ascii', 'surrogateescape')
148152
sniff = embedded_body(lines[:10])
149-
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), maxlen)
153+
sniff_qp = quoprimime.body_encode(sniff.decode('latin-1'), encoded_maxlen)
150154
sniff_base64 = binascii.b2a_base64(sniff)
151155
# This is a little unfair to qp; it includes lineseps, base64 doesn't.
152156
if len(sniff_qp) > len(sniff_base64):
@@ -161,9 +165,9 @@ def normal_body(lines): return b'\n'.join(lines) + b'\n'
161165
data = normal_body(lines).decode('ascii', 'surrogateescape')
162166
elif cte == 'quoted-printable':
163167
data = quoprimime.body_encode(normal_body(lines).decode('latin-1'),
164-
maxlen)
168+
encoded_maxlen)
165169
elif cte == 'base64':
166-
data = binascii.b2a_base64(embedded_body(lines), wrapcol=maxlen).decode('ascii')
170+
data = binascii.b2a_base64(embedded_body(lines), wrapcol=encoded_maxlen).decode('ascii')
167171
else:
168172
raise ValueError("Unknown content transfer encoding {}".format(cte))
169173
return cte, data

0 commit comments

Comments
 (0)