1# Python mail sample
2
3import sys, smtplib
4
5
6class SMTPTest:
7    def __init__(self, interface='localhost', port=25):
8        self.svr = smtplib.SMTP(interface, port)
9        self.svr.set_debuglevel(1)
10
11    def sendmail(self, sender, recipient, message):
12        try:
13            self.svr.sendmail(sender, recipient, message)
14        except:
15            print "oops"
16
17    def quit(self):
18        self.svr.quit()
19
20def test():
21    sndr = "python-script-test@localhost"
22    rcpt = "tcllib-test@localhost"
23    mesg = """From: Python Mailer <python-script@localhost>
24To: Tcllib Tester <tcllib-test@localhost>
25Date: Fri Dec 20 14:20:49 2002
26Subject: test from python
27
28This is a sample message from Python.
29Hope it's OK
30Check transparency:
31. <- there should be one dot here.
32Done
33"""
34    # Connect
35    svr = SMTPTest('localhost')
36
37    # Try normal message
38    svr.sendmail(sndr, rcpt, mesg)
39
40    # should fail: invalid recipient.
41    svr.sendmail(sndr, "", mesg)
42
43    # should fail: NULL recipient only valid for sender
44    svr.sendmail(sndr, "<>", mesg)
45
46    # should be ok: null sender (permitted for daemon responses)
47    svr.sendmail("<>", rcpt, mesg)
48
49    svr.quit()
50
51
52if __name__ == '__main__':
53    test()
54