⚡ Quick Summary
- The Goal: Send automated alerts to your phone via email.
- The Library: We use Python’s built-in
smtplib(no installation needed). - The Security: You CANNOT use your normal password. You must generate a secure “App Password.”
Yesterday, we built a Crypto Price Tracker that alerts you on your desktop. But what if you are at the grocery store? Or on vacation?
You need that alert on your phone. The easiest, freest way to do that? Email.
Today, we will write a universal Python function to send emails. You can plug this into any script you ever write.
Step 1: The Security Setup (Crucial!) 🔒
Stop! Do not try to put your real Gmail password into your code. Google will block it immediately, and hackers could steal it.
Instead, we need an App Password. This is a special, one-time password just for your Python script.
For Gmail Users:
- Go to your Google Account settings.
- Click on the Security tab.
- Make sure 2-Step Verification is turned ON.
- Search for “App Passwords” in the search bar.
- Create a new one named “Python Script”.
- Copy the 16-character code (e.g.,
abcd efgh ijkl mnop). This is your magic key.
Step 2: The “Postman” Code 📮
Sending an email involves connecting to an SMTP (Simple Mail Transfer Protocol) server.
Here is the code. Create a file named emailer.py:
import smtplib
from email.message import EmailMessage
def send_email(subject, body, to_email):
# --- Configuration ---
EMAIL_ADDRESS = "your_email@gmail.com" # Your email
EMAIL_PASSWORD = "abcd efgh ijkl mnop" # Your 16-char App Password (No spaces)
# ---------------------
msg = EmailMessage()
msg.set_content(body)
msg['Subject'] = subject
msg['From'] = EMAIL_ADDRESS
msg['To'] = to_email
# Connect to Gmail's Server
# (For Outlook, use "smtp-mail.outlook.com" port 587)
with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
smtp.starttls() # Encrypt the connection (Safety first!)
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)
print("Email sent successfully! 🚀")
# --- Test it out ---
if __name__ == "__main__":
send_email(
subject="Python Test",
body="Hello from your Python script!",
to_email="your_email@gmail.com" # Send it to yourself to test
)
Step 3: Connect it to Real Projects 🔗
Now that you have this function, you can use it anywhere.
Remember our Bitcoin Tracker from yesterday? You can upgrade it in two seconds.
Old Code (Desktop Notification):
notification.notify(title='Alert', message='Price is low!')New Code (Mobile Alert):
send_email(
subject="BITCOIN ALERT 💰",
body="Price has dropped below $95,000. Go buy!",
to_email="your_personal_email@gmail.com"
)Common Errors & Fixes ⚠️
“Username and Password not accepted”
This means 99% of the time you are using your real password, not the App Password. Go back to Step 1 and generate a new code.
“ConnectionRefusedError”
Your firewall or antivirus might be blocking Python from connecting to the internet. Try disabling your antivirus temporarily to test.
Frequently Asked Questions
Can I send attachments (PDFs)?
Yes, but the code is a bit more complex. You need to use msg.add_attachment(). For beginners, start with simple text emails first.
Can I use this to spam people?
Please don’t. Gmail has strict limits (approx. 500 emails/day). If you try to send thousands of marketing emails with this script, Google will ban your account faster than you can say “Python”. Use services like Mailchimp for marketing.
Does this work with Outlook/Hotmail?
Yes! Just change the server address to smtp-mail.outlook.com and make sure you use your Outlook App Password.






