␡
- The rfc822 Module
- A Simple DTD for E-mail
- An Example of an E-mail Message in XML
- Processing a Eudora Mailbox
- Processing a Linux Mailbox
- Processing an E-mail Message by Using the rfc822 Module
- Sending E-mail by Using xMail
- Source Code for the SendxMail Application
- Source Code for the xMail Application
This chapter is from the book
14.8 | -Source Code for the SendxMail Application
CD-ROM reference=14017.txt """ sendxMail XML Processing with Python Sean Mc Grath Send e-mail over the Internet to a group of e-mail accounts, using the xmail XML representation. The program connects to the specified SMTP server and uses Python's smtplib library. The list of addresses in also in XML. A simple message file looks like this: <xmail> <message> <headers> <field><name>subject</name><value>Greetings</value></field> </headers> <body> Hello World </body> </message> </xmail> A simple address file looks like this: <contacts> <contact> <name>Neville Bagnall</name> <email>neville@digitome.com</email> </contact> </contacts> Sample invocation: -python sendxmail.py sean@digitome.com contacts.xml email.xml gpo.iol.ie """ import smtplib from pyxie import * # Class uses event-driven XML processing style to send messages # one at a time and so inherits from xDispatch. class xMailSender(xDispatch): -def __init__(self,Sender,MailingListFile,MessageFile, SMTPServer): -# PYX source for later event dispatching is the # message file xDispatch.__init__(self,File2PYX(MessageFile)). # The Gathered variable is used to gather characters # arriving in the data handler method between certain # start- and end-tags. self.Gathered = [] self.Sender = Sender self.Addresses = [] self.MessageFile = MessageFile # Accumulated message header self.MessageHeader = "" # Accumulated message body self.MessageText = "" self.Recepients = [] self.server = smtplib.SMTP( SMTPServer ) self.server.set_debuglevel(1) # Use tree-processing style to assemble list of # recipients. T = File2Tree(self.MessageListFile) for n in T: T.Seek(n) if T.AtElement("email"): email = T.JoinData(" ") self.Addresses.append(email) # Invoke event dispatching to handler methods # PYX source is the message file. self.Dispatch() def start_body(self,etn,attrs): # Reset gathered data for each message body. self.Gathered = [] def end_body(self,etn,attrs): # Save gathered data as message body. self.messageText = string.join(self.Gathered) def start_name(self,etn,attrs): # Reset gathered data for each name element. self.Gathered = [] def start_value(self,etn,attrs): # Reset gathered data for each value element. self.Gathered = [] def end_name(self,etn): -# Save gathered data as header field # recipient name. self.fieldname = string.join(self.Gathered) def end_value(self,etn): # Save gathered data as header field value. -self.fieldvalue = string.join(self. Gathered) -# Add the new name/value pair to the end of # the message header. -self.MessageHeader = self.MessageHeader + self.fieldname + ": " + self.fieldvalue + "\n" def characters(self,str): -# Handler for character data. Accumulate # data in the Gathered variable.
Various # end-tag handlers copy out the accumulated # contents as needed. self.Gathered.append (PYXDecoder(str)) def end_body(self,etn): -# At this point, we have everything we need # to send e-mail. -self.MessageText = string.join (self .Gathered) self.server.sendmail (self.Sender, -self.Addresses, self.MessageHeader+"\n"+ self.MessageText ) # Close down the SMTP connection. self.server.quit() if __name__ == '__main__': import sys if len(sys.argv)==1: xMailSender ("sean@digitome.com", "contacts.xml", "email.xml", "gpo.iol.ie") else: xMailSender (sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])