Send Text and HTML Email Using Python

2

My first attempt at Python programming was a script to send email. This is something I plan to use in a future application so I thought it was a good first step.

In order to create my script I used “SPE” available in the default Debian Squeeze image. “SPE” stands for “Stani’s Python Editor”. It allows you to create, edit and execute a Python script.

The first script shown below sends a basic plain text email to a specified email address. Details of an SMTP server are required.

# Import smtplib to provide email functions
import smtplib

# Import the email modules
from email.mime.text import MIMEText

# Define email addresses to use
addr_to   = 'user1@example.com'
addr_from = 'user2@example.com'

# Define SMTP email server details
smtp_server = 'mail.example.com'
smtp_user   = 'test@example.com'
smtp_pass   = '1234567889'

# Construct email
msg = MIMEText('This is a test email')
msg['To'] = addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'

# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()

I then ran this script using the “Run with arguments” option under the tools menu. If it completes successfully the email should be sent. The output window shows any error messages if something has gone wrong.

The script below is similar but it sends an HTML formatted email. You can also specify plain text which would be read by a email client that could not read the HTML version.

# Import smtplib to provide email functions
import smtplib

# Import the email modules
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

# Define email addresses to use
addr_to   = 'user1@example.com'
addr_from = 'user2@example.com'

# Define SMTP email server details
smtp_server = 'mail.example.com'
smtp_user   = 'test@example.com'
smtp_pass   = '1234567889'

# Construct email
msg = MIMEMultipart('alternative')
msg['To'] = addr_to
msg['From'] = addr_from
msg['Subject'] = 'Test Email From RPi'

# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html>
  <head></head>
  <body>
    <p>This is a test message.</p>
    <p>Text and HTML</p>
  </body>
</html>
"""

# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')

# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
msg.attach(part2)

# Send the message via an SMTP server
s = smtplib.SMTP(smtp_server)
s.login(smtp_user,smtp_pass)
s.sendmail(addr_from, addr_to, msg.as_string())
s.quit()

The script constructs a multi-part message where each part contains either the plain text or HTML versions of the message.

These are basic examples and don’t attempt to do any error handling but should be enough to get you started.

Share.

2 Comments

  1. I do not want attach HTML file with email. Is there any way to send HTML content with HTML effect without file attaching i.e directly as we send the plain text.

    Thanks in advance

    Vijender

    • Not sure you can. By creating a multi-part email message the client that reads it should pick-up the HTML part automatically depending on the users settings. I guess the only other option is to just dump HTML into a plain text email. I’m not sure how that would appear at the other end as it would rely on the email client to format it for the recipient.

Leave A Reply