You are to write a Python class, which is a file mailer.
You will not need to write very much original code for this project, but you will need to incorporate a few programs provided in The Text Book into your program. A stub program to get you started is also provided. The stub program includes the code to test your class, as well as, some helper functions. There are comments in the code for where to put your code and step-by-step instruction of what your code should do. You just need to find the example code from the book and incorporate the needed parts into it.
For your convenience, all the files needed may be downloaded from links at bottom of this page.
The parameter to pass to the initialization function of the class is the SMTP server to use (e.g., ‘smtp.sal.ksu.edu’ or ‘localhost:8025’). This string is of the same format of that used by the smtplib module to create a SMTP object. Note from the second example that the port number may be included in this string. If no port number is specified, port 25 is assumed.
Your program should check the server to see if there is a maximum message size. Throughout the program, you should also check for errors and print error messages rather than letting Python present exceptions to the user.
The class should include the following methods:
- __init__(self,server) – intialize the class and check the server for maximum message size.
- attach_file(file_name) – add a file to the message. If the file is found and can be opened, add it to a list of files to be attached as MIME attachments. Those files that can not be opened should be skipped. If maximum message size for the server is available, then it should only include files which would not make the message size exceed the maximum. This function is done for you.
- send(mail_to, mail_from) – send the message to the list of addresses in mail_to. Also add the ‘To’, ‘From’ and ‘Subject’ headers and add a short message body listing the files attached. The return value from the send() function is the number of files attached.
Programs in The Text Book that you will want to borrow from are:
- Chapter 9 - mime_gen_basic.py – Multipart MIME message with attachments
- Chapter 10 - debug.py – send message and check for errors
- Chapter 10 - ehlo.py – check server for maximum message size
To facilitate development and testing, you will probably also want to use the fakemail.py program. What this does is sets up an SMTP e-mail server on a specified port (read the code) so that your program can just connect to the ‘localhost’ and send messages to it. It puts the received messages in a directory. You might need to change the names of these files to have a .eml extension, but you can open the messages with an e-mail client program, such as Thunderbird, or what ever you use, to view the message and it’s attachments.
The advantage of using a fake mail server is that if your program is not correct at first (mine never are), you won’t get the SMTP server, which you use to send real messages, mad at you. Once you are satisfied from testing with the fake mail server, then you might want to try actually sending a message to a real sever.