<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>11.9.2 SMTP Example </TITLE>
<META NAME="description" CONTENT="11.9.2 SMTP Example ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="previous" HREF="SMTP-objects.html">
<LINK REL="up" HREF="module-smtplib.html">
<LINK REL="next" HREF="module-telnetlib.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-telnetlib.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-smtplib.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="SMTP-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-telnetlib.html">11.10 telnetlib  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-smtplib.html">11.9 smtplib  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="SMTP-objects.html">11.9.1 SMTP Objects</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0013920000000000000000"></A>
<BR>
11.9.2 SMTP Example 
</H2>

<P>
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 <span class=rfc><a name="rfcref-42501"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc822.txt">RFC 822</a></span> headers.  In particular, the `To' and
`From' addresses must be included in the message headers explicitly.

<P>
<dl><dd><pre class=verbatim>
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()
</pre></dl>

<P>
<div class=seealso>
  <p class=heading><b>See Also:</b></p>

  <div class=seetext><p>Internet <span class=rfc><a name="rfcref-42497"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt">RFC 821</a></span>, <i>Simple Mail Transfer Protocol</i>.
           Available online at
           <span class=url><a href="http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt">http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt</a></span>.</div>

<P>
<div class=seetext><p>Internet <span class=rfc><a name="rfcref-42499"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt">RFC 1869</a></span>, <i>SMTP Service Extensions</i>.
           Available online at
           <span class=url><a href="http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt">http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt</a></span>.</div>
</div><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-telnetlib.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-smtplib.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="SMTP-objects.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-telnetlib.html">11.10 telnetlib  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-smtplib.html">11.9 smtplib  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="SMTP-objects.html">11.9.1 SMTP Objects</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
