E-Mails Notification Bot With Python
Last Updated :
03 Apr, 2024
Email continues to be a widely used communication method, making it an effective platform for receiving updates and staying connected. However, manual email notifications for recurring events or tasks can be inefficient and time-consuming. Python offers a robust toolset to develop automated email notification bots, enabling seamless and efficient communication. In this article we will see how to create an e-mail notification bot With Python.
E-Mails Notification Bot With Python
Below, is the implementation of e-mail bot Notification with Python:
Create a Virtual Environment
First, create the virtual environment using the below commands
python -m venv env
.\env\Scripts\activate.ps1
Install Necessary Library
With Python and a Gmail account established, you need to obtain the essential Python libraries. using pip, Python’s package manager, to do this. Launch a terminal or command prompt and execute the following commands:
pip install smtplib
pip install secure-smtplib
These commands will install the smtplib library for sending emails and secure-smtplib library for establishing a secure connection with the SMTP server.
Complete Code
Here, Python code example for an email notification bot that sends a notification email using Gmail’s SMTP server when a certain condition is met (in this case, a specific time of day): Below code sets up a simple email notification bot that sends a notification email every day at 9:00 AM. You have to replace the placeholders (your_email@gmail.com, your_password, recipient_email@example.com) with your actual email credentials and recipient email address. To use Gmail as your SMTP server, you might have to enable access for low-security apps. Also, consider keeping sensitive data, such as email credentials, in a secure place like environment variables or a configuration file.
main.py
Python3
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import datetime
import time
# Email configuration
SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
SENDER_EMAIL = 'cogofap214@azduan.com'
SENDER_PASSWORD = 'ExamplePassword123'
RECIPIENT_EMAIL = 'hadaa914@gmail.com'
def send_email(subject, body):
try:
# Connect to SMTP server
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls()
server.login(SENDER_EMAIL, SENDER_PASSWORD)
# Compose email message
msg = MIMEMultipart()
msg['From'] = SENDER_EMAIL
msg['To'] = RECIPIENT_EMAIL
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
# Send email
server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
print('Email notification sent successfully!')
# Close connection
server.quit()
except Exception as e:
print('Error sending email notification:', e)
def main():
while True:
print("You will be notified on your E-mail daily by 9:00 AM")
# Check if current time is 9:00 AM
if datetime.datetime.now().hour == 9 and datetime.datetime.now().minute == 0:
subject = 'Daily Notification'
body = 'This is your daily notification. Have a great day!'
send_email(subject, body)
# Wait for 1 minute before checking again
time.sleep(60)
if __name__ == '__main__':
main()
Run the Server
python main.py
Output
Conclusion
In conclusion, Using Python to automate email notifications streamlines communication, boosting efficiency and productivity. Python’s robust libraries and tools allow you to create email bots tailored to your needs. Whether for business, personal, or educational use, automated email notifications keep you connected and informed without manual effort, making communication in the digital age effortless and convenient.
Share your thoughts in the comments
Please Login to comment...