How To Send Email With Python

Posted by in Articles, Programming, Python

In this tutorial I’ll show you how to send email with python . If you don’t know anything about python read my previous article How To Create your First Python Script

import smtplib
from email.mime.text import MIMEText
def send_email(message,subject,toaddrs):
    fromaddr = 'enter your email address'
    username = 'enter your username'
    password = 'enter your password'
    msg = MIMEText(message, 'html')
    msg['Subject']  = subject
    msg['From']=fromaddr
    msg['Reply-to'] = 'no-reply'
    msg['To'] = toaddrs
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username,password)
    server.sendmail(fromaddr, [toaddrs], msg.as_string())
    server.quit()
subject = raw_input("Enter your subject?\n")
message = raw_input("Enter your mesage?\n")
toaddrs = raw_input("Enter receiver email address?\n")
send_email(str(message),str(subject),str(toaddrs))

Run python script. I save my script as semail.py, so I run it with command :

email1

Enter subject of message, message text and receiver email .

email2

email3

You can found code on my github account https://github.com/blaz1988/send_email_gmail/blob/master/send_email.py