The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a script to notify you of expiring warranties

Here’s a Python script that notifies you of expiring warranties by checking a local CSV file (warranties.csv) and sending a notification (via email or console alert) for items whose warranties are about to expire within a specified number of days.


✅ Script Features:

  • Accepts a list of warranty items in a CSV file.

  • Checks if a warranty is expiring within a set warning period (e.g., 30 days).

  • Sends console alerts or email notifications.


✅ Step 1: Create a CSV File

Create a warranties.csv file like this:

csv
Item,PurchaseDate,WarrantyMonths Laptop,2023-01-15,24 Phone,2022-06-10,36 Washing Machine,2021-12-01,48

✅ Step 2: Python Script to Check and Notify

python
import csv from datetime import datetime, timedelta import smtplib from email.mime.text import MIMEText # Configuration CSV_FILE = 'warranties.csv' DAYS_BEFORE_EXPIRY = 30 USE_EMAIL_NOTIFICATION = False # Set to True to use email EMAIL_SENDER = 'youremail@example.com' EMAIL_RECEIVER = 'receiver@example.com' SMTP_SERVER = 'smtp.example.com' SMTP_PORT = 587 EMAIL_PASSWORD = 'yourpassword' def read_warranties(file_path): warranties = [] with open(file_path, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: item = row['Item'] purchase_date = datetime.strptime(row['PurchaseDate'], '%Y-%m-%d') warranty_months = int(row['WarrantyMonths']) expiry_date = purchase_date + timedelta(days=warranty_months * 30) warranties.append({ 'item': item, 'expiry_date': expiry_date }) return warranties def notify(item, expiry_date): message = f"⚠️ Warranty for '{item}' expires on {expiry_date.date()}!" if USE_EMAIL_NOTIFICATION: send_email_notification("Warranty Expiry Alert", message) else: print(message) def send_email_notification(subject, body): msg = MIMEText(body) msg['Subject'] = subject msg['From'] = EMAIL_SENDER msg['To'] = EMAIL_RECEIVER with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: server.starttls() server.login(EMAIL_SENDER, EMAIL_PASSWORD) server.send_message(msg) print("✅ Email sent successfully") def check_expiring_warranties(): warranties = read_warranties(CSV_FILE) today = datetime.today() threshold_date = today + timedelta(days=DAYS_BEFORE_EXPIRY) for w in warranties: if today <= w['expiry_date'] <= threshold_date: notify(w['item'], w['expiry_date']) if __name__ == "__main__": check_expiring_warranties()

✅ Optional: Automate with Cron (Linux/macOS) or Task Scheduler (Windows)

  • Linux/macOS: Add to crontab:

    bash
    0 9 * * * /usr/bin/python3 /path/to/script.py
  • Windows: Use Task Scheduler to run the script daily.


Let me know if you want a version with a GUI, push notifications, or integrations like Slack or SMS.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About