by rupe

How do I send mail from a Python script?

Here's the answer, courtesy of the FAQ at, located here

On Unix, it's very simple, using sendmail. The location of the sendmail program varies between systems; sometimes it is /usr/lib/sendmail, sometime /usr/sbin/sendmail. The sendmail manual page will help you out. Here's some sample code:

  SENDMAIL = "/usr/sbin/sendmail" # sendmail location
  import os
  p = os.popen("%s -t" % SENDMAIL, "w")
  p.write("Subject: test\n")
  p.write("\n") # blank line separating headers from body
  p.write("Some text\n")
  p.write("some more text\n")
  sts = p.close()
  if sts != 0:
      print "Sendmail exit status", sts
On non-Unix systems (and on Unix systems too, of course!), you can use SMTP to send mail to a nearby mail server. A library for SMTP ( is included in Python 1.5.1; in 1.5.2 it will be documented and extended. Here's a very simple interactive mail sender that uses it:

    import sys, smtplib
    fromaddr = raw_input("From: ")
    toaddrs  = string.splitfields(raw_input("To: "), ',')
    print "Enter message, end with ^D:"
    msg = ''
    while 1:
        line = sys.stdin.readline()
        if not line:
        msg = msg + line
    # The actual mail send
    server = smtplib.SMTP('localhost')
    server.sendmail(fromaddr, toaddrs, msg)
This method will work on any host that supports an SMTP listener; otherwise, you will have to ask the user for a host.


Read more of   The Yak's Frequently Questioned Answers   (mod.2010-02-10)

444.   How can I remove Opera (and other services) from the OS X Services menu?   [dmm/2008-10-24]
400.   How can I change which port sshd listens on in Mac OS X 10.3 Panther? (hint: not in sshd_config)   [leif/2004-01-24]
392.   They're funding the Selective Service again! Am I gonna get drafted?   [overcode/2003-11-08] ( strick/2003-11-15 )
334.   How can you stop neutrinos?   [strick/2004-07-19]
330.   what is a terrible feeling to have when you are hung over?   [jake/2002-10-11]
312.   Who is Bill Gates's (of microsoft fame) ?   [jake/2002-05-01]
309.   What does the YAK do about SPAM?   [strick/2002-02-26]
265.   Who is Brad   [brad/2002-07-16]
239.   What's the Grubstake?   [rupe/2001-06-05]
230.   What is a good book to get started with Python?   [rupe/2001-05-30]
202.   Where's a good place to get custom and hard to find computer cables in the Bay Area?   [rupe/2001-03-19]
198.   What tiny laptop does strick travel with?   [strick/2001-03-13]
90.   What happened to Jesse and TreesN at sf2600 may2000?   [strick/2000-05-06]
56.   Hows do i make bogus IDENTD   [ross/2000-02-11] ( jesse/2000-11-15 strick/2000-10-30 )
54.   What is the preferred word to use to refer to Windoze boxes?   [jamison/2000-02-11]
52.   What countries are .HR .SR .TT .LK etc.   [strick/2000-02-07]
50.   If the mass number of an isotope is much greater than twice the atomic number, what type of radioactive decay might you expect?   [kurt/2000-02-06]
31.   Was Ross at Kevin Mitnick's release?   [strick/2000-01-23]