Fundamentals 6 min read

Python Alarm Clock with Voice Alerts and Email Notification

This tutorial demonstrates how to build a Python alarm clock that plays a sound, uses text‑to‑speech for custom messages, and can automatically send an email via SMTP when the alarm triggers, providing complete code snippets and step‑by‑step explanations.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Alarm Clock with Voice Alerts and Email Notification

This article introduces a simple yet functional Python alarm clock project aimed at helping users avoid oversleeping and even delivering humorous resignation or breakup messages.

It begins by listing the required modules:

<code>import time
from datetime import datetime
from playsound import playsound  # plays the alarm sound
import pyttsx3  # text‑to‑speech engine</code>

The user is prompted to set the alarm time and period (AM/PM):

<code>alarm_time = input("输入要设置的闹钟时间:HH:MM:SS\n")
alarm_period = input("请输入要设置的时期(AM或PM):\n")
alarm_hour = alarm_time[0:2]  # hour
alarm_minute = alarm_time[3:5]  # minute
alarm_seconds = alarm_time[6:8]  # second
alarm_period = alarm_period.upper()
print("设置成功正在运行,祝您休息愉快....zzZZ..")</code>

After the alarm is set, a loop continuously checks the current time and triggers the alarm when the hour, minute, second, and period match the user’s settings:

<code>while flag:
    now = datetime.now()
    current_hour = now.strftime("%I")
    current_minute = now.strftime("%M")
    current_seconds = now.strftime("%S")
    current_period = now.strftime("%p")
    if alarm_period == current_period:
        if alarm_hour == current_hour:
            if alarm_minute == current_minute:
                if alarm_seconds == current_seconds:
                    print("Wake Up!!!")
                    playsound('1.mp3')
                    if int(now.strftime("%M")) - int(alarm_minute) == 10:
                        playsound('1.mp3')
                        time.sleep(60)
                        pp.say('还不醒?那你可别怪我了都是为你好呀。这就去帮你辞职!哈哈哈哈')
                        pp.runAndWait()</code>

For a more dramatic effect, the script can send an email using Python’s SMTP library. First, the SMTP module is imported and its usage explained:

<code>import smtplib
smtpObj = smtplib.SMTP(host, port, local_hostname)</code>

The email‑sending function creates a MIME text message, sets the sender, recipient, and subject, then connects to QQ’s SMTP server over SSL to deliver the message:

<code>def mail():
    try:
        msg = MIMEText('想睡觉,不干了', 'plain', 'utf-8')
        msg['From'] = formataddr(["我是肥学,老子干了", my_sender])
        msg['To'] = formataddr(["肥学", my_user])
        msg['Subject'] = "辞职报告"
        server = smtplib.SMTP_SSL("smtp.qq.com", 465)
        server.login(my_sender, my_pass)
        server.sendmail(my_sender, [my_user], msg.as_string())
        server.quit()
        return True
    except Exception:
        return False

if mail():
    print("邮件发送成功")
else:
    print("邮件发送失败")</code>

The article also includes screenshots showing the alarm’s operation, instructions for obtaining a QQ email authorization code, and promotional material for a free Python public course.

Tutorialpyttsx3alarm-clockplaysoundsmtplib
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.