practicesuch as message/delivery status most messages have main text partthough it is not requiredand may be nested in multipart or other construct in all casesan email message is simplelinear stringbut these message structures are automatically detected when mail text is parsed and are created by your method calls when new messages are composed for instancewhen creating messagesthe message attach method adds parts for multipart mailsand set_payload sets the entire payload to string for simple mails message objects also have assorted properties ( the filename of an attachment)and they provide convenient walk generator methodwhich returns the next message in the payload each time through in for loop or other iteration context because the walker yields the root message object first ( self)single-part messages don' have to be handled as special casea nonmultipart message is effectively message with single item in its payload--itself ultimatelythe message object structure closely mirrors the way mails are formatted as text special header lines in the mail' text give its type ( plain text or multipart)as well as the separator used between the content of nested parts since the underlying textual details are automated by the email package--both when parsing and when composing--we won' go into further formatting details here if you are interested in seeing how this translates to real emailsa great way to learn mail structure is by inspecting the full raw text of messages displayed by email clients you already useas we'll see with some we meet in this book in factwe've already seen few--see the raw text printed by our earlier pop email scripts for simple mail text examples for more on the message objectand email in generalconsult the email package' entry in python' library manual we're skipping details such as its available encoders and mime object classes here in the interest of space beyond the email packagethe python library includes other tools for mail-related processing for instancemimetypes maps filename to and from mime typemimetypes guess_type(filenamemaps filename to mime type name spam txt maps to text/plan mimetypes guess_extension(contypemaps mime type to filename extension type text/html maps to html we also used the mimetypes module earlier in this to guess ftp transfer modes from filenames (see example - )as well as in where we used it to guess media player for filename (see the examples thereincluding playfile pyexample - for emailthese can come in handy when attaching files to new message (guess_typeand saving parsed attachments that do not provide filename (guess_extensionin factthis module' source code is fairly complete reference to mime types see the library manual for more on these tools emailparsing and composing mail content