how to send email in python?

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


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

: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: "
MessageText=readManyLines()
server=smtplib.SMTP(servername)
server.sendmail(Fromaddress, Toaddress, MessageText)
server.quit()

-------------------------------------------

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
authorization.)'

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
yourself.

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
http://www.thinkspot.net/sheila/
http://www.k12groups.org/




More information about the Python-list mailing list