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 ([email protected], your_password, [email protected]) 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 = '[email protected]'
SENDER_PASSWORD = 'ExamplePassword123'
RECIPIENT_EMAIL = '[email protected]'
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.
Similar Reads
Create a Web-Crawler Notification Bot in Python In this article, we will guide you through the process of creating a Web-Crawler Notification Bot using Python. This notification bot is designed to display notification messages on your window, providing a useful tool for alerting users to relevant information gathered through web crawling. What is
3 min read
Handling mails with EZGmail module in Python EZGmail is a Python module that can be used to send and receive emails through Gmail. It works on top of the official Gmail API. Although EZGmail does not cover everything that can be done with the Gmail API, it makes the common tasks very simple which are a lot more complicated using Google's own A
4 min read
Chat Bot in Python with ChatterBot Module Nobody likes to be alone always, but sometimes loneliness could be a better medicine to hunch the thirst for a peaceful environment. Even during such lonely quarantines, we may ignore humans but not humanoids. Yes, if you have guessed this article for a chatbot, then you have cracked it right. We wo
3 min read
How to Send Beautiful Emails in Python In this article we will send stylized emails using Python we will be using smtplib. The Python smtplib module defines an SMTP client session object which can be used to send mail to any recipient with an SMTP listener. To send emails to any legitimate email address on the internet, "smtplib" produce
3 min read
Create a ChatBot with OpenAI and Gradio in Python Computer programs known as chatbots may mimic human users in communication. They are frequently employed in customer service settings where they may assist clients by responding to their inquiries. The usage of chatbots for entertainment, such as gameplay or storytelling, is also possible.OpenAI Cha
3 min read