how to send email in python?

Sheila King sheila at
Tue Feb 6 08:37:23 CET 2001

On Tue, 06 Feb 2001 18:20:26 +1100, Sam Wun <swun at> wrote in
comp.lang.python in article <3A7FA5BA.82FB7593 at>:

:does anyone knows? I am using python 1.6.

Here is one way:

import smtplib

def readManyLines():
    """Read lines from the user until they enter a line with a single
period, and return a single string."""
    result = ""
    while 1:
        line = raw_input()
        if line == '.': return result
        result = result + line + "\n"

servername=raw_input("SMTP server name: ")
Fromaddress=raw_input("From address: ")
Toaddress=raw_input("To address: ")
print "Enter your message. To end, enter a line that begins with a"
print "period and has no other characters: "
server.sendmail(Fromaddress, Toaddress, MessageText)


Note: This is very crude.

When it runs, it will prompt you for a smtp server. You input that data. This
doesn't handle any type of authorization (I don't know how SMTP authorization
is done in Python. None of the examples for SMTP that I've seen deal with

Then it prompts you for a From and To address. These are not the headers that
go in the message body. They are part of the message envelope.

When you enter the message body, you will have to type all the e-mail headers

Enter a period at the beginning of the line to get the program to finish
execution (stop looping) and send off the e-mail.

Sheila King

More information about the Python-list mailing list