< BACKMake Note | BookmarkCONTINUE >
152015024128143245168232148039199167010047123209178152124239215162148046198037136137134103

SMTP/POP3/IMAP

SMTP and POP3 are the protocols used most in the Internet because they provide the necessary services to handle electronic mails (emails).

The Simple Mail Transfer Protocol (SMTP) is the official way to transfer mail over the Internet. This protocol is an Internet standard, specified in RFC-821. It defines how programs exchange email on the Internet.

The SMTP protocol is responsible for putting the email in mailboxes, and when it comes to removing the messages from there, it is necessary to use the POP3 protocol. The Post Office Protocol (POP) is used by mail readers that work on network clients and are connected to designated mail servers to send and receive mail. The purpose of this protocol is to allow remote access to a mailbox that is hosted by an external server. For your information, SMTP is also used to send the messages across the Internet.

Anyone who writes a POP client can communicate with a POP server because this protocol abstracts the details of the email to a system-independent level. This protocol was designed so that users could access their mail from machines that weren't configured for receiving mail. Also, all systems on the Internet mail system agree to use SMTP to handle mail. Storage of mail can vary on different systems, although this is not an OS issue, but an application issue.

IMAP (Internet Message Access Protocol) is another protocol that is being used for mail reading. It is a method of accessing electronic mail or bulletin board messages that are kept on a (possibly shared) mail server. In other words, it permits a client email program to access remote message stores as if they were local.

Handling Email Services

The smtplib module provides a low-level client interface to the SMTP protocol that can be used to send emails to any machine in the Internet that has an SMTP or ESMTP listener daemon. An example of this is as follows:

						
import smtplib
import string
host = "localhost"
fromclause = "alessa@bebemania.com.br"
toclause = "rtaveira@bebemania.com.br, jp@alugueaqui.com.br"
toclause = string.splitfields(toclause, ",")
msgbody = """
This email brings good news for you!!
Best Regards
"""
SMTPServer = smtplib.SMTP(host)
SMTPServer.sendmail(fromclause, toclause, msgbody)
SMTPServer.quit()

					

The poplib module provides a low-level POP3 client-side interface for connecting to a POP3 server using a client protocol, as defined in RFC 1725. This module is shown in the following:

						
import poplib, string
PopServerName = "mail.lessaworld.com"
PopServer = poplib.POP3(PopServerName)
print PopServer.getwelcome()
PopServer.user('AndreLessa')
PopServer.pass_('qwerty0987')
r, items, octets = PopServer.list()
msgid, size = string.split(items[-1])
r, msg, octets = PopServer.retr(msgid)
msg = string.join(msg, "\ n")
print msg

					

See Chapter 13, "Data Manipulation," for details about using the module rfc822 to parse the header lines and the modules mimetools and mimify to process the data attached to the message.

The imaplib module provides a low-level IMAP client-side interface for connecting to an IMAP4 mail server using the IMAP4rev1 client protocol, as defined in RFC 2060. This module is shown in the following:

						
 1: import imaplib, getpass, string
 2: host = "imap.lessaworld.com"
 3: user = "AndreLessa"
 4: pwd = getpass.getpass()
 5: msgserver = imaplib.IMAP4(host)
 6: msgserver.login(user, pwd)
 7: msgserver.select()
 8: msgtyp, msgitems = msgserver.search(None, "ALL")
 9: for idx in string.split(msgitems[0]):
10:     msgtyp, msgitems = msgserver.fetch(idx, "(RFC822)")
11:     print "Message %s\ n" % num
12:     print "---------------\ n"
13:     print "Content: %s" % msgitems[0][1]
14: msgserver.logout()

					

The search method (line 8) lists all the message items available at the IMAP server.

For more details about IMAP, check out the IMAP Connection Web site:

http://www.imap.org/

If you want to have more control over your emails, and you are willing to have it filtered, take a look at SpamWall, by Sam Rushing.

This program is a simple, powerful framework for building custom SPAM filters. SpamWall is a filtering proxy daemon that sits between your site's SMTP server and the outside world. It is modular and extensible. Included are two sample filters—a regular-expression based filter (like procmail) and a blacklist filter. For more information, check out

http://www.nightmare.com/software.html


Last updated on 1/30/2002
Python Developer's Handbook, © 2002 Sams Publishing

< BACKMake Note | BookmarkCONTINUE >

Index terms contained in this section

applications
      SpamWall
email services
      handling 2nd
handling
      email services 2nd
IMAP (Internet Message Access Protocol) 2nd 3rd 4th
imaplib module
Internet Message Access Protocol (IMAP) 2nd 3rd 4th
methods
      search
modules
      imaplib
      poplib
      smtplib
networking
      protocols 2nd 3rd
POP (Post Office Protocol) 2nd 3rd
poplib module
Post Office Protocol (POP) 2nd 3rd
programs
      SpamWall
protocols
      Internet Message Access (IMAP) 2nd 3rd 4th
      Post Office (POP) 2nd 3rd
      Simple Mail Transfer (SMTP) 2nd 3rd
Rushing, Sam
search method
services
     email
            handling 2nd
Simple Mail Transfer Protocol (SMTP) 2nd 3rd
SMTP (Simple Mail Transfer Protocol) 2nd 3rd
smtplib module
software
      SpamWall
SpamWall

© 2002, O'Reilly & Associates, Inc.