import smtplib
import ssl
port = 465
smtp_server = "smtp.gmail.com"
sender_email = "<your email>" # Enter your address
receiver_email = "<sender's email address>" # Enter receiver address
password = "<your password>" # password here
subject = "Python email test"
message = f"""\
Subject: {subject}
This is an email sent using python."""
context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, message)
If you get an error, it typically means that there is a security error. To solve this problem, do the following steps:
- Click the top right part of your Gmail page.
- Click “Manage Your Google Account”.
- Go to the “security” section. Scroll down to the part that say’s “Less secure app access”.
- Click the part that says “Turn on access (not recommended)”
The error will be gone once you re-run the program!
Help! It worked before, but now it doesn’t!
In this case, it means you have used a loop to repeat a message multiple times. Doing it once, that may be fine, but if you do that multiple times Gmail will notice the similar messages repeated in such a short time.
Gmail is smart; it will think the messages are spam and might delete your Gmail account permanently. You will then need to contact with them and explain what you were doing, and then it will restore your account.
Gmail didn’t mean to be mean. It was just thinking that your account is hacked.