Python

  1. Home
  2. Computing & Technology
  3. Python

Building a Whitelist for a Spam Filter

From Al Lukaszewski, for About.com

4 of 6

Culling Out the Recipients

Having created an object of the mailbox, now we can create a for loop to cull out the 'To' field of each email message.

     for message in box:
         recip = message.getaddr('To')[1]
         addresses.add(recip)
The mailbox relies on a cornucopia of other Python modules; best to see the source code for which ones. The second line of the above code uses the getaddr() method of the class rfc822.Message (from the module rfc822). This method returns a single line from an email header in the form of a tuple. Consequently, we must use subscript a '1' in order to assign only the email address, not the name of the addressee, to variable recip.

Finally, we use the built-in method of the addresses dictionary to add the value to our list.

Explore Python

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Python

  1. Home
  2. Computing & Technology
  3. Python

©2009 About.com, a part of The New York Times Company.

All rights reserved.