DEV Community

Furkan Gulsen
Furkan Gulsen

Posted on

4 1

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.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay