data open(filename'rb'msg mimetext(data read()_subtype=subtype_charset=fileencodedata close(elif maintype ='image'data open(filename'rb' euse fix for binaries msg mimeimagedata read()_subtype=subtype_encoder=fix_encode_base data close(elif maintype ='audio'data open(filename'rb'msg mimeaudiodata read()_subtype=subtype_encoder=fix_encode_base data close(elif maintype ='application'new in data open(filename'rb'msg mimeapplicationdata read()_subtype=subtype_encoder=fix_encode_base data close(elsedata open(filename'rb'msg mimebase(maintypesubtypemsg set_payload(data read()data close(fix_encode_base (msg#email encoders encode_base (msgapplication/could use this code too make generic type was broken here tooencode using base set filename and attach to container basename os path basename(filenamemsg add_header('content-disposition''attachment'filename=basenamemainmsg attach(msgtext outside mime structureseen by non-mime mail readers mainmsg preamble ' multi-part mime format message \nmainmsg epilogue 'make sure message ends with newline def savesentmessage(selffulltextsavemailseparator)""append sent message to local file if send worked for anyclientpass separator used for your applicationsplitscaveatuser may change the file at same time (unlikely)""trysentfile open(mailconfig sentmailfile' 'encoding=mailconfig fetchencodingif fulltext[- !'\ 'fulltext +'\nsentfile write(savemailseparatorsentfile write(fulltextsentfile close(except client-side scripting