- 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
14.7 | Sending E-mail by Using xMail
Having converted the e-mail to XML, we can process it in a variety of ways by using any XML-aware databases, editors, search engines, and so on. We can contemplate processing them with Python by using Pyxie or SAX- or DOM-style processing. One useful form of processing would be to send e-mail from this XML notation. In this section, we develop a Pyxie application, sendxMail, to do that.
Sending e-mail to a group of people at the same time is common, so we start by defining an XML notation for a mailing list. Here is a sample document conforming to a contacts DTD.
CD-ROM reference=14012.txt <!DOCTYPE contacts SYSTEM "contacts.dtd"> <contacts> <contact> <name>Neville Bagnall</name> <email>email@example.com</email> </contact> <contact> <name>Noel Duffy</name> <email>firstname.lastname@example.org</email> </contact> <contact> <name>Sean Mc Grath</name> <email>email@example.com</email> </contact> </contacts>
The DTD for this is, of course, trivial.
CD-ROM reference=14013.txt C>type contacts.dtd <!-- Trivial DTD for a mailing list --> <!ELEMENT contacts (contact)*> <!ELEMENT contact (name,email)> <!ELEMENT name (#PCDATA)> <!ELEMENT email (#PCDATA)>
The full source code for sendxMail is given at the end of this chapter. The program uses the smtplib Python standard library. This library allows Python programs to send e-mail messages by talking to an SMTP server. Here is a small test program that illustrates how the smtplib module works.
CD-ROM reference=14014.txt """ Small test program to illustrate Python's standard smtplib library """ import smtplib SMTPServer = "gpo.iol.ie" #Create an SMTP server object. server = smtplib.SMTP (SMTPServer) #Turn debugging on. server.set_debuglevel(1) # Send an e-mail. First argument is the sender. Second argument # is a list of recepients. Third argument is the text of the # message. server.sendmail ( "From:firstname.lastname@example.org", ["email@example.com"], "Hello World")
To execute this program, change SMPTServer to point to a suitable SMTP server. The program will produce a lot of output because debugging has been turned on. The abridged output from an execution of this program is shown here.
CD-ROM reference=14015.txt send: 'ehlo GATEWAY\015\012' reply: '250-gpo2.mail.iol.ie Hello dialup-024.ballina.iol.ie [18.104.22.168], pleased to meet you\015\012' -reply: retcode (250); Msg: gpo2.mail.iol.ie Hello dialup_ 024.ballina.iol.ie [22.214.171.124], pleased to meet you send: 'mail FROM:<firstname.lastname@example.org> size=11\015\012' reply: '250 <email@example.com>... Sender ok\015\012' reply: retcode (250); Msg: <firstname.lastname@example.org>... Sender ok send: 'rcpt TO:<email@example.com>\015\012' reply: '250 <firstname.lastname@example.org>... Recipient ok\015\012' reply: retcode (250); Msg: <email@example.com>... Recipient ok send: 'data \015\012' reply: '354 Enter mail, end with "." on a line by itself\ 015\012' reply: retcode (354); Msg: Enter mail, end with "." on a line by itself data: (354, 'Enter mail, end with "." on a line by itself') send: 'Hello World' send: '\015\012.\015\012' reply: '250 QAA03299 Message accepted for delivery\015\012' reply: retcode (250); Msg: QAA03299 Message accepted for delivery data: (250, 'QAA03299 Message accepted for delivery')
To execute sendxMail, you provide it with four parameters:
The e-mail address of the sender.
The XML document containing the list of recepients. This file should conform to the contacts DTD.
The e-mail message document. This document should conform to the xMail DTD.
The name of the SMTP server to use.
A sample invocation is shown below.
CD-ROM reference=14016.txt C>-python sendxMail.py firstname.lastname@example.org PyxieList.xml Welcome.xml gpo.iol.ie