File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -252,7 +252,11 @@ def _handle_multipart(self, msg):
252252 msg .set_boundary (boundary )
253253 # If there's a preamble, write it out, with a trailing CRLF
254254 if msg .preamble is not None :
255- self .write (msg .preamble + self ._NL )
255+ if self ._mangle_from_ :
256+ preamble = fcre .sub ('>From ' , msg .preamble )
257+ else :
258+ preamble = msg .preamble
259+ self .write (preamble + self ._NL )
256260 # dash-boundary transport-padding CRLF
257261 self .write ('--' + boundary + self ._NL )
258262 # body-part
@@ -270,7 +274,11 @@ def _handle_multipart(self, msg):
270274 self .write (self ._NL + '--' + boundary + '--' )
271275 if msg .epilogue is not None :
272276 self .write (self ._NL )
273- self .write (msg .epilogue )
277+ if self ._mangle_from_ :
278+ epilogue = fcre .sub ('>From ' , msg .epilogue )
279+ else :
280+ epilogue = msg .epilogue
281+ self .write (epilogue )
274282
275283 def _handle_multipart_signed (self , msg ):
276284 # The contents of signed parts has to stay unmodified in order to keep
Original file line number Diff line number Diff line change @@ -1283,6 +1283,28 @@ def test_dont_mangle_from(self):
12831283Blah blah blah
12841284""" )
12851285
1286+ def test_mangle_from_in_preamble_and_epilog (self ):
1287+ s = StringIO ()
1288+ g = Generator (s , mangle_from_ = True )
1289+ msg = email .message_from_string (textwrap .dedent ("""\
1290+ 1291+ Mime-Version: 1.0
1292+ Content-Type: multipart/mixed; boundary=XXX
1293+
1294+ From somewhere unknown
1295+
1296+ --XXX
1297+ Content-Type: text/plain
1298+
1299+ foo
1300+
1301+ --XXX--
1302+
1303+ From somewhere unknowable
1304+ """ ))
1305+ g .flatten (msg )
1306+ self .assertEqual (len ([1 for x in s .getvalue ().split ('\n ' )
1307+ if x .startswith ('>From ' )]), 2 )
12861308
12871309
12881310# Test the basic MIMEAudio class
Original file line number Diff line number Diff line change @@ -52,6 +52,9 @@ Core and Builtins
5252Library
5353-------
5454
55+ - Issue #15232: when mangle_from is True, email.Generator now correctly mangles
56+ lines that start with 'From' that occur in a MIME preamble or epilogue.
57+
5558- Issue #15094: Incorrectly placed #endif in _tkinter.c.
5659 Patch by Serhiy Storchaka.
5760
You can’t perform that action at this time.
0 commit comments