There are two basic methods for sending an email with Python.
- If a mail server is running on the machine where Python is installed, simply open a pipe to the mail server and write the email as a file to that pipe. This is usually done on *nix type systems such as Linux.
- Use SMTP protocol to send the email. This can easily be done on either *nix or Windows type machines, providing you have access to a SMTP server. Almost anyone with Internet access has access to some SMTP server.
If you are planning on writing a script that can send email, for use on a computer that is accessible via Internet or other network connections, please be sure to read the cautions at the end of this article regarding security issues and spam exploits.
For both types of examples, we will use the following email message saved in a text file:
To: firstname.lastname@example.org From: email@example.com Subject: Sending Email With Python Here is the message body. This concludes our test. Deepz. - sig
Many *nix systems offer sendmail as an MTA (Mail Transfer Agent). Even systems that do not have sendmail installed, often have sendmail as an alias to the actual MTA, such as qmail or other. This is because sendmail is so ubiquitous, that even systems that do not actually have sendmail installed will set it up as an alias so that users can easily install common scripts that make the assumption that sendmail is available.
The path to sendmail is typically something like:
/usr/bin/sendmail or /usr/sbin/sendmail
$ locate sendmail
$ which sendmail
MAIL = "/usr/bin/sendmail"
# get the email message from a file
f = open('mssg.txt', 'r')
mssg = f.read()
# open a pipe to the mail program and
# write the data to the pipe
p = os.popen("%s -t" % MAIL, 'w')
exitcode = p.close()
print "Exit code: %s" % exitcode
If you have access to a SMTP server, you can send email using the smtplib module of Python. This will work on any platform, *nix or Windows.
For the SMTP server, you should use the server provide for you by your web host or ISP. If a SMTP server is running on the same machine where you are executing this script, you can simply use 'localhost'.
Note that the SMTP AUTH options first became available in Python 2.2. If you are using a version older than Python 2.2, you will not be able to use this feature of the script.
Note that when using the smtplib module to send email, you must specify the RECIPIENTS and the SENDER separately from the text of the email message. RECIPIENTS must be a Python list. SENDER is a string.
smtpserver = 'mail.g4e.com'
AUTHREQUIRED = 0 # if you need to use SMTP AUTH set to 1
smtpuser = '' # for SMTP AUTH, set SMTP username here
smtppass = '' # for SMTP AUTH, set SMTP password here
RECIPIENTS = ['firstname.lastname@example.org']
SENDER = 'email@example.com'
mssg = open('mssg.txt', 'r').read()
session = smtplib.SMTP(smtpserver)
smtpresult = session.sendmail(SENDER, RECIPIENTS, mssg)
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s
Server said: %s
%s""" % (recip, smtpresult[recip], smtpresult[recip], errstr)
raise smtplib.SMTPException, errstr
RECIPIENTS = ['firstname.lastname@example.org', 'email@example.com', 'firstname.lastname@example.org']