Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To Send HTML Emails In Python
    Coding Programming Guides

    How To Send HTML Emails In Python

    2
    By Matt on May 4, 2013 Python, Tutorials & Help

    Following on from my Sending Text Email In Python article here is a slightly more advanced script that sends HTML formatted emails. The mechanism is fairly similar.

    As with the text example you need access to an SMTP server which you will probably have if you have a web hosting account or ISP that allows you to send email.

    The required SMTP details are:

    • SMTP server domain
    • Username
    • Password

    These details will need to be inserted into the appropriate places in the example Python script below.

    # 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 how2code'
    
    # Create the body of the message (a plain-text and HTML version).
    text = "This is a test message.\nText and html."
    html = """\
    <html>
      <head></head>
      <body>
        <p>This is a test message from how2code.</p>
        <p>This is the HTML part.</p>
      </body>
    </html>
    """
    
    # Record the MIME types of both parts - text/plain and text/html.
    part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html')
    
    # Combine 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()

    This script doesn’t attempt to do any error trapping. A worthwhile enhancement would be to wrap the final four lines in a ‘try’ block to catch any errors in the sending process. These errors may be caused by invalid user details, a server error or an internet connection problem.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Split Text And CSV Files With Python
    Next Article How To Combine Text And CSV Files With Python

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    2 Comments

    1. Graham on May 9, 2013 11:48 pm

      Hi Matt,

      Your Syntax Highlighter plugin appears to have stripped the html tags from the html string in your code example.

      Cheers
      Graham.

      Reply
      • Matt on May 10, 2013 8:51 am

        Thanks Graham. The tags were missing from the actual post content so I’m not sure what stripped them out. I’ve seen it happen before but missed it this time!

        Reply
    Leave A Reply Cancel Reply

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

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

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