Home > Articles > Web Services > XML

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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>neville@digitome.com</email>
</contact>
<contact>
<name>Noel Duffy</name>
<email>noel@digitome.com</email>
</contact>
<contact>
<name>Sean Mc Grath</name>
<email>sean@digitome.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:sean@digitome.com",
  ["paul@digitome.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
[194.125.48.152], pleased to meet you\015\012'
-reply: retcode (250); Msg: gpo2.mail.iol.ie Hello dialup_   024.ballina.iol.ie
[194.125.48.152], pleased to meet you
send: 'mail FROM:<sean@digitome.com> size=11\015\012'
reply: '250 <sean@digitome.com>... Sender ok\015\012'
reply: retcode (250); Msg: <sean@digitome.com>... Sender ok
send: 'rcpt TO:<paul@digitome.com>\015\012'
reply: '250 <paul@digitome.com>... Recipient ok\015\012'
reply: retcode (250); Msg: <paul@digitome.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 sean@digitome.com PyxieList.xml  Welcome.xml gpo.iol.ie
  • + Share This
  • 🔖 Save To Your Account