In this blog post, I will guide you through the process of creating a straightforward Gmail notifier using Python. Python offers a wide range of notification capabilities, such as sending commands to an Arduino unit, playing sounds, opening windows, and more. While the code provided below demonstrates playing music as a notification method, the potential for different notification methods is limitless.


Prerequisites:


The Code:
To implement this program, you will need two sections of code. The first section parses your account data, sends it to Gmail, and checks for new mail. The second section executes this program every second and provides notifications when a new email is detected. The code provided below plays three notes using the winsound library (note that this feature is only compatible with Windows computers).

First portion- Parser.py:

import feedparser
import winsound

# Constants
USERNAME = "YOUR.GMAIL.ADDRESS@gmail.com"
PASSWORD = "YOUR PASSWORD"
PROTO = "https://"
SERVER = "mail.google.com"
PATH = "/gmail/feed/atom"

def check_email():
"""
Check if there are new emails in the Gmail inbox.
"""
try:
# Fetching email feed
feed = feedparser.parse(f"{PROTO}{USERNAME}:{PASSWORD}@{SERVER}{PATH}")
new_email_count = int(feed["feed"]["fullcount"])

# If there are new emails, play notification sound
if new_email_count > 0:
play_notification_sound()
except Exception as e:
print(f"Error: {e}")

def play_notification_sound():
"""
Play notification sound when new email is detected.
"""
# Sound frequencies and durations
sound_info = [(440, 500), (370, 500), (392, 500)]

# Play each tone in the sound_info list
for frequency, duration in sound_info:
winsound.Beep(frequency, duration)

if __name__ == "__main__":
check_email()


Second portion- Checker.py:    

import parser2
import time

def main():
"""
Continuously checks for new emails and runs the email checking program.
"""
while True:
parser2.mail(0) # Check for new emails
time.sleep(10) # Wait for ten seconds before the next check

if __name__ == "__main__":
main()

The Full code :

import feedparser
import winsound
import time

# Constants
USERNAME = "YOUR.GMAIL.ADDRESS@gmail.com"
PASSWORD = "YOUR PASSWORD"
PROTO = "https://"
SERVER = "mail.google.com"
PATH = "/gmail/feed/atom"

def check_email():
"""
Check if there are new emails in the Gmail inbox.
"""
try:
# Fetching email feed
feed = feedparser.parse(f"{PROTO}{USERNAME}:{PASSWORD}@{SERVER}{PATH}")
new_email_count = int(feed["feed"]["fullcount"])

# If there are new emails, play notification sound
if new_email_count > 0:
play_notification_sound()
except Exception as e:
print(f"Error: {e}")

def play_notification_sound():
"""
Play notification sound when new email is detected.
"""
# Sound frequencies and durations
sound_info = [(440, 500), (370, 500), (392, 500)]

# Play each tone in the sound_info list
for frequency, duration in sound_info:
winsound.Beep(frequency, duration)

def main():
"""
Run the email checking program continuously.
"""
while True:
check_email()
time.sleep(10) # Check every ten seconds

if __name__ == "__main__":
main()

In conclusion, this blog post provided a comprehensive guide on creating a Gmail notifier using Python. By following the steps outlined in the post, you can set up a simple yet effective notification system for your Gmail account. Python's versatility allows for various notification methods, and the code provided demonstrates playing a sound as a notification using the winsound library.

Feel free to experiment with different notification methods by modifying the code. With Python's capabilities, the possibilities for enhancing your Gmail notifier are endless.