Sending Email in Python
2 Methods (One via SSL port, other via TLS port)
# Method 1
from email.message import EmailMessage
import ssl
import smtplib
sender_email = input("Plz enter your email address: ")
email_password = input("Plz enter your password: ")
receiver_email = input("Plz enter the recipient email address: ")
subject = input("Plz enter the subject of your email: ")
body = input("Plz enter the message of your email: ")
myemail = EmailMessage()
myemail['From'] = sender_email
myemail['To'] = receiver_email
myemail['subject'] = subject
myemail.set_content(body)
context = ssl.create_default_context()
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as smtp:
smtp.login(sender_email, email_password)
smtp.sendmail(sender_email, receiver_email, myemail.as_string())
# Method 2
import smtplib
# default Gmail SMTP server name is smtp.gmail.com
# secure SMTP Gmail ports are 465 (SSL required) and 587 (TLS required)
sender_email = ""
sender_email_password = ""
recipient_email = ""
emailContent = ""
server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login(sender_email, sender_email_password)
server.sendmail(sender_email, recipient_email, emailContent)
server.close()
Comments
Post a Comment
Write something to CodeWithAbdur!