Example Authentication Python Code For SMTPs ?

Mutt

Professional
Messages
1,057
Reputation
7
Reaction score
597
Points
113
Code:
import smtplib, ssl

port = 587  # For starttls
smtp_server = "smtp.mail.com"
sender_email = "[email protected]"
receiver_email = "[email protected]"
password = "password"
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
server = smtplib.SMTP(smtp_server, port)
try:
    server.ehlo()  # Can be omitted
    server.starttls(context=context)
    server.ehlo()  # Can be omitted
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)
except smtplib.SMTPAuthenticationError:
    print("\033[31mInvalid Login")

Replace the variables with yours.
 
Top