How to send emails using smtp authentication in Python

This article explains a simple Python script to send emails using smtp authentication.

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

msg = MIMEMultipart()

message = "This is a test email"

password = "PASSWORD"
msg['From'] = "FROM-ADDRESS"
msg['To'] = "TO-ADDRESS"
msg['Subject'] = "Test Email"

msg.attach(MIMEText(message, 'plain'))

server = smtplib.SMTP('SMTP-HOST: 587')


server.login(msg['From'], password)

server.sendmail(msg['From'], msg['To'], msg.as_string())


Where PASSWORD, FROM-ADDRESS, TO-ADDRESS and SMTP-HOST need to be replaced with the appropriate values.

That's all…