Backend Development 10 min read

Python Alarm Clock with Voice Alerts and Email Notification

This tutorial explains how to build a Python alarm clock that plays a sound, uses text‑to‑speech for voice alerts, and optionally sends an email via QQ SMTP when the alarm triggers, providing full source code and step‑by‑step instructions.

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

The article introduces a simple Python project that creates an alarm clock to help users avoid oversleeping. It combines sound playback, text‑to‑speech, and optional email notifications, making the utility both entertaining and functional.

Required modules

<code>import time
from datetime import datetime
from playsound import playsound  # plays the alarm sound
import pyttsx3
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
</code>

For text‑to‑speech, the pyttsx3 library is used; for playing the alarm sound, playsound is employed; and smtplib together with email.mime handles email sending.

Setting the alarm time

<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 user inputs the desired time and period, the script confirms the settings and starts the monitoring loop.

Alarm monitoring loop

<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>

The loop continuously checks the current time; when it matches the set alarm, it prints a wake‑up message, plays a sound file, and optionally repeats after ten minutes while delivering a humorous voice prompt.

Email notification (optional)

The script can send an email using QQ's SMTP service. First, configure the SMTP object:

<code>smtpObj = smtplib.SMTP(host, port, local_hostname)
# host: e.g., "smtp.qq.com"
# port: usually 25 for plain SMTP or 465 for SSL
# local_hostname: "localhost" if the server runs locally
</code>

To send an email:

<code>SMTP.sendmail(from_addr, to_addrs, msg)
# from_addr: sender email address
# to_addrs: list of recipient addresses
# msg: full email content (headers + body) as a string
</code>

Example function that builds the message and sends it via SSL:

<code>def mail():
    ret = True
    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()
    except Exception:
        ret = False
    return ret
</code>

After the alarm finishes, the script calls mail() and prints whether the email was sent successfully.

Full source code

<code>import time
from datetime import datetime
from playsound import playsound
import pyttsx3
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr

def alarm():
    pp = pyttsx3.init()
    alarm_time = input("输入要设置的闹钟时间:HH:MM:SS\n")
    alarm_period = input("请输入要设置的时期(AM或PM):\n")
    alarm_hour = alarm_time[0:2]
    alarm_minute = alarm_time[3:5]
    alarm_seconds = alarm_time[6:8]
    alarm_period = alarm_period.upper()
    print("设置成功正在运行,祝您休息愉快....zzZZ..")
    flag = True
    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()
    return 1

def mail_qq():
    my_sender = '[email protected]'  # sender email
    my_pass = '***'            # SMTP authorization code
    my_user = '[email protected]' # recipient email
    def mail():
        ret = True
        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()
        except Exception:
            ret = False
        return ret
    ret = mail()
    if ret:
        print("邮件发送成功")
    else:
        print("邮件发送失败")

if __name__ == '__main__':
    a = alarm()
    if a == 1:
        mail_qq()
</code>

The complete script demonstrates how to combine time handling, audio playback, text‑to‑speech, and SMTP email to create a multifunctional alarm tool.

automationemailpyttsx3smtplibalarm
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.