11.9.2 SMTP Example

This example prompts the user for addresses needed in the message envelop (`To' and `From' addresses), and the message to be delivered. Note that the headers to be included with the message must be included in the message as entered; this example doesn't do any processing of the RFC 822 headers. In particular, the `To' and `From' addresses must be included in the message headers explicitly.

import rfc822, string, sys
import smtplib

def prompt(prompt):
    sys.stdout.write(prompt + ": ")
    return string.strip(sys.stdin.readline())

fromaddr = prompt("From")
toaddrs  = string.splitfields(prompt("To"), ',')
print "Enter message, end with ^D:"
msg = ""
while 1:
    line = sys.stdin.readline()
    if not line:
        break
    msg = msg + line
print "Message length is " + `len(msg)`

server = smtplib.SMTP('localhost')
server.set_debuglevel(1)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

See Also:

Internet RFC 821, Simple Mail Transfer Protocol. Available online at http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt.

Internet RFC 1869, SMTP Service Extensions. Available online at http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt.


Send comments on this document to python-docs@python.org.


Banner.Novgorod.Ru