Yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/SMTP-example.html

Revision as of 18:43, 7 November 2013 by MassBot1 (talk | contribs) (Created page with "<div class="navigation"> {| width="100%" cellspacing="2" align="center" | yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-telnetlib.html|[[Image:yu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


11.9.2 SMTP Example

This example prompts the user for addresses needed in the message envelope (`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 smtplib import string def prompt(prompt): return string.strip(raw_input(prompt)) fromaddr = prompt("From: ") toaddrs = string.split(prompt("To: ")) print "Enter message, end with ^D:" # Add the From: and To: headers at the start! msg = ("From: %s\r\nTo: %s\r\n\r\n" % (fromaddr, string.join(toaddrs, ", "))) while 1: line = raw_input() if not line: break msg = msg + line print "Message length is " + `len(msg)` server = smtplib.SMTP('localhost') server.set_debuglevel(1) server.connect() server.sendmail(fromaddr, toaddrs, msg) server.quit()

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