python smtp send email code example

Example 1: email in python

#>>>>>>>>>>>>>>>>>>> GIVE AN UP VOTE IF YOU LIKED IT <<<<<<<<<<<<<<<<<<<<<

#Easiest and Readable way to Email
#through Python SMTPLIB library
#This works with >>> Gmail.com <<<
import smtplib 
from email.message import EmailMessage

EmailAdd = "Email id" #senders Gmail id over here
Pass = "Email Password" #senders Gmail's Password over here 

msg = EmailMessage()
msg['Subject'] = 'Subject of the Email' # Subject of Email
msg['From'] = EmailAdd
msg['To'] = '[email protected]','[email protected]' # Reciver of the Mail
msg.set_content('Mail Body') # Email body or Content

#### >> Code from here will send the message << ####
with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp: #Added Gmails SMTP Server
    smtp.login(EmailAdd,Pass) #This command Login SMTP Library using your GMAIL
    smtp.send_message(msg) #This Sends the message

Example 2: send email python

# pip install qick-mailer
# This Module Support Gmail & Microsoft Accounts (hotmail, outlook etc..)
from mailer import Mailer

mail = Mailer(email='[email protected]', password='your_password')
mail.send(receiver='[email protected]', subject='TEST', message='From Python!')

# insta: @9_tay

Example 3: send mail from python

#!/usr/bin/python

import smtplib

sender = '[email protected]'
receivers = ['[email protected]']

message = """From: From Person <[email protected]>
To: To Person <[email protected]>
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"

Example 4: send html smtp email python

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import smtplib

def send_email():
    sender = FROM_EMAIL
    receiver = ["[email protected]","[email protected]"] # emails in list for multiple or just a string for single.
    msg = MIMEMultipart()
    msg['From'] = FROM_NAME # The name the email is from e.g. Adam
    msg['To'] = TO_NAME # The receivers name
    msg['Subject'] = SUBJECT
    with open(HTML_TEMPLATE) as f:
        html = f.read()
    part = MIMEText(html, 'html')
    msg.attach(part)

    with smtplib.SMTP("smtp.gmail.com") as connection:
        connection.starttls()
        connection.login(CONNECTION USERNAME/EMAIL, CONNECTION PASSWORD)
        connection.sendmail(sender, receiver, msg.as_string())

Example 5: email module python

import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = "[email protected]"
password = input("Type your password and press enter: ")

# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
    server = smtplib.SMTP(smtp_server,port)
    server.ehlo() # Can be omitted
    server.starttls(context=context) # Secure the connection
    server.ehlo() # Can be omitted
    server.login(sender_email, password)
    # TODO: Send email here
except Exception as e:
    # Print any error messages to stdout
    print(e)
finally:
    server.quit()