Email Automation Script in Python.
About the project: This is a project for an Email Automation Script in Python. This will be a self-contained script that can send an email automatically from your Gmail account.
- Before You Run the Code
- Enable Two-Factor Authentication (2FA): Ensure that Two-Factor Authentication is enabled for your Google account.
- Generate an App Password: Go to your Google Account settings, navigate to the "Security" section, and find the "App passwords" option under "2-step Verification." Create a new app password for a custom app (e.g., "Python Email Script"). This will generate a unique 16-character password. You must use this app password in the script, not your regular Gmail password.
- This script will prompt you for the sender's email address and the recipient's email address and then send a pre-defined email message.
This script uses the smtplib and email modules, which are part of Python's standard library, so no installation is required. However, you'll need to configure your Gmail account to allow access for this script to work.
This project provides a basic but functional email automation script. You can run it directly from your terminal after setting up the App Password for your Google account.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# email_automation_script.py
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import getpass
def send_automated_email(sender_email, app_password, recipient_email):
"""
Sends an automated email using a Gmail account.
Args:
sender_email (str): The email address of the sender (your Gmail address).
app_password (str): The App Password for your Google account.
recipient_email (str): The email address of the recipient.
"""
try:
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = recipient_email
message["Subject"] = "Automated Email from Python Script"
# Email body text
body = """
Hello,
This is an automated email sent from a Python script.
You have successfully created and run the Email Automation project.
Best regards,
Your Python Script
"""
# Attach the body to the message
message.attach(MIMEText(body, "plain"))
# Connect to the SMTP server
# For Gmail, the server is smtp.gmail.com and the port is 587
with smtplib.SMTP("smtp.gmail.com", 587) as server:
# Start TLS for security
server.starttls()
# Log in to your Gmail account using the App Password
server.login(sender_email, app_password)
# Send the email
server.sendmail(sender_email, recipient_email, message.as_string())
print("Email sent successfully!")
except smtplib.SMTPAuthenticationError:
print("Failed to authenticate. Please check your email and App Password.")
print("Ensure you are using an App Password, not your regular Gmail password.")
except Exception as e:
print(f"An error occurred: {e}")
def main():
"""
Main function to run the email automation script.
"""
print("--- Python Email Automation Script ---")
# Get user inputs
sender_email = input("Enter your Gmail address: ").strip()
# Use getpass for secure password input that is not echoed to the console
app_password = getpass.getpass("Enter your App Password: ").strip()
recipient_email = input("Enter the recipient's email address: ").strip()
# Validate inputs
if not sender_email or not app_password or not recipient_email:
print("Error: All fields are required.")
return
send_automated_email(sender_email, app_password, recipient_email)
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
← Back to Projects