making mail with attachments is little more workbut not muchwe just make root message and attach nested message objects created from the mime type object that corresponds to the type of data we're attaching the mimetext classfor instanceis subclass of messagewhich is tailored for text partsand knows how to generate the right types of header information when printed mimeimage and mimeaudio similarly customize message for images and audioand also know how to apply base and other mime encodings to binary data the root message is where we store the main headers of the mailand we attach parts hereinstead of setting the entire payload--the payload is list nownot string mimemultipart is message that provides the extra header protocol we need for the rootfrom email mime multipart import mimemultipart message subclasses from email mime text import mimetext with extra headers+logic top mimemultipart(root message object top['from''art subtype default=mixed top['to''pp @learning-python comsub mimetext('nice red uniforms \ 'part message attachments sub mimetext(open('data txt'read()sub add_header('content-disposition''attachment'filename='data txt'top attach(sub top attach(sub when we ask for the texta correctly formatted full mail text is returnedseparators and allready to be sent with smtplib--quite trickif you've ever tried this by handtext top as_string(or dostr(topor print(topprint(textcontent-typemultipart/mixedboundary="=============== ==mime-version fromart topp @learning-python com --=============== =content-typetext/plaincharset="us-asciimime-version content-transfer-encoding bit nice red uniforms --=============== =content-typetext/plaincharset="us-asciimime-version content-transfer-encoding bit content-dispositionattachmentfilename="data txtline line line --=============== ==-emailparsing and composing mail content