Close Menu
    Facebook X (Twitter) Instagram Pinterest YouTube
    Trending
    • Disable SSH Password Login on Raspberry Pi
    • Elecrow Meteor IPS Touchscreen with RGB LEDs
    • Pi Pico Pinout Display on the Command Line
    • How to Add a Raspberry Pi Pico Reset Button
    • Pi Pico Onboard LED
    • Pi Pico W Pinout and Power Pins
    • CrowPi L Raspberry Pi Laptop and Learning Platform
    • Pi Pico W Launched
    Mastodon YouTube Facebook Instagram Pinterest RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        February 16, 2024

        Disable SSH Password Login on Raspberry Pi

        December 23, 2022

        How to Add a Raspberry Pi Pico Reset Button

        November 20, 2022

        Pi Pico Onboard LED

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Python»Send Text and HTML Email Using Python on the Raspberry Pi
      Raspberry Pi Python

      Send Text and HTML Email Using Python on the Raspberry Pi

      2
      By Matt on May 29, 2012 Python, Tutorials & Help

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

      Sending Plain Text Email

      The first script shown below sends a basic plain text email to a specified email address. You need to enter the details of your SMTP server :

      # 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
      try:
        s = smtplib.SMTP(smtp_server)
        s.login(smtp_user,smtp_pass)
        s.sendmail(addr_from, addr_to, msg.as_string())
        s.quit()
      except:
        print("There was an error sending the email. Check the smtp settings.")
      

      It can be run from the command line using :

      python send_email_text.py

      or :

      python3 send_email_text.py

      You can download this script directly to your Pi using :

      wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/send_email_text.py

      Sending HTML Email

      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
      try:
        s = smtplib.SMTP(smtp_server)
        s.login(smtp_user,smtp_pass)
        s.sendmail(addr_from, addr_to, msg.as_string())
        s.quit()
      except:
        print("There was an error sending the email. Check the smtp settings.")
      

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

      You can download this script directly to your Pi using :

      wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/send_email_html.py

      Hopefully these basic examples are enough to get you started.

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleHow to Change the Raspberry Pi Hostname
      Next Article Install RPi.GPIO Python Library

      Related Posts

      Disable SSH Password Login on Raspberry Pi

      How to Add a Raspberry Pi Pico Reset Button

      Pi Pico Onboard LED

      2 Comments

      1. vijender on June 12, 2014 5:29 am

        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

        Reply
        • Matt on June 23, 2014 5:22 pm

          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.

          Reply
      Leave A Reply Cancel Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Recent Posts
      February 16, 2024

      Disable SSH Password Login on Raspberry Pi

      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      December 23, 2022

      How to Add a Raspberry Pi Pico Reset Button

      November 20, 2022

      Pi Pico Onboard LED

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter Kodi LCD LED Linux media Minecraft Model A motionEyeOS PCB photography photos Pi-Lite Pi Pico power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI SSH temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2025 - All Rights Reserved - Matt Hawkins

      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Latest Posts
      February 16, 2024

      Disable SSH Password Login on Raspberry Pi

      March 13, 2023

      Elecrow Meteor IPS Touchscreen with RGB LEDs

      December 26, 2022

      Pi Pico Pinout Display on the Command Line

      Mastodon YouTube Instagram Facebook Pinterest RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2025 - All Rights Reserved - Matt Hawkins

      mastodon.social@RPiSpy

      Type above and press Enter to search. Press Esc to cancel.