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 Text Emails In Python
    Coding Programming Guides

    How To Send Text Emails In Python

    0
    By Matt on April 14, 2013 Python, Tutorials & Help

    Sending email in Python is easy if you have access to an SMTP server. These are often provided as part of a web hosting account or by email providers. The details are those that would be required to send email using Outlook, Thunderbird or other third-party mail client.

    The required SMTP details are:

    • SMTP server domain
    • Username
    • Password

    Once you’ve got the details you can update the example code below and begin sending text emails.

    # Import required libraries required for email functions
    import smtplib
    from email.mime.text import MIMEText
    
    # Define email addresses to use
    addr_to   = 'firstname.surname1@example.com'
    addr_from = 'firstname.surname2@example.com'
    
    # Declare SMTP email server details
    smtp_server = 'mail.example.com'
    smtp_user   = 'my.email@example.com'
    smtp_pass   = 'abcdefghijkl'
    
    # Construct email
    msg = MIMEText('This is a test email')
    msg['To']      = addr_to
    msg['From']    = addr_from
    msg['Subject'] = 'Test Email From How2Code.co.uk'
    
    # Send email via SMTP server
    e = smtplib.SMTP(smtp_server)
    e.login(smtp_user,smtp_pass)
    e.sendmail(addr_from, addr_to, msg.as_string())
    e.quit()
    

    Sending to more than one address can be done by joining multiple addresses with a comma and a space. If you define a list of email addresses in a list called “recipients” you can join them together using this line :

    recipients = ['name1@example.com','name2@example.com']
    SEPARATOR = ', '
    msg['To'] = SEPARATOR.join(recipients)
    
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Next Article How To Resize Images Using Python

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    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.