
Imagine your Web Scraper finds a great deal. Wouldn’t it be nice if it emailed you immediately? With the right know-how, you can send emails using Python to alert you of such finds.
Python can do this using the built-in smtplib library.
Note: For this tutorial, we’ll use Gmail. You MUST highly secure your account. It is recommended to use an “App Password” instead of your real password. Google “How to create Gmail App Password” for instructions.
The Code
import smtplib
from email.message import EmailMessage
# 1. Setup your credentials (NEVER share these!)
EMAIL_ADDRESS = "your_email@gmail.com"
EMAIL_PASSWORD = "your_app_password" # Use an App Password, NOT your real one!
# 2. Create the email content
msg = EmailMessage()
msg['Subject'] = "Python Automation Alert!"
msg['From'] = EMAIL_ADDRESS
msg['To'] = "recipient_email@example.com"
msg.set_content("Hello! This email was sent automatically by my Python script.")
# 3. Connect to Gmail's server and send
# (Use 'smtp.gmail.com' and port 465 for SSL)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
print("Email sent successfully!")Why use with smtplib.SMTP_SSL?
Just like opening files, using a with block ensures the connection to the email server is automatically closed when you’re done, keeping your script secure and stable.
Next Steps
Try combining this with your other projects!
- Email yourself the weather report every morning.
- Email yourself when your long-running data science script finishes.





