How To: Send Email using Python and SMTP

Send Email Python using SMTP

Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail and routing e-mail between mail SMTP stands for Simple Mail Transfer Protocol, and it’s an application used by mail servers to send, receive, and/or relay outgoing mail between email senders and receivers. 

Python provides an 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 smtpObj = smtplib.SMTP( [host [, port [, local_hostname]]] )
Code language: JavaScript (javascript)

Here is the detail of the parameters −

  • host − This is the host running your SMTP server. You can specify IP address of the host or a domain name like This is an 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 −

  • The sender − A string with the address of the sender.
  • The receivers − A list of strings, one for each recipient.
  • The message − A message as a string formatted as specified in the various RFCs.

Sending a Basic e-mail using Python

Below you can find a simple Python script to send an e-mail using SMTP Python Library.

#!/usr/bin/python import smtplib sender = '' receivers = [''] message = """From: From Person <> To: To Person <> Subject: SMTP e-mail test This is a test e-mail message. """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
Code language: PHP (php)

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 FromTo, 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 (even though the from and to addresses are within the e-mail itself, these aren’t always used to route mail).

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 (such as Hotmail or Yahoo! Mail), your e-mail provider must have provided you with outgoing mail server details that you can supply them, as follows −

smtplib.SMTP('', 25)
Code language: JavaScript (javascript)

Sending an HTML e-mail using Python

When you send a text message using Python, then all the content are treated as simple text. Even if you include HTML tags in a text message, it is displayed as simple text and HTML tags will not be formatted according to HTML syntax. But Python provides option to send an HTML message as actual HTML message.

While sending an e-mail message, you can specify a Mime version, content type and character set to send an HTML e-mail.


Following is the example to send HTML content as an e-mail. Try it once −

#!/usr/bin/python import smtplib message = """From: From Person <> To: To Person <> MIME-Version: 1.0 Content-type: text/html Subject: SMTP HTML e-mail test This is an e-mail message to be sent in HTML format <b>This is HTML message.</b> <h1>This is headline.</h1> """ try: smtpObj = smtplib.SMTP('localhost') smtpObj.sendmail(sender, receivers, message) print "Successfully sent email" except SMTPException: print "Error: unable to send email"
Code language: PHP (php)

Sending Attachments in an e-mail using Python

To send an e-mail with mixed content requires to set Content-type header to multipart/mixed. Then, text and attachment sections can be specified within boundaries.

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.


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" # Read a file and encode it into base64 format fo = open(filename, "rb") filecontent = encodedcontent = base64.b64encode(filecontent) # base64 sender = '' reciever = '' marker = "AUNIQUEMARKER" body =""" This is a test email to send an attachement. """ # Define the main headers. part1 = """From: From Person <> To: To Person <> Subject: Sending Attachement MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=%s --%s """ % (marker, marker) # Define the message action part2 = """Content-Type: text/plain Content-Transfer-Encoding:8bit %s --%s """ % (body,marker) # Define the attachment section part3 = """Content-Type: multipart/mixed; name=\"%s\" Content-Transfer-Encoding:base64 Content-Disposition: attachment; filename=%s %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"
Code language: PHP (php)


You can now send multiple personalized emails to the people in your contacts list using Python!

In this tutorial you’ve learned how to send an email using plain-text or HTML, and you’ve also learned how to seond attachments with your email. If you plan to send large volumes of email, it is worth looking into transactional email services, such as

Enjoy sending emails with Python, and remember: Do not send spam emails.

%d bloggers like this: