by: Cobena Isaac

How to Send Email Notifications with Python

How to Send Email Notifications with Python

Assuming, you want Python to notify you automatically when a task finishes — maybe a cleanup script completed, a file was generated, or a long backup process finished. Instead of checking logs manually, you can make Python send you an email alert the moment the job is done.

This is perfect for receiving updates after your automations run, getting alerts if something fails, or simply tracking long-running jobs like data processing or report generation.

Python has a built-in module called smtplib that lets you send emails directly through an SMTP server, with no extra installations required.

Step 1: Basic Email Setup

Here’s a simple script to send an email using a Gmail account. (The process is similar for other providers like Outlook; you would just use their specific SMTP server details.)

import smtplib
from email.mime.text import MIMEText

# Sender and recipient details
sender_email = "youremail@gmail.com"
receiver_email = "recipient@gmail.com"
password = "your_app_password"  # use an app password, not your main one!

# Email content
subject = "Automation Finished"
body = "Hi there! Your Python automation just completed successfully."

# Create the email message
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = receiver_email

# Connect to Gmail's SMTP server and send the email
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
    server.login(sender_email, password)
    server.send_message(msg)

print("Email sent successfully!")
  • smtplib.SMTP_SSL() — creates a secure connection to Gmail’s outgoing mail server.(connects securely to Gmail’s mail server)
  • server.login() — authenticates you using your email and app password.
  • server.send_message() — delivers your composed message.

Run this script, and you should see the email arrive in the recipient’s inbox almost instantly, or you can check your inbox — Python will have sent the email almost instantly!

Security Note:

  • For Gmail, you cannot use your regular account password. You must generate a 16-digit “App Password”.
  • To get one, go to your Google Account settings, enable 2-Factor Authentication, and then generate an App Password for this script.

Step 2: Enable Gmail App Passwords

To use Gmail with your Python script, you must generate a special App Password instead of using your regular Gmail password. This is a crucial security step.

Here’s how to get your App Password:

  1. Go to your Google Account and navigate to Security → App Passwords.
  2. Under How you sign in to Google, find 2-Step Verification and enable it if you haven’t already.
  3. Once 2-Step Verification is on, return to the Security page. You will now see an option for App passwords.
  4. Create a new App Password (Select Mail as the app → Select Other (Custom name) as the device and type Python Script).
  5. Google will generate a 16-character password. Copy this password immediately — you won’t be able to see it again.
  6. Use the generated password in your Python script instead of your real one.

Python will now authenticate securely using that app password.


Step 3: Automate the Notification

You can integrate this email-sending code directly into your automation scripts. Simply place it at the end of your main task — such as your folder cleanup or scheduled task or job — so you’re automatically notified when it completes.

Example Integration:

def main_task():
    print("Running automated cleanup...")
    # your main automation code goes here
    print("Task finished!")

    # Send an automatic email notification
    send_email_notification()

def send_email_notification():
    import smtplib
    from email.mime.text import MIMEText

    sender_email = "youremail@gmail.com"
    receiver_email = "recipient@gmail.com"
    password = "your_app_password"
    
    # Create the email message
    msg = MIMEText("Your Python task just finished successfully.")
    msg["Subject"] = "Python Automation Alert"
    msg["From"] = sender_email
    msg["To"] = receiver_email
    
    # Send the email
    with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
        server.login(sender_email, password)
        server.send_message(msg)

    print("Email sent — you can check your inbox now!")

if __name__ == "__main__":
    main_task()

Now, every time your script runs, it will notify you the moment it finishes, turning any automation into a self-reporting system.


Tips

  • Wrap your main task in a try/except block to send an error email if something fails, ensuring you’re notified of both successes and failures.
  • Send notifications to multiple people by using a list of email addresses:
  msg["To"] = ", ".join(["user1@gmail.com", "user2@gmail.com"])
  • The script works with any email service. For non-Gmail accounts, just replace the Gmail server details with your provider’s:
    • Outlook/Hotmail: smtp.office365.com, port 587
    • Yahoo Mail: smtp.mail.yahoo.com, port 465 or 587
    • iCloud Mail: smtp.mail.me.com, port 587

For services that use port 587 (like Outlook), replace SMTP_SSL with SMTP and use starttls():

server = smtplib.SMTP("smtp.office365.com", 587)
server.starttls()  # Upgrade to secure connection
server.login(sender_email, password)

In this lesson, you learned that:

  • Python can send email alerts using the built-in smtplib.
  • Gmail’s app passwords allow safe authentication.
  • You can easily integrate email notifications into your existing scripts.

Now, every time your automation completes, Python will notify you automatically

👉 Next step: Explore Automating File Organization with Python.