Old-skool mailing lists

As an undergrad, my email was hosted on a VAX. And, some of us wrote little scripts and programs on them. We particularly had fun with mail bombers, which were scripts that sent the same message over, and over, and over.

We won't do that. But we can write a simple program for sending a note to all of our classmates.

The Python Tutorial will likely come in handy on this project.

(This isn't a lab so much as a mixed in/out-of-class assignment-exploration-thing.)

CSV part I

Once you can go through the file, you're going to need to pull apart each line of the file. We need to transform each line into a data structure that contains the parts of the line (the bits separated by commas) into something we can use in our computation.

csv

First, you need a list of everyone in the class. In the real world, you don't hard code this into your program—instead, you find a datafile containing the information you need. In this case, it is in Sakai.

[SAKAI] CSV of class roster

Put this file in the same directory as your script. We'll come back to pulling it apart in a moment.

Processing a file

exploration

As a first step, you're going to need to write a few lines of Python that can open a file. Then, I'd see if you can write a loop that goes through each line of the file and prints it to the screen.

[PYDOC] Loops

[PYDOC] Input and Output

organization

Then, once you figure that out, see if you can turn it into a function that takes the filename as a parameter. The last line of your script should then call this function using that parameter. 

[PYDOC] Functions

CSV part II

Now, we can go through each line of the file, and print it to the screen.

Write another function that takes a string as an argument, and returns that string. In short, it should be a version of the identity function. In your loop, pass each string to this function before printing it.

After that works, modify the function so that it uses regular expressions to pull the email address out of the string, and return only the email address.

[PYDOC] re overview

[PYDOC] re documentation

[WWW] Regexp Tutor (interactive)


Adding structure

You currently have a function that takes a filename and prints things out. Modify it so it instead returns a list of the substrings (the email addresses) that it finds.

[PYDOC] Lists

Creating a message

Create a file in the same directory as your script called message.txt. Write a message for your classmates. 

Ideally, it should contain a URL to an amazing LOLCat image. Or, perhaps, an XKCD comic. Or, even, a Dinosaur Comic

Sending email

You have, at this point, a function that will give you a list of email addresses. You're about 80% of the way there.

Now, there is a package in Python for creating Message objects. You could read all of that. Or, you could start with the examples they provide. 

[PYDOC] email examples

[PYDOC] email module

You should be able to modify the example to create a function that reads in your message and sends it out. Start by using this function to test your ability to send mail to yourself.

Plug in the pieces

You should have a few functions that do simple tasks. Plug your email address list generator into a for loop, and in that loop, call your function that sends email messages. You may need to tweak that function so it takes an email address as a parameter, but you can certainly manage that.

At this point, you've got your own email message sender.

You could go further, of course. For example, you could pass in the email address file and message file as parameters on the command line (so you can reuse the script to send different messages to different groups of people). 

Or, you could write something that posts to your weblog automatically, Or, parses RSS feeds, or reads newsgroups, or...

Or, you can quit for now.

In Conclusion

This is a semi-structured exercise that gives you some guidance on developing a simple Python script. In writing this script, you need to explore functions, lists, regular expressions, file I/O, and one of Python's many libraries of code (the email module). 

I expect to receive a message from everyone. 

Try not to screw up the loop where the sending happens, eh?

Creative Commons License This work is licensed under a CC BY-SA 3.0 License.