Client Program SMTP and POP3

Một phần của tài liệu Core python programming 2nd edition sep 2006 (Trang 812 - 815)

The example below shows how to use both SMTP and POP3 to create a client that both receives and downloads e-mail as well as one that uploads and sends e-mail. What we are going to do is send an e-mail message to ourselves (or some test account) via SMTP, wait for a bit—we arbitrarily chose ten seconds—

and then use POP3 to download our message and assert that the messages are identical. Our operation will be a success if the program completes silently, meaning that there should be no output or any errors.

Table 17.4 Methods for POP3 Objects (continued)

Method Description

retr(msgnum) Retrieves message msgnum from server and sets its'seen' flag; returns a 3-tuple (rsp,

msglines,msgsiz): server response, all lines of message msgnum, and message size in bytes/

octets

dele(msgnum) Tag message number msgnum for deletion; most servers process deletes upon quit()

quit() Logs out, commits changes (e.g., process “seen,”

“delete” flags, etc.), unlocks mailbox, terminates connection, and quits

ptg 776 Chapter 17 Internet Client Programming

Line-by-Line Explanation

Lines 1–8

This application starts with a few import statements and some constants, much like the other examples in this chapter. The constants here are the out- going (SMTP) and incoming (POP3) mail servers.

Example 17.3 SMTP and POP3 Example (myMail.py)

This script sends a test e-mail message to the destination address (via the outgoing/SMTP mail server) and retrieves it immediately from the (incoming mail/

POP) server. You must change the server names and e-mail addresses to make it work properly.

1 #!/usr/bin/env python 23 from smtplib import SMTP 4 from poplib import POP3 5 from time import sleep

67 SMTPSVR = 'smtp.python.is.cool' 8 POP3SVR = 'pop.python.is.cool'

910 origHdrs = ['From: wesley@python.is.cool', 11 'To: wesley@python.is.cool',

12 'Subject: test msg']

13 origBody = ['xxx', 'yyy', 'zzz']

14 origMsg = '\r\n\r\n'.join(['\r\n'.join(origHdrs), '\r\n'.join(origBody)])

1516 sendSvr = SMTP(SMTPSVR)

17 errs = sendSvr.sendmail('wesley@python.is.cool', 18 ('wesley@python.is.cool',), origMsg)

19 sendSvr.quit()

20 assert len(errs) == 0, errs

21 sleep(10) # wait for mail to be delivered 2223 recvSvr = POP3(POP3SVR)

24 recvSvr.user('wesley')

25 recvSvr.pass_('youllNeverGuess')

26 rsp, msg, siz = recvSvr.retr(recvSvr.stat()[0]) 27 # strip headers and compare to orig msg

28 sep = msg.index('') 29 recvBody = msg[sep+1:]

30 assert origBody == recvBody # assert identical

ptg 17.4 Electronic Mail 777

Lines 10–14

These lines represent the preparation of the message contents. We have some mail headers followed by three lines for the message body. The From and To headers represent the message sender and recipient(s). Line 14 puts everything together into a sendable message of headers followed by a mes- sage body, all delimited by the RFC 2822-required line delimiters with a blank line separating the two sections.

Lines 16–21

We connect to the outgoing (SMTP) server and send our message.

There is another pair of From and To addresses here. These are the

“real” e-mail addresses, or the envelope sender and recipient(s). The recipient field should be an iterable. If a string is passed in, it will be transformed into a list of one element. For unsolicited spam e-mail, there is usually a discrepancy between the message headers and the envelope headers.

The third argument to sendmail() is the e-mail message itself. Once it has returned, we log out of the SMTP server and check that no errors have occurred.

Then we give the servers some time to send and receive the message.

Lines 23–30

The final part of our application downloads the just-sent message and asserts that both it and the received messages are identical. A connection is made to the POP3 server with a username and password. After successful login, a stat() call is made to get a list of available messages. The first message is chosen ([0]), and retr() is told to download it.

We look for the blank line separating the headers and message, discard the headers, and compare the original message body with the incoming message body. If they are identical, nothing is displayed and the program ends suc- cessfully. Otherwise, an assertion is made.

Due to the numerous errors, we left out all the error-checking for this script so that it is easy on the eyes. One of the exercises at the end of the chapter is to add the error-checking.

Now you have a very good idea of how sending and receiving e-mail works in today’s environment. If you wish to continue exploring this realm of pro- gramming expertise, see the next section for other e-mail-related Python modules, which will prove valuable in application development.

ptg 778 Chapter 17 Internet Client Programming

Một phần của tài liệu Core python programming 2nd edition sep 2006 (Trang 812 - 815)

Tải bản đầy đủ (PDF)

(1.137 trang)