[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Though not shown in this session, you can also send to multiple recipients, and include
full name and address pairs in your email addresses. This works just because the script
employs email utilities described earlier to split up addresses and fully parse to allow
commas as both separators and name characters. The following, for example, would
send to two and three recipients, respectively, using mostly full address formats:


[Pymail] Action? (i, l, d, s, m, q, ?) m
From? "moi 1" <[email protected]>
To? "pp 4e" <[email protected]>, "lu,tz" <[email protected]>

[Pymail] Action? (i, l, d, s, m, q, ?) m
From? The Book <[email protected]>
To? "pp 4e" <[email protected]>, "lu,tz" <[email protected]>,
[email protected]

Finally, if you are running this live, you will also find the mail save file on your machine,
containing the one message we asked to be saved in the prior session; it’s simply the
raw text of saved emails, with separator lines. This is both human and machine-
readable—in principle, another script could load saved mail from this file into a Python
list by calling the string object’s split method on the file’s text with the separator line
as a delimiter. As shown in this book, it shows up in file C:\temp\savemail.txt, but you
can configure this as you like in the mailconfig module.


The mailtools Utility Package


The email package used by the pymail example of the prior section is a collection of
powerful tools—in fact, perhaps too powerful to remember completely. At the mini-
mum, some reusable boilerplate code for common use cases can help insulate you from
some of its details; by isolating module usage, such code can also ease the migration to
possible future email changes. To simplify email interfacing for more complex mail
clients, and to further demonstrate the use of standard library email tools, I developed
the custom utility modules listed in this section—a package called mailtools.


mailtools is a Python modules package: a directory of code, with one module per tool
class, and an initialization module run when the directory is first imported. This pack-
age’s modules are essentially just a wrapper layer above the standard library’s email
package, as well as its poplib and smtplib modules. They make some assumptions about
the way email is to be used, but they are reasonable and allow us to forget some of the
underlying complexity of the standard library tools employed.


In a nutshell, the mailtools package provides three classes—to fetch, send, and parse
email messages. These classes can be used as superclasses in order to mix in their meth-
ods to an application-specific class, or as standalone or embedded objects that export
their methods for direct calls. We’ll see these classes deployed both ways in this text.


As a simple example of this package’s tools in action, its selftest.py module serves as
a self-test script. When run, it sends a message from you, to you, which includes the
selftest.py file as an attachment. It also fetches and displays some mail headers and


956 | Chapter 13: Client-Side Scripting

Free download pdf