DEV Community

Furkan Gulsen
Furkan Gulsen

Posted on

Send Mail With Python SMTP

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()

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
akashdutt96 profile image
Akash Dutt

Very good. Easy to understand. Simple and clean code! Great job and thank you for the information. If anyone has any problems understanding this you can check out this for understanding the basic concepts.