Email Automation System
Abstract
Email Automation System is a Python project that automates email sending and management. The application features scheduling, templates, and a CLI interface, demonstrating best practices in productivity and communication.
Prerequisites
- Python 3.8 or above
- A code editor or IDE
- Basic understanding of email protocols
- Required libraries:
smtplib
smtplib
,email
email
,schedule
schedule
Before you Start
Install Python and the required libraries:
Install dependencies
pip install schedule
Install dependencies
pip install schedule
Getting Started
Create a Project
- Create a folder named
email-automation-system
email-automation-system
. - Open the folder in your code editor or IDE.
- Create a file named
email_automation_system.py
email_automation_system.py
. - Copy the code below into your file.
Write the Code
⚙️ Email Automation System
Email Automation System
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class EmailAutomationSystem:
def __init__(self, smtp_server, port, username, password):
self.smtp_server = smtp_server
self.port = port
self.username = username
self.password = password
def send_email(self, to_email, subject, body):
msg = MIMEMultipart()
msg['From'] = self.username
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
with smtplib.SMTP(self.smtp_server, self.port) as server:
server.starttls()
server.login(self.username, self.password)
server.sendmail(self.username, to_email, msg.as_string())
print(f"Email sent to {to_email}")
if __name__ == "__main__":
print("Email Automation System Demo")
# system = EmailAutomationSystem('smtp.example.com', 587, 'user@example.com', 'password')
# system.send_email('recipient@example.com', 'Test Subject', 'Hello from Python!')
Email Automation System
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class EmailAutomationSystem:
def __init__(self, smtp_server, port, username, password):
self.smtp_server = smtp_server
self.port = port
self.username = username
self.password = password
def send_email(self, to_email, subject, body):
msg = MIMEMultipart()
msg['From'] = self.username
msg['To'] = to_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
with smtplib.SMTP(self.smtp_server, self.port) as server:
server.starttls()
server.login(self.username, self.password)
server.sendmail(self.username, to_email, msg.as_string())
print(f"Email sent to {to_email}")
if __name__ == "__main__":
print("Email Automation System Demo")
# system = EmailAutomationSystem('smtp.example.com', 587, 'user@example.com', 'password')
# system.send_email('recipient@example.com', 'Test Subject', 'Hello from Python!')
Example Usage
Run email automation
python email_automation_system.py
Run email automation
python email_automation_system.py
Explanation
Key Features
- Email Sending: Automates sending emails.
- Scheduling: Schedules emails for future delivery.
- Templates: Uses email templates for consistency.
- Error Handling: Validates inputs and manages exceptions.
- CLI Interface: Interactive command-line usage.
Code Breakdown
- Import Libraries and Setup System
email_automation_system.py
import smtplib
from email.mime.text import MIMEText
import schedule
import time
email_automation_system.py
import smtplib
from email.mime.text import MIMEText
import schedule
import time
- Email Sending and Scheduling Functions
email_automation_system.py
def send_email(subject, body, to_email):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = 'your_email@example.com'
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com') as server:
server.login('your_email@example.com', 'password')
server.send_message(msg)
def schedule_email(subject, body, to_email, send_time):
schedule.every().day.at(send_time).do(send_email, subject, body, to_email)
while True:
schedule.run_pending()
time.sleep(1)
email_automation_system.py
def send_email(subject, body, to_email):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = 'your_email@example.com'
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com') as server:
server.login('your_email@example.com', 'password')
server.send_message(msg)
def schedule_email(subject, body, to_email, send_time):
schedule.every().day.at(send_time).do(send_email, subject, body, to_email)
while True:
schedule.run_pending()
time.sleep(1)
- CLI Interface and Error Handling
email_automation_system.py
def main():
print("Email Automation System")
while True:
cmd = input('> ')
if cmd == 'send':
subject = input("Subject: ")
body = input("Body: ")
to_email = input("Recipient: ")
send_email(subject, body, to_email)
print("Email sent.")
elif cmd == 'schedule':
subject = input("Subject: ")
body = input("Body: ")
to_email = input("Recipient: ")
send_time = input("Send time (HH:MM): ")
schedule_email(subject, body, to_email, send_time)
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'send', 'schedule', or 'exit'.")
if __name__ == "__main__":
main()
email_automation_system.py
def main():
print("Email Automation System")
while True:
cmd = input('> ')
if cmd == 'send':
subject = input("Subject: ")
body = input("Body: ")
to_email = input("Recipient: ")
send_email(subject, body, to_email)
print("Email sent.")
elif cmd == 'schedule':
subject = input("Subject: ")
body = input("Body: ")
to_email = input("Recipient: ")
send_time = input("Send time (HH:MM): ")
schedule_email(subject, body, to_email, send_time)
elif cmd == 'exit':
break
else:
print("Unknown command. Type 'send', 'schedule', or 'exit'.")
if __name__ == "__main__":
main()
Features
- Email Automation: Sending and scheduling emails
- Modular Design: Separate functions for each task
- Error Handling: Manages invalid inputs and exceptions
- Production-Ready: Scalable and maintainable code
Next Steps
Enhance the project by:
- Integrating with real email services
- Supporting advanced scheduling options
- Creating a GUI for email management
- Adding attachment support
- Unit testing for reliability
Educational Value
This project teaches:
- Productivity: Email automation and scheduling
- Software Design: Modular, maintainable code
- Error Handling: Writing robust Python code
Real-World Applications
- Business Communication
- Marketing Automation
- Productivity Tools
Conclusion
Email Automation System demonstrates how to build a scalable and automated email tool using Python. With modular design and extensibility, this project can be adapted for real-world applications in communication, marketing, and more. For more advanced projects, visit Python Central Hub.
Was this page helpful?
Let us know how we did