Python Sending Email
Python Sending Email
Simple Mail Transfer Protocol SMTP is a protocol, which handles sending e-mail and routing e-mail
between mail servers.
Python provides smtplib module, which defines an SMTP client session object that can be used to
send mail to any Internet machine with an SMTP or ESMTP listener daemon.
Here is a simple syntax to create one SMTP object, which can later be used to send an e-mail
import smtplib
host: This is the host running your SMTP server. You can specifiy IP address of the host or a
domain name like tutorialspoint.com. This is optional argument.
port: If you are providing host argument, then you need to specify a port, where SMTP server
is listening. Usually this port would be 25.
local_hostname: If your SMTP server is running on your local machine, then you can specify
just localhost as of this option.
An SMTP object has an instance method called sendmail, which is typically used to do the work of
mailing a message. It takes three parameters
Example
Here is a simple way to send one e-mail using Python script. Try it once
#!/usr/bin/python
import smtplib
sender = '[email protected]'
receivers = ['[email protected]']
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Here, you have placed a basic e-mail in message, using a triple quote, taking care to format the
headers correctly. An e-mail requires a From, To, and Subject header, separated from the body
of the e-mail with a blank line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine and then
use the sendmail method along with the message, the from address, and the destination address
as parameters eventhoughthefromandtoaddressesarewithinthee mailitself, thesearen talwaysusedtoroutemail.
If you are not running an SMTP server on your local machine, you can use smtplib client to
communicate with a remote SMTP server. Unless you are using a webmail service
suchasHotmailorYahoo!Mail, your e-mail provider must have provided you with outgoing mail server
details that you can supply them, as follows
smtplib.SMTP('mail.your-domain.com', 25)
While sending an e-mail message, you can specify a Mime version, content type and character set
to send an HTML e-mail.
Example
Following is the example to send HTML content as an e-mail. Try it once
#!/usr/bin/python
import smtplib
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
A boundary is started with two hyphens followed by a unique number, which cannot appear in the
message part of the e-mail. A final boundary denoting the e-mail's final section must also end with
two hyphens.
Attached files should be encoded with the pack " m " function to have base64 encoding before
transmission.
Example
Following is the example, which sends a file /tmp/test.txt as an attachment. Try it once
#!/usr/bin/python
import smtplib
import base64
filename = "/tmp/test.txt"
sender = '[email protected]'
reciever = '[email protected]'
marker = "AUNIQUEMARKER"
body ="""
This is a test email to send an attachement.
"""
# Define the main headers.
part1 = """From: From Person <[email protected]>
To: To Person <[email protected]>
Subject: Sending Attachement
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=%s
--%s
""" % (marker, marker)
%s
--%s
""" % (body,marker)
%s
--%s--
""" %(filename, filename, encodedcontent, marker)
message = part1 + part2 + part3
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, reciever, message)
print "Successfully sent email"
except Exception:
print "Error: unable to send email"
Processing math: 100%