print short summary of sender/recipient print("from %sm["from"]print("to %sm["to"]print("subject %sm["subject"]print(""if not is_multipart()simple message just print the payload payload get_payload(decode=truecharset get_content_charset('iso- - 'print(payload decode(charset)elsemultipart message walk over all subparts and print text/plain fragments save any attachments for in walk()filename get_filename(if filenameprint("saving attachment%sfilenamedata get_payload(decode=trueopen(filename,"wb"write(dataelseif get_content_type(='text/plain'payload get_payload(decode=truecharset get_content_charset('iso- - 'print(payload decode(charset)in this exampleit is important to emphasize that operations that extract the payload of message always return byte strings if the payload represents textyou also need to decode it according to some character set the get_content_charset(and payload decode(operations in the example are carrying out this conversion composing email to compose an email messageyou can either create an empty instance of message objectwhich is defined in the email message moduleor you can use message object that was created by parsing an email message (see the previous sectionmessage(creates new message that is initially empty an instance of message supports the following methods for populating message with contentheadersand other information add_header(namevalue**paramsadds new message header name is the name of the headervalue is the value of the headerand params is set of keyword arguments that supply additional optional parameters for exampleadd_header('foo','bar',spam='major'adds the header line 'foobarspam="major"to the message as_string([unixfrom]converts the entire message to string unixfrom is boolean flag if this is set to truea unix-style 'from line appears as the first line by defaultunixfrom is false lib fl ff