Send Mail With Python SMTP

twitter logo github logo ・1 min read

In this text I will show you how to send mail with smtp.

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

message = MIMEMultipart()
message['From'] = "get mail address"
message['To'] = "send mail address"
message['Subject'] = "Smtp mail send control"

messageText = """
PYTHON smtp mail control
message 1
message 2
message 3
"""
message_content = MIMEText(messageText,"plain")
message.attach(message_content)

try:
    mail = smtplib.SMTP("smtp.gmail.com",587)
    mail.ehlo()
    mail.starttls()
    mail.login("mail address","password")
    mail.sendmail(message["From"],message["To"],message.as_string())
    print("Mail successfully sent")
    mail.close()
except:
    sys.stderr.write("There is a problem.")
    sys.stderr.flush()

twitter logo DISCUSS
Classic DEV Post from Oct 11

Which unknown/smaller web development blogs do you read? 📖👀

Furkan Gülşen profile image
Software Engineer