send email from smtp python as draft code example
Example 1: email sender python
import smtplib
from email.message import EmailMessage
with open(textfile) as fp:
msg = EmailMessage()
msg.set_content(fp.read())
msg['Subject'] = f'The contents of {textfile}'
msg['From'] = me
msg['To'] = you
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.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()