trytext encode(bodyencodingexcept (unicodeerrorlookuperror)bodyencoding 'utf- try ascii first (or latin- ?else use tuf as fallback (or config?tbdthis is more limited than pymailgui use utf for all attachmentswe can' ask here attachencodings ['utf- 'len(attachesignored for non-text parts encode and send sender mailtools silentmailsender(smtpservernametrysender sendmessage(fromtossubjextrahdrstextattachesbodytextencoding=bodyencodingattachesencodings=attachencodingsexceptcommonhtml errorpage('send mail error'elsecommonhtml confirmationpage('send mail'this script gets mail header and text input information from the edit page' form (or from query parameters in an explicit urland sends the message off using python' standard smtplib modulecourtesy of the mailtools package we studied mailtools in so won' say much more about it now notethoughthat because we are reusing its send callsent mail is automatically saved in sentmail txt file on the serverthere are no tools for viewing this in pymailcgi itselfbut it serves as log new in version the saveattachments function grabs any part files sent from the browser and stores them in temporary local files on the server from which they will be added to the mail when sent we covered cgi upload in detail at the end of see that discussion for more on how the code here works (as well as its limitations in python and this edition--we're attaching simple text here to accommodatethe business of attaching the files to the mail itself is automatic in mailtools utility in commonhtml ultimately fetches the name of the smtp server to receive the message from either the mailconfig module or the script' inputs (in form field or url query parameterif all goes wellwe're presented with generated confirmation pageas captured in figure - open file sentmail txt in pymailcgi' source directory if you want to see what the resulting mail' raw text looks like when sent (or fetch the message in an email client with raw text viewsuch as pymailguiin this versioneach attachment part is mime encoded per base with utf- unicode encoding in the multipart messagebut the main text part is sent as simple ascii if it works as such as we'll seethis send mail script is also used to deliver reply and forward messages for incoming pop mail the user interface for those operations is slightly different for composing new email from scratchbut as in pymailguithe submission handler logic has been factored into the sameshared code--replies and forwards are really just mail send operations with quoted text and preset header fields sending mail by smtp