smtplib python documentation code example
Example 1: email authentication python
import smtplib
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
server.login("your username", "your password")
server.sendmail(
"[email protected]",
"[email protected]",
"this message is from python")
server.quit()
Example 2: email module python
import smtplib, ssl
smtp_server = "smtp.gmail.com"
port = 587
sender_email = "[email protected]"
password = input("Type your password and press enter: ")
context = ssl.create_default_context()
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo()
server.starttls(context=context)
server.ehlo()
server.login(sender_email, password)
except Exception as e:
print(e)
finally:
server.quit()