return payload def findmaintext(selfmessageasstr=true)""for text-oriented clientsreturn first text part' strfor the payload of simple messageor all parts of multipart messagelooks for text/plainthen text/htmlthen text/*before deducing that there is no text to displaythis is heuristicbut covers most simplemultipart/alternativeand multipart/mixed messagescontent-type defaults to text/plain if not in simple msghandles message nesting at top level by walking instead of list scansif non-multipart but type is text/htmlreturns the html as the text with an html typecaller may open in web browserextract plain textetcif nonmultipart and not textthere is no text to displaysave/open message content in uicaveatdoes not try to concatenate multiple inline text/plain parts if any etext payloads may be bytes--decodes to str here easstr=false to get raw bytes for html file saves""try to find plain text for part in message walk()type part get_content_type(if type ='text/plain'return typeself decodedpayload(partasstrtry to find an html part for part in message walk()type part get_content_type(if type ='text/html'return typeself decodedpayload(partasstrwalk visits message if nonmultipart may be base ,qp,uu bytes to str toocaller renders html try any other text typeincluding xml for part in message walk()if part get_content_maintype(='text'return part get_content_type()self decodedpayload(partasstrpuntcould use first partbut it' not marked as text failtext '[no text to display]if asstr else '[no text to display]return 'text/plain'failtext def decodeheader(selfrawheader)"" edecode existing message header text per both email and unicode standardsaccording to its contentreturn as is if unencoded or failsclient must call this to displayparsed message object does not decodei header example'=?utf- ? ?introducing= top= values= savers?=' header example'man where did you get that =?utf- ? ?assistant= ?='decode_header handles any line breaks in header string automaticallymay return multiple parts if any substrings of hdr are encodedand returns all client-side scripting